写一段Java代码的贪吃蛇游戏
时间: 2023-08-31 13:27:08 浏览: 78
Java开发贪吃蛇游戏教程
5星 · 资源好评率100%
### 回答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 javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SnakeGame extends JFrame {
private Snake snake;
private Timer timer;
private int delay = 100;
public SnakeGame() {
snake = new Snake();
timer = new Timer(delay, new GameLoop());
setTitle("贪吃蛇");
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setLocationRelativeTo(null);
setVisible(true);
addKeyListener(new SnakeController());
timer.start();
}
private class GameLoop implements ActionListener {
public void actionPerformed(ActionEvent e) {
snake.move();
repaint();
}
}
private class SnakeController extends KeyAdapter {
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) {
snake.changeDirection(Snake.Direction.LEFT);
} else if (key == KeyEvent.VK_RIGHT) {
snake.changeDirection(Snake.Direction.RIGHT);
} else if (key == KeyEvent.VK_UP) {
snake.changeDirection(Snake.Direction.UP);
} else if (key == KeyEvent.VK_DOWN) {
snake.changeDirection(Snake.Direction.DOWN);
}
}
}
public void paint(Graphics g) {
super.paint(g);
snake.draw(g);
}
public static void main(String[] args) {
new SnakeGame();
}
}
class Snake {
private int x;
private int y;
private Direction direction;
public Snake() {
x = 0;
y = 0;
direction = Direction.RIGHT;
}
public void changeDirection(Direction newDirection) {
direction = newDirection;
}
public void move() {
if (direction == Direction.RIGHT) {
x++;
} else if (direction == Direction.LEFT) {
x--;
} else if (direction == Direction.UP) {
y--;
} else if (direction == Direction.DOWN) {
y++;
}
}
public void draw(Graphics g) {
g.setColor(Color.green);
g.fillRect(x * 10, y * 10, 10, 10);
}
enum Direction {
LEFT, RIGHT, UP, DOWN
}
}
```
这段代码实现了一个简单的贪吃蛇游戏。游戏界面使用JFrame来创建窗口,使用Graphics类在窗口中绘制蛇。Snake类表示蛇的属性和行为,包括蛇的坐标、方向和移动等。贪吃蛇的移动通过定时器Timer来控制,在每一次定时器触发时更新蛇的位置,并通过repaint()方法重新绘制蛇的位置。用户通过键盘事件控制蛇的方向。
### 回答3:
以下是用Java编写的一个简单的贪吃蛇游戏的代码段:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SnakeGame extends JFrame {
private static final int WIDTH = 30; // 游戏界面的宽度(格数)
private static final int HEIGHT = 20; // 游戏界面的高度(格数)
private static final int DELAY = 150; // 控制蛇移动的速度
private Timer timer;
private SnakePanel snakePanel;
private Snake snake;
private Point food;
public SnakeGame() {
snakePanel = new SnakePanel();
snake = new Snake();
food = new Point();
timer = new Timer(DELAY, new ActionListener() {
public void actionPerformed(ActionEvent e) {
snake.move();
if (snake.isCollidingWithFood(food)) {
snake.eat(food);
generateFood();
}
if (snake.isCollidingWithItself() || snake.isOutOfBounds(WIDTH, HEIGHT)) {
gameOver();
}
snakePanel.repaint();
}
});
add(snakePanel);
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
snake.changeDirection(e.getKeyCode());
}
});
setTitle("贪吃蛇游戏");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
pack();
setLocationRelativeTo(null);
generateFood();
timer.start();
}
private void generateFood() {
boolean valid = false;
while (!valid) {
int x = (int) (Math.random() * WIDTH);
int y = (int) (Math.random() * HEIGHT);
if (!snake.contains(x, y)) {
food.setLocation(x, y);
valid = true;
}
}
}
private void gameOver() {
timer.stop();
JOptionPane.showMessageDialog(this, "游戏结束", "提示", JOptionPane.INFORMATION_MESSAGE);
}
private class SnakePanel extends JPanel {
private static final int SIZE = 20; // 每个格子的大小
public SnakePanel() {
setPreferredSize(new Dimension(WIDTH * SIZE, HEIGHT * SIZE));
setBackground(Color.WHITE);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
snake.draw(g, SIZE);
g.setColor(Color.RED);
g.fillOval(food.x * SIZE, food.y * SIZE, SIZE, SIZE);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new SnakeGame().setVisible(true);
}
});
}
}
```
阅读全文