物体通过路径平滑移动

时间:2015-05-09 15:00:02

标签: javascript html

我正在编写Pacman克隆。我所完成的步骤包括地板,迷宫,以及pacman和pacman穿过墙壁路径的运动。

迷宫采用数组格式,墙壁表示为"x"。此外,机芯应与键盘上的wasd键一起使用。目前,物体的运动是平滑的,并且只有在我具有击中的良好时机时才能在路径中行走并且正确地改变方向。

我还阅读了这篇文章Relevant Post,并且我遵循了一些这些技巧,以便我可以存储按键。在我这样做之后,我不认为从先前的设计改变了一些东西。此外,当我改变到不允许的方向时,它会旋转到该方向并停止移动。我想像经典吃豆人游戏一样实现这个。以下是我的问题的代码。

这是 handleKeyDown 功能,我检查按键。

function handleKeyDown(event) {
        currentlyPressedKeys[event.keyCode] = true;

        if (String.fromCharCode(event.keyCode) == "A") {
            curr_key=temp;
            curr_key="A";
            memory_key = temp;
            pacRot = 0;

        }
        if (String.fromCharCode(event.keyCode) == "D") {
            curr_key=temp;
            curr_key="D";
            memory_key = temp;
            pacRot = 180;

        }
        if (String.fromCharCode(event.keyCode) == "W") {
            curr_key=temp;
            curr_key="W";
            memory_key = temp;
            pacRot = 270;

        }
        if (String.fromCharCode(event.keyCode) == "S") {
            curr_key=temp;
            curr_key="S";
            memory_key = temp;
            pacRot = 90;

        }

    }

现在功能 can_move(key)检查对象是否可以移动到方向

        function can_move(key){
        if (key == "W") {
                if (stage[Math.round(y) - 1][x] == "x") {
                    y = Math.round(y);
                    return false;
                } else {
                    return true;
                }
            }
            if (key == "A") {
                if (stage[y][Math.round(x) - 1] == "x") {
                    x = Math.round(x);
                    return false;
                } else {
                    return true;
                }
            }
            if (key == "S") {
                if (stage[Math.round(y) + 1][x] == "x") {
                    y = Math.round(y);
                    return false;
                } else {
                    return true;
                }
            }
            if (key == "D") {
                if (stage[y][Math.round(x) + 1] == "x") {
                    x = Math.round(x);
                    return false;
                } else {
                    return true;
                }
            }
    }

最后在tick()

之后在drawScene()内调用的主要移动函数 move_conditions
        function move_conditions(elapsed) {

        if(can_move(curr_key)==true){
            if(curr_key=="W"){
                ySpeed = 5;
                xSpeed = 0;
                y -= (ySpeed * 25) / 1000.0;
            x = Math.round(x);
            }
            if(curr_key=="A"){
                 ySpeed = 0;
                xSpeed = 5;
                x -= (xSpeed * 25) / 1000.0;
            y = Math.round(y);
            }
            if(curr_key=="S"){
                ySpeed = 5;
                xSpeed = 0;
                y += (ySpeed * 25) / 1000.0;
            x = Math.round(x);
            }
            if(curr_key=="D"){
                ySpeed = 0;
                xSpeed = 5;
                x += (xSpeed * 25) / 1000.0;
            y = Math.round(y);
            }

        }else if(can_move(memory_key)==true){
            if(memory_key=="W"){
                ySpeed = 5;
                xSpeed = 0;
                y -= (ySpeed * 25) / 1000.0;
            x = Math.round(x);
            }
            if(memory_key=="A"){
                 ySpeed = 0;
                xSpeed = 5;
                x -= (xSpeed * 25) / 1000.0;
            y = Math.round(y);
            }
            if(memory_key=="S"){
                ySpeed = 5;
                xSpeed = 0;
                y += (ySpeed * 25) / 1000.0;
            x = Math.round(x);
            }
            if(memory_key=="D"){
                ySpeed = 0;
                xSpeed = 5;
                x += (xSpeed * 25) / 1000.0;
            y = Math.round(y);
            }
        }

0 个答案:

没有答案
相关问题