如何实现贪吃蛇用空格控制开始和暂停,给我JAVA示例代码
时间: 2023-06-08 18:09:18 浏览: 153
好的,我会为您展示一个简单的贪吃蛇游戏,实现了用空格控制开始和暂停的功能,在Java中的示例代码如下:
```java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
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 = 300;
private final int HEIGHT = 300;
private final int CELL_SIZE = 10;
private final int TOTAL_CELLS = (WIDTH * HEIGHT) / (CELL_SIZE * CELL_SIZE);
private boolean gameRunning = false;
private boolean gamePaused = false;
private Timer timer;
private int[] x = new int[TOTAL_CELLS];
private int[] y = new int[TOTAL_CELLS];
private int bodyParts = 6;
private int appleX;
private int appleY;
private char direction = 'R';
public SnakeGame() {
addKeyListener(this);
setBackground(Color.BLACK);
setFocusable(true);
setPreferredSize(new Dimension(WIDTH, HEIGHT));
initGame();
}
public void initGame() {
gameRunning = true;
gamePaused = false;
direction = 'R';
timer = new Timer(50, this);
timer.start();
generateApple();
}
public void generateApple() {
int r = (int) (Math.random() * (WIDTH - CELL_SIZE));
appleX = r % CELL_SIZE == 0 ? r : (r / CELL_SIZE) * CELL_SIZE;
r = (int) (Math.random() * (HEIGHT - CELL_SIZE));
appleY = r % CELL_SIZE == 0 ? r : (r / CELL_SIZE) * CELL_SIZE;
}
public void checkCollision() {
for (int i = bodyParts; i > 0; i--) {
if ((x[0] == x[i] && y[0] == y[i]) || x[0] < 0 || x[0] > WIDTH - CELL_SIZE || y[0] < 0 || y[0] > HEIGHT - CELL_SIZE) {
gameRunning = false;
}
}
if (x[0] == appleX && y[0] == appleY) {
bodyParts++;
generateApple();
}
}
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] -= CELL_SIZE;
break;
case 'D':
y[0] += CELL_SIZE;
break;
case 'L':
x[0] -= CELL_SIZE;
case 'R':
x[0] += CELL_SIZE;
break;
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
drawSnake(g);
drawApple(g);
}
public void drawSnake(Graphics g) {
g.setColor(Color.GREEN);
for (int i = 0; i < bodyParts; i++) {
g.fillRect(x[i], y[i], CELL_SIZE, CELL_SIZE);
}
}
public void drawApple(Graphics g) {
g.setColor(Color.RED);
g.fillRect(appleX, appleY, CELL_SIZE, CELL_SIZE);
}
public void actionPerformed(ActionEvent e) {
if (gameRunning && !gamePaused) {
checkCollision();
move();
}
repaint();
}
public void keyPressed(KeyEvent e) {
if (!gameRunning) {
initGame();
}
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
gamePaused = !gamePaused;
} else if ((e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_W) && direction != 'D') {
direction = 'U';
} else if ((e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyCode() == KeyEvent.VK_S) && direction != 'U') {
direction = 'D';
} else if ((e.getKeyCode() == KeyEvent.VK_LEFT || e.getKeyCode() == KeyEvent.VK_A) && direction != 'R') {
direction = 'L';
} else if ((e.getKeyCode() == KeyEvent.VK_RIGHT || e.getKeyCode() == KeyEvent.VK_D) && direction != 'L') {
direction = 'R';
}
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
public static void main(String[] args) {
JFrame frame = new JFrame("Snake Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new SnakeGame());
frame.pack();
frame.setVisible(true);
}
}
```
这个示例中,空格键可以用来控制游戏的开始和暂停,其他方向键用来控制蛇的运动方向。当蛇吃到苹果时,会增长一个身体部位,每吃到一个苹果都会重新生成一个新的苹果。游戏会在蛇头触碰到游戏边界或者蛇身时结束。