JS Alert何时收集物品?

时间:2019-03-28 15:55:52

标签: javascript html css

我创建了一个迷宫游戏,用户必须在迷宫中收集所有硬币,但是一旦用户收集了所有硬币,我试图使警报出现,而不是按照一般的特定顺序。该警报目前非常麻烦,无法正常工作,根据对这一问题的研究,我不确定哪里出了问题。我是JavaScript新手,因此对任何明显的错误深表歉意。

编辑 我被告知要直接重新回答我的问题-因此这不是重复的问题。我也有一个地图被拒绝但无法上传,因为它的代码太多。

此外,当我说工作不正常时,它会以随机计数的方式出现,就像应该收集到最后的奖励时一样。

var el = document.getElementById('game');

function drawWorld(){
    el.innerHTML = '';
    for(var y = 0; y < map.length ; y = y + 1) {
        for(var x = 0; x < map[y].length ; x = x + 1) {
        if (map[y][x] === 1) {
            el.innerHTML += "<div class='borders'></div>";
        } else if (map[y][x] === 2) {
            el.innerHTML += "<div class='reward'></div>";
        } else if (map[y][x] === 3) {
            el.innerHTML += "<div class='ground'></div>";
        } else if (map[y][x] === 5) {
            el.innerHTML += "<div class='character'></div>";
        } else if (map[y][x] === 4) {
                el.innerHTML += "<div class='ship'></div>";
            }
        }
        el.innerHTML += "<br>";
    }

    winGame();
}

function restartGame(){
    window.location.reload();
}

function winGame(){
if (!map[5].includes(2) && !map[2].includes(2) &&         
!map[3].includes(2) && !map[2].includes(2) && !map[4].includes(2) 
&&     !map[2].includes(2))
        alert("Well done!");
}


drawWorld();

document.onkeydown = function(event){
    if (event.keyCode === 37){
        if ( map[character.y][character.x-1] !== 1){
            map[character.y][character.x] = 3;
            character.x = character.x - 1;
            map[character.y][character.x] = 5;
            drawWorld();
        }
    } else if (event.keyCode === 38){
        if ( map[character.y-1][character.x] !== 1){
            map[character.y][character.x] = 3;
            character.y = character.y - 1;
            map[character.y][character.x] = 5;
            drawWorld();
        }
    } else if (event.keyCode === 39){
        if ( map[character.y][character.x+1] !== 1){
            map[character.y][character.x] = 3;
            character.x = character.x + 1;
            map[character.y][character.x] = 5;
            drawWorld();
        }
    } else if (event.keyCode === 40){
        if ( map[character.y+1][character.x] !== 1){
            map[character.y][character.x] = 3;
            character.y = character.y + 1;
            map[character.y][character.x] = 5;
            drawWorld();
        }
    }

    console.log(map)

}    

0 个答案:

没有答案