0

我想用 Javascript 创建一个 DOS 风格的游戏,但我无法正确显示地图。

这是 Windows 控制台中的数组:

char Map[1][10][20] = {
    "####################",
    "#                  #",
    "#                  #",
    "#                  #",
    "#                  #",
    "#                  #",
    "#                  #",
    "#                  #",
    "#                  #",
    "####################",
};

这就是我的javascript代码

var Map = {
    "#":["#","#","#","#","#","#","#","#","#","#","#","#","#","#"],
    "#":["#"," "," "," "," "," "," "," "," "," "," "," "," ","#"],
    "#":["#"," "," "," "," "," "," "," "," "," "," "," "," ","#"],
    "#":["#"," "," "," "," "," "," "," "," "," "," "," "," ","#"],
    "#":["#"," "," "," "," "," "," "," "," "," "," "," "," ","#"],
    "#":["#"," "," "," "," "," "," "," "," "," "," "," "," ","#"],
    "#":["#"," "," "," "," "," "," "," "," "," "," "," "," ","#"],
    "#":["#"," "," "," "," "," "," "," "," "," "," "," "," ","#"],
    "#":["#"," "," "," "," "," "," "," "," "," "," "," "," ","#"],
    "#":["#","#","#","#","#","#","#","#","#","#","#","#","#","#"]
};

本质上,我希望它完全按照它的出现输出第一个数组。我应该能够做其他所有事情。

4

2 回答 2

2

使用数组,而不是使用相同键定义每一行的对象(如果不是无效的,则相互覆盖1):

var Map = [
    ["#","#","#","#","#","#","#","#","#","#","#","#","#","#"],
    ["#"," "," "," "," "," "," "," "," "," "," "," "," ","#"],
    ["#"," "," "," "," "," "," "," "," "," "," "," "," ","#"],
    ["#"," "," "," "," "," "," "," "," "," "," "," "," ","#"],
    ["#"," "," "," "," "," "," "," "," "," "," "," "," ","#"],
    ["#"," "," "," "," "," "," "," "," "," "," "," "," ","#"],
    ["#"," "," "," "," "," "," "," "," "," "," "," "," ","#"],
    ["#"," "," "," "," "," "," "," "," "," "," "," "," ","#"],
    ["#"," "," "," "," "," "," "," "," "," "," "," "," ","#"],
    ["#","#","#","#","#","#","#","#","#","#","#","#","#","#"]
];

1:在严格模式下,你会得到一个语法错误

于 2013-09-19T03:27:16.100 回答
0

您的变量Map应该是一个数组数组,而不是一个对象。

另一种更直观的初始化方法:

// Splits each row by character to from a new array
var map = [
    "####################".split(''),
    "#                  #".split(''),
    "#                  #".split(''),
    "#                  #".split(''),
    "#                  #".split(''),
    "#                  #".split(''),
    "#                  #".split(''),
    "#                  #".split(''),
    "#                  #".split(''),
    "####################".split('')];

结果与@Bergi 的答案相同。

您在问题中提到想要“完全按照其出现的方式输出第一个数组”。如果您在浏览器中执行此操作,一种方法是使用pre标记:

HTML:

<pre id="map"></pre>

代码:

function updateMap() {
    // Iterates the array, joining each subarray with no separator,
    // then joining the result with a new line
    mapEle.innerText = map.map(function (val, idx, arr) {
        return val.join('');
    }).join('\n');
}

每次修改地图时,都可以调用updateMap().

例如:

var chars = ['|', '/', '\u2014', '\\'];
var counter = 0;
window.setInterval(function () {
    map[4][9] = chars[counter % chars.length];
    updateMap();
    counter++;
}, 200);

Demo Fiddle

看看gamedev.stackexchange 上的这个问题及其答案 - 如果您想专注于游戏而不是构建引擎的一部分,链接库可能会很有趣:

于 2013-09-19T04:16:23.157 回答