试图编写java pacman.i使用bufferedimage来存储迷宫。要检查pacman与墙壁的碰撞,我使用2d int数组,值0表示空白空间& pacman可以移过它,1是wall.pacman对象应该在按键上一次移动1个方块,停止在keyrelease上。但是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;
}
}
答案 0 :(得分: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;
}