JS互动小说

JS interactive fiction

我正在尝试用 JavaScript 制作一款互动小说游戏,我首先想到的是放下导航系统,这是玩家从一个位置(房间)移动的一种方式,其中包含描述,给另一个。我是 JS 的新手并且还在学习,到目前为止我能够制作的代码根本不起作用,真的不知道为什么,有人可以帮助我吗?也许告诉我另一种更有效的方法?

node = function(desc, exits){
  this.desc = desc;
  this.exits = exits;
}

goTo = function(target){
  goTo(prompt(target[0] + ' where do you want to go? ' + target[1]))
}

/*
goTo = function(target){
  goTo(prompt(target.desc + ' where do you want to go? ' + target.exits))
}


var house = new node('this is the house desc', ['field', 'cave']);
var field = new node('this is the field desc', ['house', 'cave']);
var cave = new node('this is the cave desc', ['field', 'house']);
*/

var house = ['this is the house desc', [field, cave]]
var field = ['this is the field desc', [house, cave]]
var cave = ['this is the cave desc', [field, house]]

goTo(house);

被注释掉的那段代码是我尝试使用函数作为创建对象的方式来做事...不行...

经过更多思考并在 OS 上进行了一些搜索后,我到达了我想要的位置。代码现在运行良好,而且更简单,我会把它留在这里以防万一:)

var x = 1;
var y = 0;

var o = 'Nothing here'
var house = 'House description';
var field = 'Field description';
var cave = 'Cave description';

var map = [
  [o, house, o],
  [o, field, cave],
  [o, o, o],
  [o, o, o]
];

while (y <= 3 && x <= 2 && x >= 0 && y >= 0) {
  switch (prompt(map[y][x] + ' - Your current position is: ' + 'x: ' + x + ' y: ' + y)) {
    case 'n':
      y--;
      break;
    case 's':
      y++;
      break;
    case 'e':
      x++;
      break;
    case 'w':
      x--;
      break;
    default:
      break;
  }
}