利用java写一个贪吃蛇游戏并具有暂停功能
时间: 2023-12-20 21:07:06 浏览: 28
好的,以下是一个简单的贪吃蛇游戏的Java代码,同时也实现了暂停功能。你可以参考一下:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SnakeGame extends JPanel implements ActionListener, KeyListener {
private static final int WIDTH = 300;
private static final int HEIGHT = 300;
private static final int DOT_SIZE = 10;
private static final int ALL_DOTS = 900;
private static final int RAND_POS = 29;
private static final int DELAY = 140;
private int[] x = new int[ALL_DOTS];
private int[] y = new int[ALL_DOTS];
private int dots;
private int apple_x;
private int apple_y;
private Timer timer;
private boolean inGame = true;
private boolean paused = false;
private char direction = 'R';
public SnakeGame() {
addKeyListener(this);
setBackground(Color.black);
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setFocusable(true);
initGame();
}
private void initGame() {
dots = 3;
for (int i = 0; i < dots; i++) {
x[i] = 50 - i * DOT_SIZE;
y[i] = 50;
}
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 i = 0; i < dots; i++) {
if (i == 0) {
g.setColor(Color.green);
} else {
g.setColor(Color.white);
}
g.fillRect(x[i], y[i], DOT_SIZE, DOT_SIZE);
}
Toolkit.getDefaultToolkit().sync();
} else {
gameOver(g);
}
}
private void gameOver(Graphics g) {
String msg = "Game Over";
Font font = new Font("Helvetica", Font.BOLD, 14);
FontMetrics metr = getFontMetrics(font);
g.setColor(Color.white);
g.setFont(font);
g.drawString(msg, (WIDTH - metr.stringWidth(msg)) / 2, HEIGHT / 2);
}
private void checkApple() {
if ((x[0] == apple_x) && (y[0] == apple_y)) {
dots++;
locateApple();
}
}
private void move() {
for (int i = dots; i > 0; i--) {
x[i] = x[(i - 1)];
y[i] = y[(i - 1)];
}
switch (direction) {
case 'U':
y[0] -= DOT_SIZE;
break;
case 'D':
y[0] += DOT_SIZE;
break;
case 'L':
x[0] -= DOT_SIZE;
break;
case 'R':
x[0] += DOT_SIZE;
break;
}
}
private void checkCollision() {
for (int i = dots; i > 0; i--) {
if ((i > 4) && (x[0] == x[i]) && (y[0] == y[i])) {
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 (!paused) {
if (inGame) {
checkApple();
checkCollision();
move();
}
repaint();
}
}
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if ((key == KeyEvent.VK_LEFT) && (direction != 'R')) {
direction = 'L';
}
if ((key == KeyEvent.VK_RIGHT) && (direction != 'L')) {
direction = 'R';
}
if ((key == KeyEvent.VK_UP) && (direction != 'D')) {
direction = 'U';
}
if ((key == KeyEvent.VK_DOWN) && (direction != 'U')) {
direction = 'D';
}
if (key == KeyEvent.VK_SPACE) {
paused = !paused;
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
public static void main(String[] args) {
JFrame frame = new JFrame("Snake");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SnakeGame game = new SnakeGame();
frame.add(game);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
```
在这个代码中,我们使用了Java的Swing库来绘制游戏窗口,并实现了贪吃蛇游戏的逻辑。同时,我们在keyPressed方法中检测了空格键,以实现暂停功能。