编写一个贪吃蛇的JAVA小游戏
时间: 2023-06-22 14:37:56 浏览: 241
好的,我来为您编写一个基本的贪吃蛇小游戏。游戏规则如下:
1. 贪吃蛇初始长度为3,每次吃到食物会增加1格长度。
2. 如果贪吃蛇撞到自己或者墙壁,游戏结束。
3. 食物每次随机生成在游戏区域内的一个位置。
以下是JAVA代码实现:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SnakeGame extends JPanel implements ActionListener {
private final int WIDTH = 300;
private final int HEIGHT = 300;
private final int UNIT_SIZE = 10;
private final int GAME_UNITS = (WIDTH * HEIGHT) / UNIT_SIZE;
private final int DELAY = 100;
private final int[] x = new int[GAME_UNITS];
private final int[] y = new int[GAME_UNITS];
private int bodyParts = 3;
private int foodEaten;
private int foodX;
private int foodY;
private char direction = 'R';
private boolean running = false;
private Timer timer;
public SnakeGame() {
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setBackground(Color.black);
setFocusable(true);
addKeyListener(new MyKeyAdapter());
startGame();
}
public void startGame() {
newFood();
running = true;
timer = new Timer(DELAY, this);
timer.start();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
draw(g);
}
public void draw(Graphics g) {
if (running) {
// 绘制食物
g.setColor(Color.red);
g.fillRect(foodX, foodY, UNIT_SIZE, UNIT_SIZE);
// 绘制蛇身
for (int i = 0; i < bodyParts; i++) {
if (i == 0) {
g.setColor(Color.green);
} else {
g.setColor(new Color(45, 180, 0));
}
g.fillRect(x[i], y[i], UNIT_SIZE, UNIT_SIZE);
}
// 绘制分数
g.setColor(Color.white);
g.drawString("得分:" + foodEaten, 10, 20);
} else {
gameOver(g);
}
}
public void newFood() {
foodX = (int) (Math.random() * (WIDTH / UNIT_SIZE)) * UNIT_SIZE;
foodY = (int) (Math.random() * (HEIGHT / UNIT_SIZE)) * UNIT_SIZE;
}
public void move() {
// 移动身体
for (int i = bodyParts; i > 0; i--) {
x[i] = x[i - 1];
y[i] = y[i - 1];
}
// 移动头部
switch (direction) {
case 'U':
y[0] = y[0] - UNIT_SIZE;
break;
case 'D':
y[0] = y[0] + UNIT_SIZE;
break;
case 'L':
x[0] = x[0] - UNIT_SIZE;
break;
case 'R':
x[0] = x[0] + UNIT_SIZE;
break;
}
}
public void checkFood() {
if (x[0] == foodX && y[0] == foodY) {
bodyParts++;
foodEaten++;
newFood();
}
}
public void checkCollisions() {
// 撞墙检测
for (int i = bodyParts; i > 0; i--) {
if (x[0] == x[i] && y[0] == y[i]) {
running = false;
}
}
if (x[0] < 0 || x[0] >= WIDTH || y[0] < 0 || y[0] >= HEIGHT) {
running = false;
}
if (!running) {
timer.stop();
}
}
public void gameOver(Graphics g) {
g.setColor(Color.white);
g.setFont(new Font("Ink Free", Font.BOLD, 40));
FontMetrics metrics = getFontMetrics(g.getFont());
g.drawString("Game Over", (WIDTH - metrics.stringWidth("Game Over")) / 2, HEIGHT / 2);
g.drawString("得分:" + foodEaten, (WIDTH - metrics.stringWidth("得分:" + foodEaten)) / 2, (HEIGHT / 2) + 50);
}
@Override
public void actionPerformed(ActionEvent e) {
if (running) {
move();
checkFood();
checkCollisions();
}
repaint();
}
public class MyKeyAdapter extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT:
if (direction != 'R') {
direction = 'L';
}
break;
case KeyEvent.VK_RIGHT:
if (direction != 'L') {
direction = 'R';
}
break;
case KeyEvent.VK_UP:
if (direction != 'D') {
direction = 'U';
}
break;
case KeyEvent.VK_DOWN:
if (direction != 'U') {
direction = 'D';
}
break;
}
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("贪吃蛇");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new SnakeGame());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
```
您可以将以上代码保存到一个名为`SnakeGame.java`的文件中,并使用JDK编译运行。
阅读全文