1. 程式人生 > >use javascript for game2 slaying dragon

use javascript for game2 slaying dragon

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;
    }

    
}