1
votes

I'm trying to make my first Pacman game but i have met a wall that i can't seem to smash on my own :(

It's about how to detect collision in my game, so the pacman can't go through barriers / walls. I have made it so it can't go outside of the screen with this code:

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

, but if i have a rectangle on the board in middle of the screen, i don't know how to program it so it will stop just before a wall.

I have uploaded a foto of the map: MY game

I need to stop my pacman to move onto the wall inside the arena as you see (the rectangle on top to the left)

My code of the Board class:

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;    

    }

}

Hope someone can tell me what to do.. can't seem to find an easy example tutorial on the internet that explain the thing that i want it to do!

1
Check you this framework for collision checking. See hereMurat Karagöz
So you basically want to know how to do collision detection on any wall?Hayden
You can create a Shape defining all the walls. The method Shape.intersects( Rectangle2D r ) will tell you when pacman hits a wall, using pacman's bounding box as the rectangle. Back up to the last good position, set speed to 0, etc. - Area provides even more features if Shape isn't enough.laune

1 Answers

0
votes

This isn't exactly about making pac-man in java but it is about a 2d platformer which has all the collision mapping in 2d space and how to create and add objects such as a player and enemies with animations included in it, you should find it useful on the whole: https://www.youtube.com/watch?v=9dzhgsVaiSo&index=1&list=PL-2t7SM0vDfcIedoMIghzzgQqZq45jYGv

Note you will need to handle the AI for the ghosts on your own :D

disclaimer I did not make these video's I just found them very useful.