Hi everyone,
I did a short program in three hours, tell me how can I improve it :
//INTRODUCTION
alert("Game : Don't die.\n(no save)");
//CLASS AND CONSTRUCTOR
var Avatar = function(name){
this.name = name;
this.health = 100;
this.lvl = 1;
this.xp = 0;
this.lvlUp = function(){
if(this.xp >= 100){
this.lvl += 1;
this.xp = 0;
}
};
}
var monster = {
name : "Goblin",
health : 100,
attack1 : "Punch",
attack2 : "Bite",
attack3 : "Head butt"
}
var resultFight = function(){
if(userAvatar.health < 1 && monster.health < 1){
alert("You have lost...");
exitFight = false;
exitMenu = false;
}else if(userAvatar.health < 1){
alert("You have lost...");
exitFight = false;
exitMenu = false;
}else if(monster.health < 1){
alert("Good ! You have won !\nYou earn 50 experience points.");
userAvatar.xp += 50;
userAvatar.lvlUp();
monster.health = 100;
exitFight = false;
}
}
var attackFight = function(x, y, attackUser, attackMonster){
var monsterAttackDamages = Math.floor(Math.random() * y);
var userAttackDamages = Math.floor(Math.random() * x);
userAvatar.health -= monsterAttackDamages;
monster.health -= userAttackDamages;
if(userAvatar.health >1 && monster.health > 1){
alert("You use " + attackUser + ". Your enemy loses " + userAttackDamages + " health points.");
alert(monster.name + " uses " + attackMonster + ". You lose " + monsterAttackDamages + " health points.");
}else if(userAvatar.health > 1){
alert("You use " + attackUser + ". Your enemy loses " + userAttackDamages + " health points.");
}else if(monster.health > 1){
alert(monster.name + " uses " + attackMonster + ". You lose " + monsterAttackDamages + " health points.");
}else{
alert("I think you're tired...");
}
}
//USER PSEUDO
var exitPseudo = true;
while(exitPseudo){
var userPseudo = prompt("Please, submit your pseudo :");
if(userPseudo.length < 30){
if(confirm("Are you sure you call " + userPseudo + " ?")){
exitPseudo = false;
}else{
alert("Please, submit your pseudo again.");
}
}else{
alert("Ooops... I didn't understand.");
}
}
//STYLE USER
var exitStyle = true;
while(exitStyle){
var userStyle = prompt("What style would you like to be ? (1,2,3 or 4)\n1. Water\n2. Fire\n3. Air\n4. Rock");
switch(userStyle){
case "1":
if(confirm("Are you sure to be water style ?")){
var userAvatar = new Avatar(userPseudo);
userAvatar.style = "Water";
userAvatar.attack1 = "Punch";
userAvatar.attack2 = "Splash";
userAvatar.attack3 = "Hydrocanon";
exitStyle = false;
}else{
alert("Please, submit your style again.");
}
break;
case "2":
if(confirm("Are you sure to be fire style ?")){
var userAvatar = new Avatar(userPseudo);
userAvatar.style = "Fire";
userAvatar.attack1 = "Punch";
userAvatar.attack2 = "Butane lighter";
userAvatar.attack3 = "Flame-throwers";
exitStyle = false;
}else{
alert("Please, submit your style again.");
}
break;
case "3":
if(confirm("Are you sure to be air style ?")){
var userAvatar = new Avatar(userPseudo);
userAvatar.style = "Air";
userAvatar.attack1 = "Punch";
userAvatar.attack2 = "Storm";
userAvatar.attack3 = "Tornado";
exitStyle = false;
}else{
alert("Please, submit your style again.");
}
break;
case "4":
if(confirm("Are you sure to be rock style ?")){
var userAvatar = new Avatar(userPseudo);
userAvatar.style = "Rock";
userAvatar.attack1 = "Punch";
userAvatar.attack2 = "Sludge";
userAvatar.attack3 = "Earthquake";
exitStyle = false;
}else{
alert("Please, submit your style again.");
}
break;
default:
alert("Please, put 1, 2, 3 or 4.");
}
}
var exitMenu = true;
while(exitMenu){
var whatDo = prompt("What do you want to do ? (1,2,3 or 4)\n1. See informations of " + userPseudo + "\n2. Go fight an enemy\n3. Sleep a little\n4. Exit");
switch(whatDo){
case "1":
alert("Name : " + userAvatar.name + "\nStyle : " + userAvatar.style + "\nHealth : " + userAvatar.health + "\nLevel : " + userAvatar.lvl + "\nExperience points : " + userAvatar.xp + "/100");
break;
case "2":
var exitFight = true;
alert("Warning ! There is a " + monster.name + ", fight !");
while(exitFight){
var userChoice = prompt("What do you want to do ? (1,2,3 or 4)\n1. " + userAvatar.attack1 + "\n2. " + userAvatar.attack2 +"\n3. " + userAvatar.attack3 + "\n4. Escape\n\n\tHealth " + userPseudo + " : " + userAvatar.health + "\t Health " + monster.name + " : " + monster.health);
switch(userChoice){
case "1":
attackFight(25, 5, userAvatar.attack1.toLowerCase(), monster.attack1.toLowerCase());
break;
case "2":
attackFight(60, 20, userAvatar.attack2.toLowerCase(), monster.attack2.toLowerCase());
break;
case "3":
attackFight(100, 40, userAvatar.attack3.toLowerCase(), monster.attack3.toLowerCase());
break;
case "4":
exitFight = false;
monster.health = 100;
alert("You're lousy...");
break;
default :
alert("Huum... Do you want to die ?!");
}
resultFight();
}
break;
case "3":
userAvatar.health = 100;
alert("Zzz...Zzz...Zzz......");
alert("You retrieved your health points !");
break;
case "4":
alert("Thanks for playing.");
exitMenu = false;
break;
default:
alert("Please, put 1, 2, 3 or 4.");
}
if(userAvatar.lvl == 3){
alert("Waouw... You're lvl 3 ! You have finished the game, see you soon !");
exitMenu = false;
}
}
Please, be indulgent. I await your advice. I don’t use brackets @lolman