请用java写一段通吃蛇游戏代码
时间: 2023-07-10 19:03:52 浏览: 92
好的,以下是一段简单的通吃蛇游戏代码(注释已添加):
```
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SnakeGame extends JPanel implements KeyListener, ActionListener {
// 游戏区域大小
private static final int WIDTH = 400;
private static final int HEIGHT = 400;
// 蛇的初始坐标和长度
private int snakeX[] = new int[600];
private int snakeY[] = new int[600];
private int length = 3;
// 食物的坐标
private int foodX;
private int foodY;
// 蛇的当前移动方向
private String direction = "Right";
// 游戏是否结束
private boolean isGameOver = false;
// 定时器,控制蛇的移动速度
private Timer timer = new Timer(150, this);
public SnakeGame() {
// 设置游戏区域大小
setPreferredSize(new Dimension(WIDTH, HEIGHT));
// 初始化蛇的初始位置
snakeX[0] = 100;
snakeY[0] = 100;
snakeX[1] = 75;
snakeY[1] = 100;
snakeX[2] = 50;
snakeY[2] = 100;
// 随机初始化第一个食物
generateFood();
// 添加键盘监听器
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
// 开始定时器
timer.start();
}
private void generateFood() {
// 随机生成食物的位置,确保不与蛇的位置重叠
foodX = (int)(Math.random() * ((WIDTH - 10) / 10)) * 10 + 5;
foodY = (int)(Math.random() * ((HEIGHT - 10) / 10)) * 10 + 5;
for (int i = 0; i < length; i++) {
if (foodX == snakeX[i] && foodY == snakeY[i]) {
generateFood();
break;
}
}
}
private void move() {
// 根据当前方向移动蛇头
if (direction.equals("Right")) {
snakeX[0] += 10;
} else if (direction.equals("Left")) {
snakeX[0] -= 10;
} else if (direction.equals("Up")) {
snakeY[0] -= 10;
} else if (direction.equals("Down")) {
snakeY[0] += 10;
}
// 检查蛇是否吃到了食物
if (snakeX[0] == foodX && snakeY[0] == foodY) {
length++;
generateFood();
}
// 更新蛇的身体位置
for (int i = length - 1; i > 0; i--) {
snakeX[i] = snakeX[i - 1];
snakeY[i] = snakeY[i - 1];
}
// 检查蛇是否碰到了墙或自己的身体
if (snakeX[0] < 5 || snakeX[0] >= WIDTH - 5 || snakeY[0] < 5 || snakeY[0] >= HEIGHT - 5) {
isGameOver = true;
}
for (int i = 1; i < length; i++) {
if (snakeX[0] == snakeX[i] && snakeY[0] == snakeY[i]) {
isGameOver = true;
break;
}
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
// 绘制游戏区域
g.setColor(Color.WHITE);
g.fillRect(0, 0, WIDTH, HEIGHT);
g.setColor(Color.BLACK);
g.drawRect(0, 0, WIDTH - 1, HEIGHT - 1);
// 绘制蛇头
g.setColor(Color.GREEN);
g.fillRect(snakeX[0], snakeY[0], 10, 10);
// 绘制蛇的身体
g.setColor(Color.BLACK);
for (int i = 1; i < length; i++) {
g.fillRect(snakeX[i], snakeY[i], 10, 10);
}
// 绘制食物
g.setColor(Color.RED);
g.fillRect(foodX, foodY, 10, 10);
// 绘制游戏结束提示
if (isGameOver) {
g.setColor(Color.BLACK);
g.setFont(new Font("Arial", Font.BOLD, 24));
g.drawString("Game Over", 150, 200);
}
}
@Override
public void keyPressed(KeyEvent e) {
// 键盘按键监听器
int key = e.getKeyCode();
if ((key == KeyEvent.VK_LEFT || key == KeyEvent.VK_A) && !direction.equals("Right")) {
direction = "Left";
} else if ((key == KeyEvent.VK_RIGHT || key == KeyEvent.VK_D) && !direction.equals("Left")) {
direction = "Right";
} else if ((key == KeyEvent.VK_UP || key == KeyEvent.VK_W) && !direction.equals("Down")) {
direction = "Up";
} else if ((key == KeyEvent.VK_DOWN || key == KeyEvent.VK_S) && !direction.equals("Up")) {
direction = "Down";
}
}
@Override
public void actionPerformed(ActionEvent e) {
// 定时器监听器,每次移动蛇并重绘游戏区域
if (!isGameOver) {
move();
}
repaint();
}
public static void main(String[] args) {
// 创建游戏窗口
JFrame frame = new JFrame("Snake Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(new SnakeGame(), BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
@Override
public void keyTyped(KeyEvent e) {}
@Override
public void keyReleased(KeyEvent e) {}
}
```
希望能够帮助到你!
阅读全文