运用java写一个贪吃蛇小游戏
时间: 2023-12-20 11:07:05 浏览: 26
好的,我可以为您提供一个简单的 Java 贪吃蛇小游戏示例。
游戏规则:
1. 贪吃蛇初始长度为 3,每次移动一格;
2. 贪吃蛇不能碰到边界,也不能碰到自己;
3. 有食物出现在随机位置,贪吃蛇吃到食物后长度加一;
4. 游戏结束条件为贪吃蛇碰到边界或自己。
下面是示例代码:
```
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.LinkedList;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class SnakeGame extends JPanel implements KeyListener {
private static final long serialVersionUID = 1L;
private static final int WIDTH = 500;
private static final int HEIGHT = 500;
private static final int UNIT_SIZE = 10;
private static final int GAME_UNITS = (WIDTH * HEIGHT) / (UNIT_SIZE * UNIT_SIZE);
private static final int DELAY = 75;
private static final int[] X_OFFSET = { -1, 0, 1, 0 };
private static final int[] Y_OFFSET = { 0, -1, 0, 1 };
private static final Color SNAKE_COLOR = Color.GREEN;
private static final Color FOOD_COLOR = Color.RED;
private JFrame frame;
private LinkedList<Point> snake;
private Point food;
private int direction;
private boolean running;
public SnakeGame() {
frame = new JFrame("Snake Game");
frame.setSize(WIDTH, HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(this);
frame.setVisible(true);
frame.addKeyListener(this);
snake = new LinkedList<Point>();
snake.add(new Point(WIDTH / 2, HEIGHT / 2));
snake.add(new Point(WIDTH / 2, HEIGHT / 2 + UNIT_SIZE));
snake.add(new Point(WIDTH / 2, HEIGHT / 2 + 2 * UNIT_SIZE));
direction = 0;
running = true;
spawnFood();
new Thread(new Runnable() {
public void run() {
while (running) {
try {
Thread.sleep(DELAY);
} catch (InterruptedException e) {
e.printStackTrace();
}
tick();
repaint();
}
}
}).start();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
// 绘制贪吃蛇
g.setColor(SNAKE_COLOR);
for (Point p : snake) {
g.fillRect(p.x, p.y, UNIT_SIZE, UNIT_SIZE);
}
// 绘制食物
g.setColor(FOOD_COLOR);
g.fillRect(food.x, food.y, UNIT_SIZE, UNIT_SIZE);
}
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_LEFT && direction != 2) {
direction = 0;
} else if (keyCode == KeyEvent.VK_UP && direction != 3) {
direction = 1;
} else if (keyCode == KeyEvent.VK_RIGHT && direction != 0) {
direction = 2;
} else if (keyCode == KeyEvent.VK_DOWN && direction != 1) {
direction = 3;
}
}
public void keyTyped(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
private void tick() {
// 移动贪吃蛇
Point head = snake.getFirst();
Point newHead = new Point(head.x + X_OFFSET[direction] * UNIT_SIZE,
head.y + Y_OFFSET[direction] * UNIT_SIZE);
if (newHead.x < 0 || newHead.x >= WIDTH || newHead.y < 0 || newHead.y >= HEIGHT) {
// 碰到边界,游戏结束
running = false;
return;
}
for (Point p : snake) {
if (p.equals(newHead)) {
// 碰到自己,游戏结束
running = false;
return;
}
}
snake.addFirst(newHead);
if (newHead.equals(food)) {
// 吃到食物,长度加一,重新生成食物
spawnFood();
} else {
snake.removeLast();
}
}
private void spawnFood() {
Point p = new Point((int) (Math.random() * WIDTH / UNIT_SIZE) * UNIT_SIZE,
(int) (Math.random() * HEIGHT / UNIT_SIZE) * UNIT_SIZE);
while (snake.contains(p)) {
p = new Point((int) (Math.random() * WIDTH / UNIT_SIZE) * UNIT_SIZE,
(int) (Math.random() * HEIGHT / UNIT_SIZE) * UNIT_SIZE);
}
food = p;
}
public static void main(String[] args) {
new SnakeGame();
}
}
```
希望这个示例可以帮助您了解如何用 Java 编写简单的贪吃蛇小游戏。