Java实现一个贪吃蛇小游戏代码并加注释
时间: 2023-06-15 14:04:03 浏览: 36
以下是Java实现贪吃蛇小游戏的代码,已加上注释:
```java
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class SnakeGame extends JPanel implements ActionListener, KeyListener {
// 定义游戏界面大小
public static final int WIDTH = 500;
public static final int HEIGHT = 500;
// 定义蛇的大小
public static final int DOT_SIZE = 10;
// 定义蛇的最大长度
public static final int MAX_DOTS = (WIDTH * HEIGHT) / (DOT_SIZE * DOT_SIZE);
// 定义游戏的速度
public static final int DELAY = 140;
// 定义蛇的身体和食物的坐标
private ArrayList<Point> snake;
private Point food;
// 定义方向
private boolean leftDirection = false;
private boolean rightDirection = true;
private boolean upDirection = false;
private boolean downDirection = false;
// 定义游戏是否结束
private boolean inGame = true;
// 定义定时器
private Timer timer;
public SnakeGame() {
// 设置游戏界面大小
setPreferredSize(new Dimension(WIDTH, HEIGHT));
// 初始化蛇的身体和食物的坐标
snake = new ArrayList<Point>();
snake.add(new Point(50, 50));
snake.add(new Point(40, 50));
snake.add(new Point(30, 50));
// 生成随机食物的坐标
generateFood();
// 创建定时器
timer = new Timer(DELAY, this);
timer.start();
// 添加键盘监听器
addKeyListener(this);
setFocusable(true);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
// 如果游戏还没有结束
if (inGame) {
// 绘制食物
g.setColor(Color.RED);
g.fillRect(food.x, food.y, DOT_SIZE, DOT_SIZE);
// 绘制蛇的身体
for (int i = 0; i < snake.size(); i++) {
g.setColor(Color.GREEN);
g.fillRect(snake.get(i).x, snake.get(i).y, DOT_SIZE, DOT_SIZE);
}
} else {
// 如果游戏结束,显示游戏结束
String msg = "Game Over";
g.setColor(Color.RED);
g.drawString(msg, (WIDTH - g.getFontMetrics().stringWidth(msg)) / 2, HEIGHT / 2);
}
}
public void checkFood() {
// 如果蛇头的坐标和食物的坐标重合
if (snake.get(0).x == food.x && snake.get(0).y == food.y) {
// 在蛇的最后添加一个身体
snake.add(new Point(snake.get(snake.size() - 1)));
generateFood(); // 生成新的食物
}
}
public void checkCollision() {
// 如果蛇头碰到边界,游戏结束
if (snake.get(0).x < 0 || snake.get(0).x > WIDTH - DOT_SIZE || snake.get(0).y < 0
|| snake.get(0).y > HEIGHT - DOT_SIZE) {
inGame = false;
}
// 如果蛇头碰到身体,游戏结束
for (int i = 1; i < snake.size(); i++) {
if (snake.get(0).x == snake.get(i).x && snake.get(0).y == snake.get(i).y) {
inGame = false;
}
}
}
public void generateFood() {
// 生成随机食物的坐标
Random rand = new Random();
int x = rand.nextInt(WIDTH / DOT_SIZE) * DOT_SIZE;
int y = rand.nextInt(HEIGHT / DOT_SIZE) * DOT_SIZE;
food = new Point(x, y);
}
@Override
public void actionPerformed(ActionEvent e) {
// 如果游戏还没有结束
if (inGame) {
// 移动蛇的身体
for (int i = snake.size() - 1; i > 0; i--) {
snake.set(i, new Point(snake.get(i - 1)));
}
// 根据方向移动蛇头
if (leftDirection) {
snake.get(0).x -= DOT_SIZE;
}
if (rightDirection) {
snake.get(0).x += DOT_SIZE;
}
if (upDirection) {
snake.get(0).y -= DOT_SIZE;
}
if (downDirection) {
snake.get(0).y += DOT_SIZE;
}
// 检查蛇头是否吃到食物
checkFood();
// 检查蛇头是否碰到边界或身体
checkCollision();
// 重绘游戏界面
repaint();
}
}
@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;
rightDirection = false;
leftDirection = false;
}
if ((key == KeyEvent.VK_DOWN) && (!upDirection)) {
downDirection = true;
rightDirection = false;
leftDirection = false;
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
public static void main(String[] args) {
JFrame frame = new JFrame("Snake Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
SnakeGame game = new SnakeGame();
frame.add(game);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
```