写一个基于java的贪吃蛇游戏的代码
时间: 2024-04-30 11:19:22 浏览: 150
以下是一个基于 Java 的简单贪吃蛇游戏的代码:
```java
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.LinkedList;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class SnakeGame extends JPanel implements Runnable, KeyListener {
private static final long serialVersionUID = 1L;
private static final int WIDTH = 500;
private static final int HEIGHT = 500;
private Thread thread;
private boolean isRunning;
private LinkedList<Point> snake;
private int direction;
private Point fruit;
public SnakeGame() {
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setFocusable(true);
addKeyListener(this);
snake = new LinkedList<Point>();
snake.add(new Point(0, 0));
direction = KeyEvent.VK_RIGHT;
fruit = new Point(0, 0);
placeFruit();
isRunning = true;
thread = new Thread(this);
thread.start();
}
public void run() {
while (isRunning) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
update();
repaint();
}
}
private void update() {
Point head = snake.peekFirst();
Point newPoint = head;
switch (direction) {
case KeyEvent.VK_LEFT:
newPoint = new Point(head.x - 1, head.y);
break;
case KeyEvent.VK_RIGHT:
newPoint = new Point(head.x + 1, head.y);
break;
case KeyEvent.VK_UP:
newPoint = new Point(head.x, head.y - 1);
break;
case KeyEvent.VK_DOWN:
newPoint = new Point(head.x, head.y + 1);
break;
}
if (newPoint.equals(fruit)) {
snake.addFirst(newPoint);
placeFruit();
} else {
snake.removeLast();
if (snake.contains(newPoint)) {
isRunning = false;
}
snake.addFirst(newPoint);
}
if (head.x < 0 || head.x >= WIDTH || head.y < 0 || head.y >= HEIGHT) {
isRunning = false;
}
}
private void placeFruit() {
Random rand = new Random();
int x = rand.nextInt(WIDTH);
int y = rand.nextInt(HEIGHT);
fruit.setLocation(x, y);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.GREEN);
for (Point p : snake) {
g.fillRect(p.x, p.y, 1, 1);
}
g.setColor(Color.RED);
g.fillRect(fruit.x, fruit.y, 1, 1);
}
public void keyPressed(KeyEvent e) {
int newDirection = e.getKeyCode();
if (newDirection == KeyEvent.VK_LEFT && direction != KeyEvent.VK_RIGHT) {
direction = newDirection;
} else if (newDirection == KeyEvent.VK_RIGHT && direction != KeyEvent.VK_LEFT) {
direction = newDirection;
} else if (newDirection == KeyEvent.VK_UP && direction != KeyEvent.VK_DOWN) {
direction = newDirection;
} else if (newDirection == KeyEvent.VK_DOWN && direction != KeyEvent.VK_UP) {
direction = newDirection;
}
}
public void keyTyped(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
public static void main(String[] args) {
JFrame frame = new JFrame("Snake Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(new SnakeGame());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
```
这个游戏使用了 Java Swing 组件进行界面绘制,通过 `LinkedList` 存储贪吃蛇的每一个节点,并使用 `Point` 类型记录每个节点的坐标。通过 `KeyListener` 监听键盘事件,改变贪吃蛇的运动方向。游戏中使用了一个 `Thread` 对游戏进行更新,保证游戏的流畅性。
阅读全文