迷宫2D java游戏中的碰撞检测

时间:2015-01-25 13:48:26

标签: java collision maze pacman

我正在尝试制作我的第一个Pacman游戏,但我遇到的墙壁似乎无法自行粉碎:(

这是关于如何在我的游戏中检测碰撞,因此pacman无法通过障碍/墙壁。我已经做到这一点,所以它不能使用这段代码离开屏幕:

    if (pacman_x < 27) {
        pacman_velX = 0;
        pacman_x = 27;
    }
    if (pacman_y < 27) {
        pacman_velY = 0;
        pacman_y = 27;
    }
    if (pacman_x > 621) {
        pacman_velX = 0;
        pacman_x = 621;
    }
    if (pacman_y > 513) {
        pacman_velY = 0;
        pacman_y = 513;
    }

,但是如果我在屏幕中间的电路板上有一个矩形,我不知道如何编程,所以它会在墙前停止。

我上传了地图的照片: MY game

如你所见,我需要阻止我的pacman移动到竞技场内的墙上(左边顶部的矩形)

我的董事会成员代码:

public class Board extends JPanel implements ActionListener, KeyListener {

private Timer timer;
private Map m;
private Pacman p;
int pacman_x = 27, pacman_y = 27, pacman_velX = 0, pacman_velY = 0; 

public Board() 
{
    m = new Map();
    p = new Pacman();
    timer = new Timer(10, this);
    timer.start();
    addKeyListener(this);
    setFocusable(true);
    setFocusTraversalKeysEnabled(false); // So we can't use shift 
}

public void actionPerformed(ActionEvent e) {
    pacman_x = pacman_x + pacman_velX;
    pacman_y = pacman_y + pacman_velY;
    repaint();          

    if (pacman_x < 27) {
        pacman_velX = 0;
        pacman_x = 27;
    }
    if (pacman_y < 27) {
        pacman_velY = 0;
        pacman_y = 27;
    }
    if (pacman_x > 621) {
        pacman_velX = 0;
        pacman_x = 621;
    }
    if (pacman_y > 513) {
        pacman_velY = 0;
        pacman_y = 513;
    }

    if (pacman_x >= 150 && pacman_y >= 27) {
        pacman_velX = 0;
        pacman_x = 27;
    }

}

public void paint(Graphics g) {
    super.paint(g);

    Graphics2D g2d = (Graphics2D) g;

    for(int y = 0; y < 21; y++) {
        for (int x = 0; x < 25; x++) {
            if (m.getMap(x, y).equals("b")) {
                g.drawImage(m.getBarrier(), x * 27, y * 27, null);
            }
            if (m.getMap(x, y).equals("s")) {
                g.drawImage(m.getSpace(), x * 27, y * 27, null);
            }
        }

    }

    // Place pacman on board        
    g.drawImage(p.getPacman(), pacman_x , pacman_y, null);  

    // Create Rectangles barriers
    Rectangle r1 = new Rectangle(150, 27, 27, 27);

    g2d.setColor(new Color(63, 72, 204, 250));      
    g.fillRect(r1.x, r1.y, 27, 27);

    // Sets Color on lives
    int lives = 3;
    g2d.setColor(new Color(255, 0, 0, 250));
    g2d.drawString("Lives left: " + lives, 20, 20);

    // Sets Color on Scoreboard text
    int point = 0;      
    g2d.setColor(new Color(255, 0, 0, 250));
    g2d.drawString("Score: " + point, 20, 550);
}

@Override
public void keyPressed(KeyEvent e) {

    int pacman_direction = e.getKeyCode();

    if (pacman_direction == KeyEvent.VK_LEFT) {

        pacman_velX = -3;
        pacman_velY = 0;

    }       

    if (pacman_direction == KeyEvent.VK_UP) {

        pacman_velX = 0;
        pacman_velY = -3;           

    }

    if (pacman_direction == KeyEvent.VK_RIGHT) {

        pacman_velX = 3;
        pacman_velY = 0;

    }

    if (pacman_direction == KeyEvent.VK_DOWN) {

        pacman_velX = 0;
        pacman_velY = 3;    

    }

}

希望有人可以告诉我该怎么做..似乎无法在互联网上找到一个简单的示例教程来解释我想要它做的事情!

1 个答案:

答案 0 :(得分:0)

这并不完全是关于在java中制作pac-man,但它是关于2d平台游戏,它在2d空间中具有所有碰撞映射以及如何创建和添加诸如玩家和包含动画的敌人之类的对象,你应该发现它总体上很有用:https://www.youtube.com/watch?v=9dzhgsVaiSo&index=1&list=PL-2t7SM0vDfcIedoMIghzzgQqZq45jYGv

注意你需要自己处理鬼魂的AI:D

免责声明我没有制作这些视频我发现它们非常有用。