我现在正在研究的 pacman 运动遇到了一个小问题。
我似乎无法找到一种方法使该运动类似于起源的 pacman 运动。为了解释我希望它如何移动,我上传了这张照片。
就目前而言,我已经在黄色矩形和蓝色墙壁之间进行了碰撞。问题是当我如图所示向左移动并单击向上箭头时,它不应该停止,而是继续移动,然后在有空闲空间时向上移动。
现在,如果单击向上箭头,黄色矩形将停止,如下所示:
已上传包含我的动作的 pacman 类,以及检测到碰撞时来自我的板类的代码。
吃豆子类
public class Pacman {
private String pacmanup = "pacmanup.png";
private String pacmandown = "pacmandown.png";
private String pacmanleft = "pacmanleft.png";
private String pacmanright = "pacmanright.png";
private int dx;
private int dy;
private int x;
private int y;
private int width;
private int height;
private boolean visible;
private Image imageup;
private Image imagedown;
private Image imageleft;
private Image imageright;
public Pacman() {
Thread thread = new Thread();
thread.start();
ImageIcon i1 = new ImageIcon(this.getClass().getResource(pacmanup));
imageup = i1.getImage();
ImageIcon i2 = new ImageIcon(this.getClass().getResource(pacmandown));
imagedown = i2.getImage();
ImageIcon i3 = new ImageIcon(this.getClass().getResource(pacmanleft));
imageleft = i3.getImage();
ImageIcon i4 = new ImageIcon(this.getClass().getResource(pacmanright));
imageright = i4.getImage();
width = imageup.getWidth(null);
height = imageup.getHeight(null);
visible = true;
x = 27;
y = 27;
}
public int getDx() {
return dx;
}
public void setDx(int dx) {
this.dx = dx;
}
public int getDy() {
return dy;
}
public void setDy(int dy) {
this.dy = dy;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public Image getImageup() {
return imageup;
}
public Image getImagedown() {
return imagedown;
}
public Image getImageleft() {
return imageleft;
}
public Image getImageright() {
return imageright;
}
public void setVisible(boolean visible) {
this.visible = visible;
}
public boolean isVisible() {
return visible;
}
public Rectangle getBounds() {
return new Rectangle(x, y, width, height);
}
public Rectangle getOffsetBounds() {
return new Rectangle(x + dx, y + dy, width, height);
}
public void move() {
x += dx;
y += dy;
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) {
dx = -1;
dy = 0;
}
if (key == KeyEvent.VK_RIGHT) {
dx = 1;
dy = 0;
}
if (key == KeyEvent.VK_UP) {
dx = 0;
dy = -1;
}
if (key == KeyEvent.VK_DOWN) {
dx = 0;
dy = 1;
}
}
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) {
}
if (key == KeyEvent.VK_RIGHT) {
}
if (key == KeyEvent.VK_UP) {
}
if (key == KeyEvent.VK_DOWN) {
}
}
Board 类的代码
Rectangle r5 = pacman.getOffsetBounds();
for (int j = 0; j<barriers.size(); j++) {
Barrier b = (Barrier) barriers.get(j);
Rectangle r4 = b.getBounds();
if (r5.intersects(r4)) {
pacman.setDx(0);
pacman.setDy(0);
break;
}
}
pacman.move();
}
在我的棋盘类中创建了一个名为 direction 的字符串,用于检测黄色矩形移动的方向。
“左”“上”“右”或“下”
已经尝试编写一些代码,但无法正常工作。这是我尝试过的:
// moving to the left and tried go up
else if (pacman.getDx() == 0 && pacman.getDy() == -1 && direction.equals("left")) {
pacman.setDy(0);
pacman.setDx(-1);
break;
好吧,当它向左移动并单击向上时,它会继续向左移动,但它不会像它应该做的那样上升到可用空间:)
是键适配器/按下的键吗?当您单击向上时,也许按下的键未激活或其他什么?