gpt4 book ai didi

java - 贪吃蛇游戏。处理 keyPressed 方法

转载 作者:行者123 更新时间:2023-11-30 03:18:38 25 4
gpt4 key购买 nike

我让蛇跑下来了,现在我尝试让 keypressed() 方法起作用。我不认为它是从键盘上听的。我的想法是,例如,如果按下向下键,则变量方向设置为 1,并且 switch case 语句将处理该问题。我怀疑 switch case 没有读取方向变量。

我的主要类(class):

package com.bin.snake;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class Game extends JPanel{

boolean playingSnake = true;
Snake snake = new Snake();

public Game() {
addKeyListener(new KeyListener() {

@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub

}

@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub

}

@Override
public void keyPressed(KeyEvent e) {

snake.keyPressed(e);
}
});
setFocusable(true);
}

public void paint(Graphics g) {// inherit paint method of JPanel class with
// parameter g type Graphics
// parameter g is the object to paint things
super.paint(g); // erase latest image
Graphics2D g2d = (Graphics2D) g; // casting g to Graphics2D type
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);// rendering
// image
snake.paint(g2d);

}

public static void main(String[] args) {
JFrame frame = new JFrame("Snake!");
frame.setVisible(true);
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Game game = new Game();
frame.add(game);
while (true) {
game.updateGame();
game.repaint();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

public void updateGame() {
snake.moveSnake();
}

}

我的蛇类:

package com.bin.snake;

import java.awt.Graphics2D;
import java.awt.event.KeyEvent;

public class Snake {

private Game game;
int iSnakeLength = 10;
int direction = 1;
final int SIDE = 13;

int[] snakeY = new int[2000];
int[] snakeX = new int[2000];
int xs = 0;
int ys = 0;

public void moveSnake() {

switch (direction) {
case 0:// up
snakeY[0] -=1.5;
break;
case 1:// down
snakeY[0] += 1.5;
break;
case 2:// right
snakeX[0] += 1.5;
break;
case 3:// left
snakeX[0] -=1.5;
break;
}

for (int i = iSnakeLength; i > 0; i--) {
snakeX[i] = snakeX[i - 1];
snakeY[i] = snakeY[i - 1];

}
}

public void paint(Graphics2D g) {
g.fillRect(snakeX[0], snakeY[0], SIDE, SIDE);
for (int i = 0; i < iSnakeLength; i++) {
g.fillRect(snakeX[i + 1], snakeY[i + 1], SIDE, SIDE);
}

}

public void keyPressed(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_DOWN){
direction = 1;
}
if(e.getKeyCode() == KeyEvent.VK_UP){
direction = 0;
}
if(e.getKeyCode() == KeyEvent.VK_LEFT){
direction = 3;
}
if(e.getKeyCode() == KeyEvent.VK_RIGHT){
direction = 2;
}
}

}

最佳答案

I don't think it's listening from the keyboard. My idea

KeyEvents 仅分派(dispatch)给具有焦点的组件。 JPanel 默认情况下不可聚焦。

要使面板可聚焦,您可以使用:

panel.setFocusable( true );

其他评论:

  1. 自定义绘制是通过重写paintComponent(...)方法而不是paint()方法来完成的。阅读 Custom Painting 上的 Swing 教程了解更多信息和示例。

  2. 您不应该使用 KeyListener。 Swing 被设计为与Key Bindings 一起使用。阅读 How to Use Key Bindings 上的 Swing 教程。您还可以查看Motion Using the Keyboard其中包含使用按键绑定(bind)移动组件的工作示例。

  3. 对于动画,您应该使用 Swing Timer 来安排动画。从我已经提供的教程链接中查看目录。有一节介绍如何使用 Swing Timers

关于java - 贪吃蛇游戏。处理 keyPressed 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31899740/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com