我正在制作一个蛇游戏,它可以在我的电脑上正常工作,但不适用于任何学校的电脑。它编译,我可以运行它,但我的按键事件不起作用,所以我无法控制蛇,因为键不起作用。
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.Timer;
public class Snake implements ActionListener, KeyListener
{
public static Snake snake;
public JFrame jframe;
public RenderPanel renderPanel;
public Timer timer = new Timer(20, this);
public ArrayList<Point> snakeParts = new ArrayList<Point>();
public static final int UP = 0, DOWN = 1, LEFT = 2, RIGHT = 3, SCALE = 10;
public int ticks = 0, direction = DOWN, score, tailLength = 10, time;
public Point head, mouse;
public Random random;
public boolean over = false, paused;
public Dimension dim;
public Snake()
{
dim = Toolkit.getDefaultToolkit().getScreenSize();
jframe = new JFrame("TechnoSnake");
jframe.setVisible(true);
jframe.setSize(805, 700);
jframe.setResizable(false);
jframe.setLocation(dim.width / 2 - jframe.getWidth() / 2, dim.height / 2 - jframe.getHeight() / 2);
jframe.add(renderPanel = new RenderPanel());
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.addKeyListener(this);
startGame();
}
public void startGame()
{
over = false;
paused = false;
time = 0;
score = 0;
tailLength = 1;
ticks = 0;
direction = DOWN;
head = new Point(0, -1);
random = new Random();
snakeParts.clear();
mouse = new Point(random.nextInt(79), random.nextInt(66));
timer.start();
}
public void actionPerformed(ActionEvent arg0)
{
renderPanel.repaint();
ticks++;
if (ticks % 2 == 0 && head != null && !over && !paused)
{
time++;
snakeParts.add(new Point(head.x, head.y));
if (direction == UP)
if (head.y - 1 >= 0 && noTailAt(head.x, head.y - 1))
head = new Point(head.x, head.y - 1);
else
over = true;
if (direction == DOWN)
if (head.y + 1 < 67 && noTailAt(head.x, head.y + 1))
head = new Point(head.x, head.y + 1);
else
over = true;
if (direction == LEFT)
if (head.x - 1 >= 0 && noTailAt(head.x - 1, head.y))
head = new Point(head.x - 1, head.y);
else
over = true;
if (direction == RIGHT)
if (head.x + 1 < 80 && noTailAt(head.x + 1, head.y))
head = new Point(head.x + 1, head.y);
else
over = true;
if (snakeParts.size() > tailLength)
snakeParts.remove(0);
if (mouse != null)
{
if (head.equals(mouse))
{
score += 10;
tailLength++;
mouse.setLocation(random.nextInt(79), random.nextInt(66));
}
}
}
}
public boolean noTailAt(int x, int y)
{
for (Point point : snakeParts)
{
if (point.equals(new Point(x, y)))
{
return false;
}
}
return true;
}
public static void main(String[] args)
{
snake = new Snake();
}
public void keyPressed(KeyEvent e)
{
int i = e.getKeyCode();
if ((i == KeyEvent.VK_A || i == KeyEvent.VK_LEFT) && direction != RIGHT)
direction = LEFT;
if ((i == KeyEvent.VK_D || i == KeyEvent.VK_RIGHT) && direction != LEFT)
direction = RIGHT;
if ((i == KeyEvent.VK_W || i == KeyEvent.VK_UP) && direction != DOWN)
direction = UP;
if ((i == KeyEvent.VK_S || i == KeyEvent.VK_DOWN) && direction != UP)
direction = DOWN;
if (i == KeyEvent.VK_SPACE)
if (over)
startGame();
else
paused = !paused;
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
}
}
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import javax.swing.JPanel;
public class RenderPanel extends JPanel
{
//public static Color green = new Color(1666073);
public static Color brown = new Color(6697728);
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
//g.setColor(green);
g.setColor(Color.BLACK);
g.fillRect(0, 0, 800, 700);
Snake snake = Snake.snake;
g.setColor(Color.BLUE);
for (Point point : snake.snakeParts)
{
g.fillRect(point.x * Snake.SCALE, point.y * Snake.SCALE, Snake.SCALE, Snake.SCALE);
}
g.fillRect(snake.head.x * Snake.SCALE, snake.head.y * Snake.SCALE, Snake.SCALE, Snake.SCALE);
g.setColor(Color.RED);
g.fillRect(snake.mouse.x * Snake.SCALE, snake.mouse.y * Snake.SCALE, Snake.SCALE, Snake.SCALE);
String string = "Score: " + snake.score + ", Length: " + snake.tailLength + ", Time: " + snake.time / 20;
g.setColor(Color.white);
g.drawString(string, (int) (getWidth() / 2 - string.length() * 2.5f), 10);
string = "Game Over!";
if (snake.over)
g.drawString(string, (int) (getWidth() / 2 - string.length() * 2.5f), (int) snake.dim.getHeight() / 4);
string = "Paused!";
if (snake.paused && !snake.over)
g.drawString(string, (int) (getWidth() / 2 - string.length() * 2.5f), (int) snake.dim.getHeight() / 4);
}
}
答案 0 :(得分:1)
你真的不应该向KeyListener
添加JFrame
。 JFrame
由框架JRootPane
组成,其中包含内容窗格(现在是您的RenderPanel
),并且可能还有一个玻璃窗格。所有这些都可以将焦点从框架中移开,这意味着它永远无法响应关键事件
KeyListener
只会在注册的组件具有焦点并且具有焦点时才会引发关键事件。
请尝试使用key bindings。它们可以更好地控制生成关键事件所需的焦点水平