use if while 判断语句,可以做到文字游戏的剧情。当涉及各种各样的游戏剧情会产生不一样的判定
从而使游戏的趣味ing大大增加。
<pre name="code" class="javascript">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=1)
{
console.log("you hit the dragon");
totalDamage+=damagethisround;
if(totalDamage >= 4)
{
console.log("you beat the dragon!");
slaying=false}
else
{
youHit=Math.floor(Math.random()*2);
}
}else
{
console.log("dragon defeat you");
slaying=false;
}
}