0

尝试编写 java pacman.i 使用 bufferedimage 来存储迷宫。为了检查 pacman 与墙壁的碰撞,我使用了 2d int 数组,值 0 表示空白空间 & pacman 可以在上面移动,1 是 wall.pacman 对象应该在按键时间,按键释放时停止。但是 pacman 没有正确移动,它有时也会在墙壁方块上移动,有时它会移动然后完全停止,即使它在空方块中也是如此。不知道我哪里出错了。我使用了以下课程。请有人帮忙。

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

1 回答 1

0

在此处查看您的变量:

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

你在哪里重置mapx以及mapy在事件之间?我没有看到你在任何地方这样做。那么后续事件之间mapx会发生什么?mapy例如,如果您向左移动并撞到墙壁,然后向上移动,您mapx现在仍然卡在墙上。

不要使用字段,因为您没有在其他任何地方使用它们。只需在方法中制作变量即可。你有一个类似的潜在问题,dx如果在改变方向之前撞到墙dy,它们只会重置为 0 。

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;
}
于 2013-11-17T15:43:41.327 回答