首页 > 解决方案 > 我正在构建一个基于文本的 RPG。如何在房间之间导航?

问题描述

我正在通过构建一个非常基本的基于文​​本的角色扮演游戏来学习 JavaScript。

我设计了两个房间供角色穿梭。第一个房间,West 没有任何物品或怪物。第二个房间,东,有 3 种药水和一个地精。理想情况下,用户将能够将他的角色从西移动到东,并一次拾取一种药水并攻击地精。如果用户的角色在西屋,由于西屋完全是空的,该角色无法拾取任何物品或与任何怪物战斗。

我将房间设计为对象,并为导航和交互创建了一些函数:move()、moveEast()、get() 和 cast()。我很难弄清楚如何将 currentLocation 指定为 East 或 West,以及如何将其与 get() 函数联系起来。请帮忙。这是下面的代码...

var character = {
    name: "Morrigan",
    class: "Sorceress",
    level: 1,
    health: 40,
    items: "ring",
    currentLocation: {
        visible: null,
        item: [" ", 1],
        monster: " ",
    }
}

//Creating the monster class and several monsters underneath.
function Monster (name, level, health, strength, living)
{
    this.name = name
    this.level = level
    this.health = health
    this.strength = strength
    this.living = living

    this.showMonster = function()
    {
        document.write("Name: " + this.name + "<br>")
        document.write("Level: " + this.level + "<br>")
        document.write("Health: " + this.health + "<br>")
        document.write("strength: " + this.strength + "<br>")
    }
}

//Creating different types of monsters.
let goblin = new Monster("Goblin", 2, 24, 5, true);
let wolf = new Monster("Wolf", 4, 30, 10, true);
let orc = new Monster("Orc", 12, 50, 20, true);

//User movement.
function moveEast () {
    if (East.visible == true) {
        console.log("You can't move East, boy!");
    } else {
        console.log("You move East with yo smart ass! Now go find me some truffles.");
        East.visible = true;
        West.visible = false;
        character.currentLocation = "East";
    }
}
function moveWest () {
    if (West.visible == true) {
        console.log("You can't move West, boy!");
    } else {
        console.log("You move West with yo smart ass! Now make me some rice noodles!");
        West.visible = true;
        East.visible = false;
        character.currentLocation = "West";
    }
}

//Casts a spell to attack any goblin.
function cast() {
    if (goblin.living == true) {
        var damage = Math.floor(Math.random() * 6) + 1;
        goblin.health -= damage;
        console.log("You attack!" + " " + "You inflect " + damage + " of damage!")
        if (goblin.health <= 0) {
            return "You destroyed that ass goblin!";
            goblin.living = false;
            document.write("Mister Goblin died.");
        } else {
        console.log("Goblin health:" + " " + goblin["health"]);
        }
    } else {
        console.log("There is no goblin, you silly bastard!");
        return "Done";
    }
}

//Locations.
var East = {
    visible: null,
    item: ["potion", 3],
    monster: goblin,
    currentLocation: "East",
}
var West = {
    visible: null,
    item: ["There's nothing here", 0],
    monster: "There's no one here",
    currentLocation: "West",
}
var currentLocation = {
    visible: null,
    item: [" ", 1],
    monster: " ",
}
function get () {
    character.currentLocation = East;
    if (character.currentLocation.item[1] > 0) {
     console.log("You pickup a " + character.currentLocation.item[0] + ".");
     character.currentLocation.item[1] -= 1;
     console.log("There are " + character.currentLocation.item[1] + " left.");
     } else {
     console.log("There's nothing here you goose!")
     }
 }

标签: javascriptnavigationtext-basedadventure

解决方案


推荐阅读