0
votes

trying to code java pacman.i used bufferedimage to store the maze.To check pacman's collision with walls,i used 2d int array,value 0 means empty space & pacman can move over it,1 is wall.pacman object should move 1 square at a time on keypress,stop on keyrelease. But pacman is not moving correctly,it sometimes moves over the wall square too,sometimes it moves and then stops completely even when it is in empty square . not sure where iam going wrong. I used the below classes. Please could somebody help.

public class Map extends JPanel implements Runnable{

private int TILE_W=15; //divide map into 15x15 tiles
private int TILE_H=20; //there will be 20 tiles along width and 15 along height,total of 300 tiles

private Image tile; // 15x15 png image

private BufferedImage img; //stores maze image

private long DELAY=50;

private Pacman pacman;

private int[][] mapdata={{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 
                         {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                         {1,0,0,1,1,1,1,0,1,0,1,1,1,1,1,1,1,1,1,1},
                         {1,0,0,1,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,1},
                         {1,0,0,1,0,0,1,0,1,0,1,0,1,1,1,1,1,1,0,1},
                         {1,0,0,1,1,1,1,0,1,0,1,0,1,0,0,0,0,1,0,1},
                         {1,0,0,0,0,0,0,0,1,0,1,0,1,0,0,0,0,1,0,1},
                         {1,0,0,1,1,1,1,0,1,0,1,0,1,1,1,1,1,1,0,1},
                         {1,0,0,1,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,1},
                         {1,0,0,1,0,0,1,0,1,0,1,0,1,1,1,1,1,1,0,1},
                         {1,0,0,1,0,0,1,0,1,0,1,0,1,0,0,0,0,1,0,1},
                         {1,0,0,1,0,0,1,0,1,0,1,0,1,0,0,0,0,1,0,1},
                         {1,0,0,1,1,1,1,0,1,0,1,0,1,1,1,1,1,1,0,1},
                         {1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1},
                         {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
                        };

public Map(){
  ImageIcon ict=new ImageIcon(this.getClass().getResource("tile.png"));
  tile=ict.getImage();
  addKeyListener(new KbListener());
  setFocusable(true);
  setDoubleBuffered(true);
  pacman=new Pacman(mapdata);
  img=new BufferedImage(350,350,BufferedImage.TYPE_INT_ARGB);
  Graphics2D g2D=(Graphics2D) img.getGraphics();
  for(int x=0;x<TILE_W;x++)
    for(int y=0;y<TILE_H;y++){
            if(mapdata[x][y]==1)
                g2D.drawImage(tile,y*15,x*15,this);
    }
  Thread t=new Thread(this);
  t.start();
}

@Override
public void paintComponent(Graphics g){
    super.paintComponent(g);
    Graphics2D g2D=(Graphics2D) g;
    g2D.drawImage(img,0,0,this);
    g2D.drawImage(pacman.getImage(),pacman.getX(),pacman.getY(),this);
    Toolkit.getDefaultToolkit().sync();
    g.dispose();
}

public void run() {
    // TODO Auto-generated method stub
    while(true){
     repaint();
     pacman.move();
     try {
        Thread.sleep(DELAY);
     } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
     }
   }
}

private class KbListener extends KeyAdapter{
    public void keyReleased(KeyEvent e){
        pacman.keyReleased(e);
    }

    public void keyPressed(KeyEvent e){
        pacman.keyPressed(e);
    }
}

}


public class Pacman {
private Image pacman;
private int x,y;
private int dx,dy;

private int[][] mapdata;
private int mapx,mapy;

public Pacman(int[][] mapdata){
    ImageIcon icp=new ImageIcon(this.getClass().getResource("pacman.png"));
    pacman=icp.getImage();
    x=30;
    y=15;
    dx=0;
    dy=0;
    mapx=2;
    mapy=1;
    this.mapdata=mapdata;
}

public void move(){
    x+=dx;
    y+=dy;  
}

public Image getImage(){
    return pacman;
}

public int getX(){
    return x;
}

public int getY(){
    return y;
}

public void keyPressed(KeyEvent e){
    int k=e.getKeyCode();
    if(k==KeyEvent.VK_LEFT){
        mapx=(x-15)/15;
        if(mapdata[mapx][mapy]==1)
            dx=0;
        else dx=-15; 
    }
    if(k==KeyEvent.VK_RIGHT){
        mapx=(x+15)/15;
        if(mapdata[mapx][mapy]==1)
            dx=0;
        else dx=15;
    }
    if(k==KeyEvent.VK_UP){
        mapy=(y-15)/15;
        if(mapdata[mapx][mapy]==1)
            dy=0;
        else dy=-15;
    }
    if(k==KeyEvent.VK_DOWN){
        mapy=(y+15)/15;
        if(mapdata[mapx][mapy]==1)
            dy=0;
        else dy=15;
    }
}

public void keyReleased(KeyEvent e){
    int k=e.getKeyCode();
    if(k==KeyEvent.VK_LEFT)
        dx=0;
    if(k==KeyEvent.VK_RIGHT)
        dx=0;
    if(k==KeyEvent.VK_UP)
        dy=0;
    if(k==KeyEvent.VK_DOWN)
        dy=0;
}
}
1
1) For better help sooner, post an SSCCE. 2) Your 'question' contains no '?'. Do you have a question, if so, what is it? 3) For Swing, typically use key bindings over the AWT based, lower level, KeyListener. See How to Use Key Bindings for details on how to use them. 4) See this answer for one form of collision detection that does work.Andrew Thompson

1 Answers

0
votes

Take a look at your variables here:

if(k==KeyEvent.VK_LEFT){
    mapx=(x-15)/15;
    if(mapdata[mapx][mapy]==1)
        dx=0;
    else dx=-15; 
}

Where do you reset mapx and mapy between events? I don't see you do it anywhere. So what happens to mapx and mapy between subsequent events? For example if you move left and hit a wall, then move up, your mapx is now still stuck in the wall.

Don't use fields because you aren't using them anywhere else. Just make the variables in the method. You have a similar potential problem with dx and dy where they only get reset to 0 if you hit a wall before changing directions.

int mapx = x / 15;
int mapy = y / 15;

if (k == KeyEvent.VK_LEFT) {
    mapx--;

    if (mapx < 0 ||
            mapdata[mapx][mapy] == 1)
        dx = 0;
    else dx =- 15;

    dy = 0;

} else if (k == KeyEvent.VK_RIGHT) {
    mapx++;

    if (mapx > mapdata.length - 1 ||
            mapdata[mapx][mapy] == 1)
        dx = 0;
    else dx = 15;

    dy = 0;

} else if (k == KeyEvent.VK_UP) {
    mapy--;

    if (mapy < 0 ||
            mapdata[mapx][mapy] == 1)
        dy = 0;
    else dy =- 15;

    dx = 0;

} else if (k == KeyEvent.VK_DOWN) {
    mapy++;

    if (mapy > mapdata[mapx].length - 1 ||
            mapdata[mapx][mapy] == 1)
        dy = 0;
    else dy = 15;

    dx = 0;
}