1

我正在尝试制作我的第一个吃豆人游戏,但我遇到了一堵我似乎无法自己打破的墙:(

这是关于如何在我的游戏中检测碰撞,所以吃豆人不能穿过障碍物/墙壁。我已经做到了,因此它不能使用以下代码超出屏幕:

    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;
    }

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

我上传了一张地图的照片: 我的游戏

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

我的 Board 类代码:

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;    

    }

}

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

4

1 回答 1

0

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

请注意,您需要自己处理幽灵的 AI:D

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

于 2015-01-25T14:15:36.160 回答