写一段Java代码的贪吃蛇游戏
时间: 2023-08-31 07:24:10 浏览: 82
### 回答1:
好的,这是一段简单的贪吃蛇游戏的 Java 代码。这段代码中实现了贪吃蛇移动、吃到食物的逻辑:
```
import java.awt.*;
import javax.swing.*;
import java.util.ArrayList;
public class SnakeGame {
// 定义贪吃蛇的身体
static ArrayList<Point> snake = new ArrayList<>();
// 定义食物的位置
static Point food;
// 定义游戏窗口的长和宽
static final int WIDTH = 400, HEIGHT = 400;
// 定义贪吃蛇移动的方向
static int direction = 3; // 1: 上, 2: 右, 3: 下, 4: 左
public static void main(String[] args) {
// 初始化贪吃蛇的位置和食物的位置
snake.add(new Point(WIDTH / 2, HEIGHT / 2));
food = new Point((int)(Math.random() * WIDTH), (int)(Math.random() * HEIGHT));
// 开启一个新线程,每隔 50 毫秒更新一次贪吃蛇的位置
new Thread(() -> {
while (true) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 移动贪吃蛇
move();
}
}).start();
}
// 移动贪吃蛇
static void move() {
// 创建新的头部
Point head = new Point(snake.get(0));
// 根据方向更新头部位置
if (direction == 1) {
head.y--;
} else if (direction == 2) {
head.x++;
} else if (direction == 3) {
head.y++;
} else if (direction == 4) {
head.x--;
}
// 如果贪吃蛇吃到了食物,则不删除贪吃蛇的尾巴
if (head.x == food.x && head.y == food.y) {
food.x = (int)(Math.random() * WIDTH);
### 回答2:
贪吃蛇游戏是一款经典的游戏,在Java中可以通过使用图形界面来实现。以下是一个简单的贪吃蛇游戏的示例代码:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SnakeGame extends JFrame {
private Snake snake;
private Timer timer;
private boolean isGameOver;
public SnakeGame() {
snake = new Snake();
timer = new Timer(100, new TimerListener());
isGameOver = false;
add(snake);
addKeyListener(new KeyboardListener());
setResizable(false);
pack();
setTitle("贪吃蛇游戏");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
timer.start();
}
private class Snake extends JPanel {
// 蛇的身体坐标
private int[] x = new int[100];
private int[] y = new int[100];
// 蛇的长度
private int length;
// 蛇的当前方向
private Direction direction;
public Snake() {
length = 3;
direction = Direction.RIGHT;
// 初始化蛇的初始位置
for (int i = 0; i < length; i++) {
x[i] = 50 - i * 10;
y[i] = 50;
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (!isGameOver) {
// 绘制食物
g.setColor(Color.RED);
g.fillRect(200, 200, 10, 10);
// 绘制蛇的身体
g.setColor(Color.GREEN);
for (int i = 0; i < length; i++) {
g.fillRect(x[i], y[i], 10, 10);
}
} else {
g.setColor(Color.RED);
g.setFont(new Font("Arial", Font.BOLD, 20));
g.drawString("游戏结束", 100, 100);
}
}
public void move() {
// 移动蛇的身体
for (int i = length - 1; i > 0; i--) {
x[i] = x[i - 1];
y[i] = y[i - 1];
}
// 根据方向移动蛇头
switch (direction) {
case UP:
y[0] -= 10;
break;
case DOWN:
y[0] += 10;
break;
case LEFT:
x[0] -= 10;
break;
case RIGHT:
x[0] += 10;
break;
}
// 检查蛇是否撞到墙壁或自己
if (x[0] < 0 || x[0] >= 400 || y[0] < 0 || y[0] >= 400 || isCollided()) {
isGameOver = true;
timer.stop();
}
// 检查蛇是否吃到食物
if (x[0] == 200 && y[0] == 200) {
length++;
x[length - 1] = x[length - 2];
y[length - 1] = y[length - 2];
}
repaint();
}
public boolean isCollided() {
for (int i = 1; i < length; i++) {
if (x[0] == x[i] && y[0] == y[i]) {
return true;
}
}
return false;
}
}
private enum Direction {
UP, DOWN, LEFT, RIGHT
}
private class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
snake.move();
}
}
private class KeyboardListener extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
Direction currentDirection = snake.direction;
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
if (currentDirection != Direction.DOWN) {
snake.direction = Direction.UP;
}
break;
case KeyEvent.VK_DOWN:
if (currentDirection != Direction.UP) {
snake.direction = Direction.DOWN;
}
break;
case KeyEvent.VK_LEFT:
if (currentDirection != Direction.RIGHT) {
snake.direction = Direction.LEFT;
}
break;
case KeyEvent.VK_RIGHT:
if (currentDirection != Direction.LEFT) {
snake.direction = Direction.RIGHT;
}
break;
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(SnakeGame::new);
}
}
```
这段代码使用了Java的Swing库来创建图形界面。其中Snake类继承自JPanel,用于绘制游戏界面。游戏主窗口通过继承JFrame并添加事件监听器来响应键盘操作。在游戏中,蛇可以通过上下左右键来改变移动方向,蛇吃到食物后身体长度增加,游戏结束的条件为蛇撞到墙壁或自身。