设计一个贪吃蛇游戏利用java并且具有暂停功能
时间: 2024-02-21 20:02:25 浏览: 16
好的,下面是一个简单的贪吃蛇游戏的Java代码,同时也包含了暂停功能的实现:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SnakeGame extends JFrame implements ActionListener {
private final int WIDTH = 500; // 游戏窗口宽度
private final int HEIGHT = 500; // 游戏窗口高度
private final int UNIT_SIZE = 10; // 蛇身体单位大小
private final int MAX_UNITS = (WIDTH * HEIGHT) / UNIT_SIZE; // 蛇身体最大长度
private final int DELAY = 100; // 游戏速度
private int x[] = new int[MAX_UNITS]; // 蛇身体X坐标
private int y[] = new int[MAX_UNITS]; // 蛇身体Y坐标
private int bodyParts = 6; // 蛇身体长度
private int appleX; // 苹果X坐标
private int appleY; // 苹果Y坐标
private char direction = 'R'; // 蛇移动方向
private boolean running = false; // 游戏是否正在运行
private Timer timer; // 游戏计时器
private JButton pauseButton; // 暂停按钮
public SnakeGame() {
initGame();
initUI();
}
private void initGame() { // 初始化游戏
addKeyListener(new MyKeyAdapter());
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setBackground(Color.BLACK);
setFocusable(true);
initSnake();
newApple();
running = true;
timer = new Timer(DELAY, this);
timer.start();
}
private void initUI() { // 初始化UI界面
pauseButton = new JButton("暂停");
pauseButton.addActionListener(this);
JPanel buttonPanel = new JPanel();
buttonPanel.add(pauseButton);
add(buttonPanel, BorderLayout.SOUTH);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
private void initSnake() { // 初始化蛇
bodyParts = 6;
for (int i = 0; i < bodyParts; i++) {
x[i] = (WIDTH / 2) - (i * UNIT_SIZE);
y[i] = (HEIGHT / 2);
}
}
private void newApple() { // 新增苹果
appleX = (int) (Math.random() * (WIDTH / UNIT_SIZE)) * UNIT_SIZE;
appleY = (int) (Math.random() * (HEIGHT / UNIT_SIZE)) * UNIT_SIZE;
}
private 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;
}
}
private void checkApple() { // 检查是否吃到苹果
if ((x[0] == appleX) && (y[0] == appleY)) {
bodyParts++;
newApple();
}
}
private void checkCollision() { // 检查碰撞
for (int i = bodyParts; i > 0; i--) {
if ((x[0] == x[i]) && (y[0] == y[i])) {
running = false;
}
}
if (x[0] < 0) {
running = false;
}
if (x[0] > WIDTH) {
running = false;
}
if (y[0] < 0) {
running = false;
}
if (y[0] > HEIGHT) {
running = false;
}
if (!running) {
timer.stop();
}
}
private void gameOver(Graphics g) { // 游戏结束
String message = "游戏结束!";
Font font = new Font("Times New Roman", Font.BOLD, 30);
FontMetrics metrics = getFontMetrics(font);
g.setColor(Color.WHITE);
g.setFont(font);
g.drawString(message, (WIDTH - metrics.stringWidth(message)) / 2, (HEIGHT / 2));
}
@Override
public void actionPerformed(ActionEvent e) {
if (running) {
move();
checkApple();
checkCollision();
}
repaint();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
draw(g);
}
private void draw(Graphics g) { // 画出游戏界面
if (running) {
g.setColor(Color.RED);
g.fillOval(appleX, appleY, UNIT_SIZE, UNIT_SIZE);
for (int i = 0; i < bodyParts; i++) {
if (i == 0) {
g.setColor(Color.GREEN);
g.fillRect(x[i], y[i], UNIT_SIZE, UNIT_SIZE);
} else {
g.setColor(Color.YELLOW);
g.fillRect(x[i], y[i], UNIT_SIZE, UNIT_SIZE);
}
}
} else {
gameOver(g);
}
}
private 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) {
new SnakeGame();
}
}
```
上述代码中,实现了一个简单的贪吃蛇游戏,并且包含了暂停功能。具体实现方式为在界面下方添加一个“暂停”按钮,点击后暂停游戏计时器,游戏界面停止更新。