用Java写一个贪吃蛇小游戏并注释
时间: 2023-05-13 08:05:35 浏览: 122
非常感谢您的提问,我可以为您提供一个简单的Java贪吃蛇小游戏的代码,并附上注释,代码如下:
```
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SnakeGame extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private final int WIDTH = 300; // 游戏窗口的宽度
private final int HEIGHT = 300; // 游戏窗口的高度
private final int DOT_SIZE = 10; // 蛇身的大小
private final int ALL_DOTS = 900; // 游戏区域最多能容纳的蛇身数量
private final int RAND_POS = 29; // 食物出现的位置范围
private final int DELAY = 140; // 游戏速度,单位为毫秒
private int x[] = new int[ALL_DOTS]; // 蛇身的横坐标
private int y[] = new int[ALL_DOTS]; // 蛇身的纵坐标
private int dots; // 蛇身的数量
private int apple_x; // 食物的横坐标
private int apple_y; // 食物的纵坐标
private Timer timer; // 游戏计时器
private boolean leftDirection = false; // 蛇是否向左移动
private boolean rightDirection = true; // 蛇是否向右移动
private boolean upDirection = false; // 蛇是否向上移动
private boolean downDirection = false; // 蛇是否向下移动
private boolean inGame = true; // 游戏是否结束
public SnakeGame() {
initGame();
}
private void initGame() {
// 初始化蛇身
dots = 3;
for (int i = 0; i < dots; i++) {
x[i] = 50 - i * DOT_SIZE;
y[i] = 50;
}
// 初始化食物
locateApple();
// 初始化游戏窗口
setTitle("贪吃蛇小游戏");
setSize(WIDTH, HEIGHT);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
// 添加键盘监听器
addKeyListener(new TAdapter());
// 初始化游戏计时器
timer = new Timer(DELAY, this);
timer.start();
}
private void locateApple() {
// 随机生成食物的位置
int r = (int) (Math.random() * RAND_POS);
apple_x = r * DOT_SIZE;
r = (int) (Math.random() * RAND_POS);
apple_y = r * DOT_SIZE;
}
private void checkApple() {
// 检查蛇是否吃到了食物
if ((x[0] == apple_x) && (y[0] == apple_y)) {
dots++;
locateApple();
}
}
private void checkCollision() {
// 检查蛇是否碰到了游戏区域的边缘或自己的身体
for (int i = dots; i > 0; i--) {
if ((i > 4) && (x[0] == x[i]) && (y[0] == y[i])) {
inGame = false;
}
}
if (y[0] >= HEIGHT) {
inGame = false;
}
if (y[0] < 0) {
inGame = false;
}
if (x[0] >= WIDTH) {
inGame = false;
}
if (x[0] < 0) {
inGame = false;
}
if (!inGame) {
timer.stop();
}
}
private void move() {
// 移动蛇身
for (int i = dots; i > 0; i--) {
x[i] = x[(i - 1)];
y[i] = y[(i - 1)];
}
if (leftDirection) {
x[0] -= DOT_SIZE;
}
if (rightDirection) {
x[0] += DOT_SIZE;
}
if (upDirection) {
y[0] -= DOT_SIZE;
}
if (downDirection) {
y[0] += DOT_SIZE;
}
}
private void doDrawing(Graphics g) {
// 绘制游戏区域
if (inGame) {
g.setColor(Color.red);
g.fillOval(apple_x, apple_y, DOT_SIZE, DOT_SIZE);
for (int i = 0; i < dots; i++) {
if (i == 0) {
g.setColor(Color.green);
g.fillRect(x[i], y[i], DOT_SIZE, DOT_SIZE);
} else {
g.setColor(Color.blue);
g.fillRect(x[i], y[i], DOT_SIZE, DOT_SIZE);
}
}
Toolkit.getDefaultToolkit().sync();
} else {
gameOver(g);
}
}
private void gameOver(Graphics g) {
// 游戏结束
String msg = "Game Over";
Font small = new Font("Helvetica", Font.BOLD, 14);
FontMetrics metr = getFontMetrics(small);
g.setColor(Color.black);
g.setFont(small);
g.drawString(msg, (WIDTH - metr.stringWidth(msg)) / 2, HEIGHT / 2);
}
@Override
public void actionPerformed(ActionEvent e) {
// 游戏计时器触发事件
if (inGame) {
checkApple();
checkCollision();
move();
}
repaint();
}
private class TAdapter extends KeyAdapter {
// 键盘监听器
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if ((key == KeyEvent.VK_LEFT) && (!rightDirection)) {
leftDirection = true;
upDirection = false;
downDirection = false;
}
if ((key == KeyEvent.VK_RIGHT) && (!leftDirection)) {
rightDirection = true;
upDirection = false;
downDirection = false;
}
if ((key == KeyEvent.VK_UP) && (!downDirection)) {
upDirection = true;
leftDirection = false;
rightDirection = false;
}
if ((key == KeyEvent.VK_DOWN) && (!upDirection)) {
downDirection = true;
leftDirection = false;
rightDirection = false;
}
}
}
public static void main(String[] args) {
// 启动游戏
EventQueue.invokeLater(() -> {
SnakeGame ex = new SnakeGame();
ex.setVisible(true);
});
}
}
```
希望这个代码能够帮助到您,如果您有任何问题或需要进一步的帮助,请随时联系我。
阅读全文