今天在图书馆泡了一天,也敲了一天的代码,感觉还是有信心的,只要每天都比昨天强一点就行,相信自己!
下面是代码,这个游戏主要是函数跟while循环的调用,感觉还是太简单了点
var slaying = true;
var youHit = Math.floor(Math.random()*2 );
var damageThisRound = Math.floor(Math.random()*5 + 1);
var totalDamage = 0;
while (slaying) {
if (youHit){
console.log("Congratulations!");
totalDamage +=damageThisRound;
if (totalDamage >= 4){
console.log("you slew the dragon!");
slaying = false;
}
else {
youHit = Math.floor(Math.random()*2 );
}
}
else {
console.log("Dragon defeated you!");
slaying = false;
}
}