使用JAVA语言实现贪吃蛇,要求实现“开始”“暂停”“加速”三个额外功能
时间: 2023-10-20 07:07:20 浏览: 81
好的,我可以为您提供一个简单的贪吃蛇游戏的Java实现,同时添加“开始”、“暂停”、“加速”三个额外功能。以下是代码实现:
```java
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
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.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class SnakeGame extends JPanel implements ActionListener, KeyListener {
private static final long serialVersionUID = 1L;
private final int WIDTH = 500;
private final int HEIGHT = 500;
private final int DOT_SIZE = 10;
private final int ALL_DOTS = 900;
private final int RAND_POS = 29;
private final int DELAY = 140;
private final int SPEED_UP_DELAY = 70;
private final int START_X = 100;
private final int START_Y = 100;
private final int[] x = new int[ALL_DOTS];
private final int[] y = new int[ALL_DOTS];
private int dots;
private int apple_x;
private int apple_y;
private boolean leftDirection = false;
private boolean rightDirection = true;
private boolean upDirection = false;
private boolean downDirection = false;
private boolean inGame = true;
private Timer timer;
private boolean isPaused = false;
private boolean isSpeedUp = false;
public SnakeGame() {
addKeyListener(this);
setBackground(Color.black);
setFocusable(true);
setPreferredSize(new Dimension(WIDTH, HEIGHT));
initGame();
}
private void initGame() {
dots = 3;
for (int z = 0; z < dots; z++) {
x[z] = START_X - z * DOT_SIZE;
y[z] = START_Y;
}
locateApple();
timer = new Timer(DELAY, this);
timer.start();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
doDrawing(g);
}
private void doDrawing(Graphics g) {
if (inGame) {
g.setColor(Color.red);
g.fillOval(apple_x, apple_y, DOT_SIZE, DOT_SIZE);
for (int z = 0; z < dots; z++) {
if (z == 0) {
g.setColor(Color.green);
} else {
g.setColor(Color.yellow);
}
g.fillRect(x[z], y[z], DOT_SIZE, DOT_SIZE);
}
g.setColor(Color.white);
g.setFont(new Font("Arial", Font.BOLD, 18));
g.drawString("Score: " + (dots - 3), 20, 25);
if (isPaused) {
g.setFont(new Font("Arial", Font.BOLD, 40));
g.drawString("PAUSED", 170, 250);
}
} else {
gameOver(g);
}
}
private void gameOver(Graphics g) {
g.setFont(new Font("Arial", Font.BOLD, 40));
g.drawString("Game Over", 170, 250);
g.setFont(new Font("Arial", Font.BOLD, 18));
g.drawString("Score: " + (dots - 3), 20, 25);
}
private void checkApple() {
if ((x[0] == apple_x) && (y[0] == apple_y)) {
dots++;
locateApple();
if (isSpeedUp) {
timer.setDelay(SPEED_UP_DELAY);
}
}
}
private void move() {
for (int z = dots; z > 0; z--) {
x[z] = x[(z - 1)];
y[z] = y[(z - 1)];
}
if (leftDirection) {
x[0] -= DOT_SIZE;
}
if (rightDirection) {
x[0] += DOT_SIZE;
}
if (upDirection) {
y[0] -= DOT_SIZE;
}
if (downDirection) {
y[0] += DOT_SIZE;
}
}
private void checkCollision() {
for (int z = dots; z > 0; z--) {
if ((z > 4) && (x[0] == x[z]) && (y[0] == y[z])) {
inGame = false;
}
}
if (y[0] >= HEIGHT) {
inGame = false;
}
if (y[0] < 0) {
inGame = false;
}
if (x[0] >= WIDTH) {
inGame = false;
}
if (x[0] < 0) {
inGame = false;
}
if (!inGame) {
timer.stop();
}
}
private void locateApple() {
int r = (int) (Math.random() * RAND_POS);
apple_x = r * DOT_SIZE;
r = (int) (Math.random() * RAND_POS);
apple_y = r * DOT_SIZE;
}
@Override
public void actionPerformed(ActionEvent e) {
if (!isPaused) {
if (inGame) {
checkApple();
checkCollision();
move();
}
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;
}
if (key == KeyEvent.VK_SPACE) {
isPaused = !isPaused;
}
if (key == KeyEvent.VK_SHIFT) {
isSpeedUp = true;
if (inGame) {
timer.setDelay(SPEED_UP_DELAY);
}
}
}
@Override
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_SHIFT) {
isSpeedUp = false;
if (inGame) {
timer.setDelay(DELAY);
}
}
}
@Override
public void keyTyped(KeyEvent e) {
}
public static void main(String[] args) {
JFrame frame = new JFrame("Snake Game");
frame.getContentPane().add(new SnakeGame());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
```
在这个实现中,我们添加了三个功能:
1. “开始”功能:在`initGame()`方法中,我们初始化了游戏,并在`Timer`对象上调用`start()`方法来启动游戏循环。在`main()`方法中,我们创建`JFrame`对象并将`SnakeGame`对象添加到其中,并将其设置为可见。
2. “暂停”功能:我们使用`isPaused`变量来跟踪游戏当前是否处于暂停状态。在`keyPressed()`方法中,我们检查用户是否按下空格键。如果是,我们将`isPaused`变量取反,以切换游戏状态。在`doDrawing()`方法中,如果游戏被暂停,我们将显示“PAUSED”消息。
3. “加速”功能:我们使用`isSpeedUp`变量来跟踪游戏当前是否处于加速状态。在`keyPressed()`方法中,我们检查用户是否按下Shift键。如果是,我们将`isSpeedUp`变量设置为true,并将`Timer`对象的延迟设置为`SPEED_UP_DELAY`。在`keyReleased()`方法中,我们将`isSpeedUp`变量设置为false,并将`Timer`对象的延迟设置为`DELAY`。
希望这可以帮助您实现Java贪吃蛇游戏,并添加“开始”、“暂停”、“加速”三个额外功能。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)