我有一个Keylistener
游戏课程,我正在制作一个有问题的游戏。我似乎无法通过按键使 pacman 移动。似乎该Keylistener
方法从未被调用过。我对 Java 和 GUI 界面比较陌生,所以如果我的编码有点生疏,请原谅我。
import java.awt.event.*;
public class PacRunner implements ActionListener, KeyListener
{
private static Grid gr;
public static void main (String[] args)
{
gr = new Grid();
gr.addGhost(new Location(4,11));
gr.movePac(new Location(6,11));
gr.show();
}
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_UP) gr.move(Location.UP);
else if(e.getKeyCode() == KeyEvent.VK_DOWN) gr.move(Location.DOWN);
else if(e.getKeyCode() == KeyEvent.VK_LEFT) gr.move(Location.LEFT);
else if(e.getKeyCode() == KeyEvent.VK_RIGHT) gr.move(Location.RIGHT);
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
@Override
public void actionPerformed(ActionEvent e) {}
}