那我就我一个简单的版本
时间: 2023-07-31 17:08:57 浏览: 36
好的,以下是一个简单的JAVA贪吃蛇游戏的代码示例,其中使用了JAVA Swing库和基本的面向对象编程思想:
```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 KeyListener, ActionListener {
// 游戏窗口大小
public static final int WIDTH = 400;
public static final int HEIGHT = 400;
// 蛇体方格大小
public static final int UNIT_SIZE = 20;
// 游戏速度
public static final int DELAY = 100;
// 游戏状态
private boolean isPlaying = false;
private boolean isGameOver = false;
// 游戏得分
private int score = 0;
// 蛇体和食物
private ArrayList<Point> snake;
private Point food;
// 随机数生成器
private Random rand = new Random();
public SnakeGame() {
// 初始化游戏窗口
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setBackground(Color.BLACK);
setFocusable(true);
addKeyListener(this);
// 初始化蛇体和食物
snake = new ArrayList<Point>();
snake.add(new Point(WIDTH / 2, HEIGHT / 2));
food = new Point(rand.nextInt(WIDTH / UNIT_SIZE) * UNIT_SIZE,
rand.nextInt(HEIGHT / UNIT_SIZE) * UNIT_SIZE);
// 启动游戏计时器
Timer timer = new Timer(DELAY, this);
timer.start();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
// 绘制蛇体和食物
g.setColor(Color.YELLOW);
for (Point p : snake) {
g.fillRect(p.x, p.y, UNIT_SIZE, UNIT_SIZE);
}
g.setColor(Color.RED);
g.fillRect(food.x, food.y, UNIT_SIZE, UNIT_SIZE);
// 绘制游戏状态和得分
if (!isPlaying) {
g.setColor(Color.WHITE);
g.drawString("Press SPACE to start", WIDTH / 2 - 60, HEIGHT / 2);
} else if (isGameOver) {
g.setColor(Color.WHITE);
g.drawString("Game Over! Press SPACE to restart", WIDTH / 2 - 90, HEIGHT / 2);
} else {
g.setColor(Color.WHITE);
g.drawString("Score: " + score, 10, 20);
}
}
public void actionPerformed(ActionEvent e) {
// 更新蛇体位置
Point head = snake.get(0);
Point newHead = new Point(head.x, head.y);
if (direction == Direction.UP) {
newHead.y -= UNIT_SIZE;
} else if (direction == Direction.DOWN) {
newHead.y += UNIT_SIZE;
} else if (direction == Direction.LEFT) {
newHead.x -= UNIT_SIZE;
} else if (direction == Direction.RIGHT) {
newHead.x += UNIT_SIZE;
}
snake.add(0, newHead);
snake.remove(snake.size() - 1);
// 检测碰撞和吃食物
if (newHead.equals(food)) {
score++;
food.setLocation(rand.nextInt(WIDTH / UNIT_SIZE) * UNIT_SIZE,
rand.nextInt(HEIGHT / UNIT_SIZE) * UNIT_SIZE);
} else if (newHead.x < 0 || newHead.x >= WIDTH ||
newHead.y < 0 || newHead.y >= HEIGHT ||
snake.contains(newHead)) {
isGameOver = true;
}
// 重绘游戏场景
repaint();
}
public void keyPressed(KeyEvent e) {
// 处理方向键输入
int key = e.getKeyCode();
if (key == KeyEvent.VK_UP && direction != Direction.DOWN) {
direction = Direction.UP;
} else if (key == KeyEvent.VK_DOWN && direction != Direction.UP) {
direction = Direction.DOWN;
} else if (key == KeyEvent.VK_LEFT && direction != Direction.RIGHT) {
direction = Direction.LEFT;
} else if (key == KeyEvent.VK_RIGHT && direction != Direction.LEFT) {
direction = Direction.RIGHT;
}
// 处理空格键输入
if (key == KeyEvent.VK_SPACE) {
if (isGameOver) {
// 重新开始游戏
isPlaying = true;
isGameOver = false;
score = 0;
snake.clear();
snake.add(new Point(WIDTH / 2, HEIGHT / 2));
food.setLocation(rand.nextInt(WIDTH / UNIT_SIZE) * UNIT_SIZE,
rand.nextInt(HEIGHT / UNIT_SIZE) * UNIT_SIZE);
} else {
// 开始或暂停游戏
isPlaying = !isPlaying;
}
}
}
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
public static void main(String[] args) {
JFrame frame = new JFrame("Snake Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new SnakeGame());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
enum Direction {
UP, DOWN, LEFT, RIGHT
}
```
这个简单的贪吃蛇游戏使用了JAVA Swing库绘制游戏窗口和场景,使用了面向对象编程思想将游戏逻辑分离为不同的类和方法,并使用了JAVA计时器和键盘监听器实现游戏速度和玩家输入的处理。您可以根据自己的需求和技术水平进行修改和优化。