写一个java的贪吃蛇游戏
时间: 2024-05-10 17:18:56 浏览: 120
这里是一个简单的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.ArrayList;
import java.util.List;
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 static final int SIZE = 10;
// 游戏更新间隔,单位:毫秒
private static final int INTERVAL = 100;
// 游戏是否结束
private boolean isGameOver = false;
// 游戏线程
private Thread gameThread;
// 蛇身
private List<Point> snake;
// 食物
private Point food;
// 蛇头方向
private Direction direction = Direction.RIGHT;
public SnakeGame() {
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setBackground(Color.BLACK);
setFocusable(true);
addKeyListener(this);
startGame();
}
// 开始游戏
private void startGame() {
snake = new ArrayList<Point>();
snake.add(new Point(0, 0));
snake.add(new Point(SIZE, 0));
snake.add(new Point(SIZE * 2, 0));
createFood();
gameThread = new Thread(this);
gameThread.start();
}
// 创建食物
private void createFood() {
int x = (int) (Math.random() * (WIDTH / SIZE)) * SIZE;
int y = (int) (Math.random() * (HEIGHT / SIZE)) * SIZE;
food = new Point(x, y);
}
// 绘制游戏画面
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
// 绘制食物
g.setColor(Color.RED);
g.fillRect(food.x, food.y, SIZE, SIZE);
// 绘制蛇身
g.setColor(Color.WHITE);
for (Point p : snake) {
g.fillRect(p.x, p.y, SIZE, SIZE);
}
// 如果游戏结束,绘制 Game Over 文字
if (isGameOver) {
g.setColor(Color.WHITE);
g.drawString("Game Over", WIDTH / 2 - 30, HEIGHT / 2);
}
}
// 游戏主循环
@Override
public void run() {
while (!isGameOver) {
try {
Thread.sleep(INTERVAL);
} catch (InterruptedException e) {
e.printStackTrace();
}
update();
repaint();
}
}
// 更新游戏状态
private void update() {
// 移动蛇头
Point head = new Point(snake.get(0));
switch (direction) {
case UP:
head.y -= SIZE;
break;
case DOWN:
head.y += SIZE;
break;
case LEFT:
head.x -= SIZE;
break;
case RIGHT:
head.x += SIZE;
break;
}
snake.add(0, head);
// 判断是否吃到食物
if (head.equals(food)) {
createFood();
} else {
snake.remove(snake.size() - 1);
}
// 判断游戏是否结束
if (head.x < 0 || head.x >= WIDTH || head.y < 0 || head.y >= HEIGHT) {
isGameOver = true;
}
for (int i = 1; i < snake.size(); i++) {
if (head.equals(snake.get(i))) {
isGameOver = true;
}
}
}
// 监听键盘事件
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
if (direction != Direction.DOWN) {
direction = Direction.UP;
}
break;
case KeyEvent.VK_DOWN:
if (direction != Direction.UP) {
direction = Direction.DOWN;
}
break;
case KeyEvent.VK_LEFT:
if (direction != Direction.RIGHT) {
direction = Direction.LEFT;
}
break;
case KeyEvent.VK_RIGHT:
if (direction != Direction.LEFT) {
direction = Direction.RIGHT;
}
break;
}
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
public static void main(String[] args) {
JFrame frame = new JFrame("Snake Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new SnakeGame());
frame.pack();
frame.setVisible(true);
}
// 蛇头方向枚举
private enum Direction {
UP, DOWN, LEFT, RIGHT
}
}
```
阅读全文