吃豆子克隆,当按下按钮的方向无法进入时,如何保持pacman移动

时间:2013-05-29 02:11:43

标签: java libgdx collision-detection pacman

对不起伙计们,我不知道标题是否是解释它的最佳方式。我正在使用libgdx在java中创建pacman克隆。我已经在瓷砖上渲染了地图并进行了碰撞检测。唯一的问题是,pacman无论如何都会继续前进。当他向右走,你按下或向上按下时,他就会停下来。我尝试了很多不同的东西,似乎无法让它正常工作。

这是我的pacman播放器类

package com.psillidev.pacman1;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Rectangle;

public class player {
    private static int height = 16;
    private static int width = 16;

    private int playerX , playerY;
    private int direction;
    private Texture playerTexture;
    private boolean isAlive; 


    public player(int startX,int startY) {
        playerTexture = new Texture(Gdx.files.internal("data/pacman.png"));
        playerX = startX;
        playerY = startY;
    }

    public void move(Map map) {
        if (direction == 0) {
            if (isNoTile(map,direction,getX(),getY())) {
                setY(getY() + (int)(Gdx.graphics.getDeltaTime() + 2));
            }
        }

        if (direction ==1) {
            if (isNoTile(map,direction,getX(),getY())) {
                setX(getX() + (int)(Gdx.graphics.getDeltaTime() + 2));
            }
        }

        if (direction ==2) {
            if (isNoTile(map,direction,getX(),getY())) {
                setY(getY() - (int)(Gdx.graphics.getDeltaTime() + 2));
            }
        }

        if (direction ==3) {
            if (isNoTile(map,direction,getX(),getY())) {
                setX(getX() - (int)(Gdx.graphics.getDeltaTime() + 2));
            }
        }

    }

    private boolean isNoTile(Map map, int dir, int translated_x, int translated_y) {
        System.out.println("X: " + translated_x + " Y: " + translated_y);
        for (Rectangle r : map.getBlockedTiles()) {

            switch (dir) {
            case 0:
                Rectangle up = new Rectangle(translated_x,translated_y + 2 , 16, 16);

                if (up.overlaps(r)) {

                    return false;

                }
                break;

            case 1:
                Rectangle right = new Rectangle(translated_x+2,translated_y, 16, 16);
                if (right.overlaps(r)) {
                    return false;
                }
                break;

            case 2:
                Rectangle down = new Rectangle(translated_x,translated_y - 2, 16, 16);
                if (down.overlaps(r)) {
                    return false;
                }
                break;

            case 3:
                Rectangle left = new Rectangle(translated_x-2,translated_y, 16, 16);
                if (left.overlaps(r)) {
                    return false;
                }
                break;

            default:
                break;
            }
        }

        return true;
    }

    public void render(SpriteBatch batch) {
        batch.draw(playerTexture, playerX, playerY);
    }

    public int getX() {
        return playerX;
    }

    public void setX(int x) {
        playerX = x;
    }

    public int getY() {
        return playerY;
    }

    public void setY(int y) {
        playerY = y;
    }

    public int getDirection() {
        return direction;
    }

    public void setDirection(int dir) {

        direction = dir;
    }

    public Rectangle getRect() {
        return new Rectangle(playerX,playerY,width,height);
    }
}

3 个答案:

答案 0 :(得分:1)

我无法回答您提供的代码,但我的建议是您需要对KeyListener课程进行调整。我假设你有KeyListener正在监听用户按箭头键。按下某个键时,您是否正在执行Pacman.setDirection()

之类的操作

您实际需要做的是在调用setDirection()之前检测方向是否有效。所以KeyListener的代码就是这样......

  1. 确定用户是按下,向下,向左还是向右
  2. 根据Pacman的当前位置检查游戏网格/地图以查看方向是否有效
  3. 如果方向有效,请致电setDirection()。如果没有,请不要拨打任何电话,Pacman将继续朝当前方向发展。

答案 1 :(得分:0)

这真的不应该由玩家自己处理,但可能由运动经理处理,这样可以更容易地测试/扩展游戏。在你的代码中,你只允许他按照输入决定的方向移动到空白区域,所以如果你向右走,如果UP是一堵墙,则无论右边是什么,他都会自动停止移动。

你可以做的是输入:如果要求向上移动,检查向上是否为空,然后向上移动,如果向上不是空的,则继续沿着他之前移动的方向移动。其他方向相同。

答案 2 :(得分:0)

您的setDirection方法不正确,需要

public void setDirection(int dir) {
    // only change direction if we can.
    if (isValidDirection(dir)){
        direction = dir;
    }
}