【发布时间】:2015-04-26 13:08:47
【问题描述】:
我正在制作一个简单的 Snake 游戏,除了 Key Listener 之外,一切似乎都在工作。根据我所看到的一切,它是正确编写的。
游戏出现了,但即使您按下箭头键,蛇也不会移动。
提前致谢!
这是主要的:
public class SnakeFrame extends JFrame implements KeyListener{
/**
*
*/
private static final long serialVersionUID = 1L;
private int direction;
private World w;
private WorldComponant wc;
GameLoopThread glt;
public SnakeFrame(){
setSize(420,440);
setTitle("Snake");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new BorderLayout());
w = new World();
wc = new WorldComponant(w);
glt = new GameLoopThread(wc);
direction = Direction.SOUTH;
c.add(wc);
}
public static void main(String[] args){
SnakeFrame s = new SnakeFrame();
s.setVisible(true);
}
public void keyPressed(KeyEvent e) {
switch(e.getKeyCode()){
case (KeyEvent.VK_UP):{
if(direction!=Direction.SOUTH){
direction = Direction.NORTH;
wc.move(direction);
}
break;
}
case (KeyEvent.VK_DOWN):{
if(direction!=Direction.NORTH){
direction = Direction.SOUTH;
wc.move(direction);
}
break;
}
case (KeyEvent.VK_RIGHT):{
if(direction!=Direction.WEST){
direction = Direction.EAST;
wc.move(direction);
}
break;
}
case (KeyEvent.VK_LEFT):{
if(direction!=Direction.EAST){
direction = Direction.WEST;
wc.move(direction);
}
break;
}
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
}
这是 GameLoopThread:
public class GameLoopThread extends Thread{
private WorldComponant sc;
public GameLoopThread(WorldComponant sc){
this.sc = sc;
}
public void run(){
while(true){
sc.repaint();
try{
Thread.sleep(100);
}catch(Exception e){
e.printStackTrace();
}
}
}
}
这是移动方法。它在 World Class 中,也从 WorldComponant 调用:
public void move(int direction) {
Point head = snake.peekFirst();
Point newPt = head;
switch(direction){
case (Direction.NORTH):{
newPt = new Point(head.x, head.y-1);
break;
}
case (Direction.SOUTH):{
newPt = new Point(head.x, head.y+1);
break;
}
case (Direction.WEST):{
newPt = new Point(head.x-1, head.y);
break;
}
case (Direction.EAST):{
newPt = new Point(head.x+1, head.y);
break;
}
}
snake.remove(snake.peekLast());
if(newPt.equals(food)){
Point addPt = (Point) newPt.clone();
switch(direction){
case (Direction.NORTH):{
newPt = new Point(head.x, head.y-1);
break;
}
case (Direction.SOUTH):{
newPt = new Point(head.x, head.y+1);
break;
}
case (Direction.WEST):{
newPt = new Point(head.x-1, head.y);
break;
}
case (Direction.EAST):{
newPt = new Point(head.x+1, head.y);
break;
}
}
score++;
snake.add(addPt);
placeFood();
}
else if(newPt.x < 0 || newPt.x > gridw - 1){
generateDefaultSnake();
JOptionPane.showMessageDialog(null, "YOU LOST! :(", "Loser!", JOptionPane.ERROR_MESSAGE);
return;
}
else if(newPt.y < 0 || newPt.y > gridh - 1){
generateDefaultSnake();
JOptionPane.showMessageDialog(null, "YOU LOST! :(", "Loser!", JOptionPane.ERROR_MESSAGE);
return;
}
else if(snake.contains(newPt)){
generateDefaultSnake();
JOptionPane.showMessageDialog(null, "YOU LOST! :(", "Loser!", JOptionPane.ERROR_MESSAGE);
return;
}
snake.add(newPt);
}
【问题讨论】:
-
您是否曾经将已实现的 keylistener 添加到您的 JFrame 中?另一方面,如果您的 WorldComponant 类恰好是
JComponent的子类,您应该考虑使用键绑定。 -
不要使用 KeyListener!有关使用
Key Bindings的更好方法,请参阅 Motion Using the Keyboard。
标签: java swing user-interface jframe keylistener