Java写一个贪吃蛇大作战
时间: 2023-11-05 18:04:24 浏览: 112
基于Java实现贪吃蛇大作战小游戏【100011695】
当然可以!下面是一个简单的Java代码示例,实现了一个基本的贪吃蛇大作战游戏:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class SnakeGame extends JFrame {
private static final int WIDTH = 400; // 游戏窗口宽度
private static final int HEIGHT = 400; // 游戏窗口高度
private static final int UNIT_SIZE = 20; // 单元格大小
private static final int GAME_UNITS = (WIDTH * HEIGHT) / (UNIT_SIZE * UNIT_SIZE); // 单元格数量
private static final int DELAY = 75; // 游戏速度
private final List<Integer> snakeX; // 蛇身体的x坐标
private final List<Integer> snakeY; // 蛇身体的y坐标
private int foodX; // 食物的x坐标
private int foodY; // 食物的y坐标
private Dir direction = Dir.RIGHT; // 蛇的移动方向
private boolean running = false; // 游戏是否正在运行
private Timer timer; // 定时器
public SnakeGame() {
snakeX = new ArrayList<>();
snakeY = new ArrayList<>();
initGame();
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT && direction != Dir.RIGHT) {
direction = Dir.LEFT;
} else if (key == KeyEvent.VK_RIGHT && direction != Dir.LEFT) {
direction = Dir.RIGHT;
} else if (key == KeyEvent.VK_UP && direction != Dir.DOWN) {
direction = Dir.UP;
} else if (key == KeyEvent.VK_DOWN && direction != Dir.UP) {
direction = Dir.DOWN;
}
}
});
setFocusable(true);
}
private void initGame() {
snakeX.clear();
snakeY.clear();
snakeX.add(WIDTH / 2);
snakeY.add(HEIGHT / 2);
generateFood();
running = true;
timer = new Timer(DELAY, e -> gameLoop());
timer.start();
}
private void generateFood() {
Random random = new Random();
foodX = random.nextInt((WIDTH / UNIT_SIZE)) * UNIT_SIZE;
foodY = random.nextInt((HEIGHT / UNIT_SIZE)) * UNIT_SIZE;
}
private void gameLoop() {
if (running) {
move();
checkCollision();
repaint();
}
}
private void move() {
for (int i = snakeX.size() - 1; i > 0; i--) {
snakeX.set(i, snakeX.get(i - 1));
snakeY.set(i, snakeY.get(i - 1));
}
switch (direction) {
case LEFT:
snakeX.set(0, snakeX.get(0) - UNIT_SIZE);
break;
case RIGHT:
snakeX.set(0, snakeX.get(0) + UNIT_SIZE);
break;
case UP:
snakeY.set(0, snakeY.get(0) - UNIT_SIZE);
break;
case DOWN:
snakeY.set(0, snakeY.get(0) + UNIT_SIZE);
break;
}
}
private void checkCollision() {
// 检查是否撞到自己的身体
for (int i = snakeX.size() - 1; i > 0; i--) {
if (snakeX.get(0).equals(snakeX.get(i)) && snakeY.get(0).equals(snakeY.get(i))) {
running = false;
}
}
// 检查是否撞到边界
if (snakeX.get(0) < 0 || snakeX.get(0) >= WIDTH || snakeY.get(0) < 0 || snakeY.get(0) >= HEIGHT) {
running = false;
}
// 检查是否吃到食物
if (snakeX.get(0).equals(foodX) && snakeY.get(0).equals(foodY)) {
snakeX.add(foodX);
snakeY.add(foodY);
generateFood();
}
}
@Override
public void paint(Graphics g) {
super.paint(g);
// 绘制背景
g.setColor(Color.BLACK);
g.fillRect(0, 0, WIDTH, HEIGHT);
// 绘制蛇头
g.setColor(Color.GREEN);
g.fillRect(snakeX.get(0), snakeY.get(0), UNIT_SIZE, UNIT_SIZE);
// 绘制蛇身体
g.setColor(Color.WHITE);
for (int i = 1; i < snakeX.size(); i++) {
g.fillRect(snakeX.get(i), snakeY.get(i), UNIT_SIZE, UNIT_SIZE);
}
// 绘制食物
g.setColor(Color.RED);
g.fillRect(foodX, foodY, UNIT_SIZE, UNIT_SIZE);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
SnakeGame game = new SnakeGame();
game.setSize(WIDTH, HEIGHT);
game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.setTitle("贪吃蛇大作战");
game.setResizable(false);
game.setVisible(true);
});
}
private enum Dir {
LEFT,
RIGHT,
UP,
DOWN
}
}
```
这段代码使用了Java的Swing库来创建窗口和处理绘图,利用定时器来更新游戏状态。通过键盘监听器来控制蛇的移动方向,通过碰撞检测判断游戏是否结束,并且随机生成食物。
希望这个示例能满足你的需求,如果有任何问题,请随时提问!
阅读全文