如何用新敌人重置游戏?

时间:2018-05-08 18:37:47

标签: javascript loops readline

所以,我正在使用readline-sync构建一个基本的命令行游戏。 基本的前提是你与敌人战斗,如果你杀死了敌人,游戏就会继续下去,如果你死了游戏就会打另一个。

我的问题是,当我杀死敌人并且游戏继续进行时,它仍然与同一个敌人和敌人的健康状况相同。我如何才能得到它,以便当我杀死一个敌人时,一个新的敌人进入100个健康状态我会和他战斗?

我想我知道我只是从构造函数中创建了一个敌人,但有没有办法让它在敌人死亡时重建一个拥有100个生命值的全新敌人?

这是我的JavaScript:

var readline = require("readline-sync");

function Player(name, hp, items){
    this.name = name;
    this.health = hp;
    this.items = items;
}

function Enemy(name, hp, items){
    this.name = name;
    this.health = hp;
    this.items = items;
}

//1 in 3 chance of getting attacked
var enemyAttacking = ["safe", "safe", "attack"];

//1 in 2 chance of escaping when running
var escaped = [1, 2];

//aren't getting attacked message
var noAttackMes = ["You didn't run into any monsters. Move along...", "No creatures along the pathway. Continue walking.", "You're all alone. Keep going." , "Enemies are not engaging you. Keep walking.", "The path is clear for you. Continue your journey.", "Enemies are nearby, but are not attacking. Keep walking"];

 //randomly chooses a weapon for the enemy to have
 var weapon = ["axe", "sword", "bow and arrow"];
 var weaponSelected = Math.floor(Math.random() * weapon.length);

 //randomly chooses an enemy
 var enemies = ["giant", "dark wizard", "wolf"];
 var enemyChosen = Math.floor(Math.random() * enemies.length);
 var newEnemy = new Enemy(enemies[enemyChosen], 100, weapon[weaponSelected]);

 //randomly choose an amount of hp to remove
 var hpRemove = [20, 40, 60, 80];

//greeting player
var playerName = readline.question("\nGreetings! Welcome to the Seven Kingdoms! In order to gain passage, you must tell me your name: ");
var newPlayer = new Player(playerName, 100, "none");
console.log(`\nGreetings ${playerName}! You have been granted access, but beware of all the nasty beasts and creatures that lurk in the shadows...`);

//will run walk() until player dies
while(newPlayer.health > 0){
    walk();
}

function walk(){  
    var playerWalk = readline.question("\nPlease press 'w' to walk or type 'print' to view inventory: ");
     if(playerWalk === "w"){
         if(enemyAttacking[Math.floor(Math.random() * enemyAttacking.length)] === "attack"){
             console.log("You're being attacked!");
             fight();
         } else {
             console.log(noAttackMes[Math.floor(Math.random() * noAttackMes.length)]);
         }
     }else if(playerWalk === "print") {
         console.log("Here's what's in your inventory:");
         console.log(newPlayer);   
    }
}

function enemyAttack(){
    if(newEnemy.health <= 0){
        console.log(`\nYou have killed the ${enemies[enemyChosen]}! He has dropped his ${weapon[weaponSelected]} and you can now reclaim it as your own.`);
    }else {
        newPlayer.health -= hpRemove[Math.floor(Math.random() * hpRemove.length)];
        console.log(`\nThe ${enemies[enemyChosen]} has attacked you back! Your health now stands at ${newPlayer.health}.`);
    }

    if(newPlayer.health <= 0){
        console.log(`\nYou have died the most gruesome of gruesome deaths. Do not be ashamed, as you fought with honor, and will be welcomed to Valhalla with open arms.`);
    }
}

function fight(){
    var fightOrFlight = ["Fight", "Run"];
    var fightChoice = readline.keyInSelect(fightOrFlight, `The ${enemies[enemyChosen]} is attacking you! What do you want to do?!`);

    if(fightChoice === 0){
        attackEnemy();
    }else {
        run();
    }

    while(newEnemy.health > 0 && newPlayer.health > 0){
        var fightChoice2 = readline.keyInSelect(fightOrFlight, `You have both taken hits. What will you like to do next?`);
        if(fightChoice2 === 0){
            attackEnemy();
        }else {
            run();
        }
    }
}

function run(){
    if(escaped[Math.floor(Math.random() * escaped.length)] === 1){
        console.log("\nYou have escaped the creature and may continue walking!");
    }else {
        console.log("\nYou have not escaped! The creature is attacking!");
        attackEnemy();
    }
}

function attackEnemy(){
    newEnemy.health -= hpRemove[Math.floor(Math.random() * hpRemove.length)];
    console.log(`\nYou have attacked the ${enemies[enemyChosen]}! His health now stands at ${newEnemy.health}.`);
    if(newEnemy.health <= 0){
        newPlayer.items = newEnemy.items;
        newPlayer.health += 20;
    }
    enemyAttack();
}

1 个答案:

答案 0 :(得分:1)

要重新创建新的敌人,您可以添加以下代码:

newEnemy = new Enemy( enemies[Math.floor(Math.random() * enemies.length);], 100, weapon[Math.floor(Math.random() * weapon.length)] );

在条件if(newEnemy.health <= 0){