用Java编写一个弹窗式贪吃蛇游戏
时间: 2023-08-24 17:05:20 浏览: 55
以下是一个简单的 Java 弹窗式贪吃蛇游戏的代码示例:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SnakeGame extends JFrame implements ActionListener {
private final int WIDTH = 400;
private final int HEIGHT = 430;
private final int DOT_SIZE = 10;
private final int ALL_DOTS = 900;
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 Image ball;
private Image apple;
private Image head;
public SnakeGame() {
setTitle("贪吃蛇游戏");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(WIDTH, HEIGHT);
setResizable(false);
ImageIcon iid = new ImageIcon("dot.png");
ball = iid.getImage();
ImageIcon iia = new ImageIcon("apple.png");
apple = iia.getImage();
ImageIcon iih = new ImageIcon("head.png");
head = iih.getImage();
initGame();
addKeyListener(new TAdapter());
setVisible(true);
}
public void initGame() {
dots = 3;
for (int i = 0; i < dots; i++) {
x[i] = 50 - i * DOT_SIZE;
y[i] = 50;
}
locateApple();
timer = new Timer(140, this);
timer.start();
}
public void checkApple() {
if ((x[0] == apple_x) && (y[0] == apple_y)) {
dots++;
locateApple();
}
}
public void move() {
for (int i = dots; i > 0; i--) {
x[i] = x[(i - 1)];
y[i] = y[(i - 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;
}
}
public 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();
}
}
public void locateApple() {
int r = (int) (Math.random() * (WIDTH - DOT_SIZE));
apple_x = ((r / DOT_SIZE) * DOT_SIZE);
r = (int) (Math.random() * (HEIGHT - DOT_SIZE));
apple_y = ((r / DOT_SIZE) * DOT_SIZE);
}
public void actionPerformed(ActionEvent e) {
if (inGame) {
checkApple();
checkCollision();
move();
}
repaint();
}
public void paint(Graphics g) {
super.paint(g);
if (inGame) {
g.drawImage(apple, apple_x, apple_y, this);
for (int i = 0; i < dots; i++) {
if (i == 0) {
g.drawImage(head, x[i], y[i], this);
} else {
g.drawImage(ball, x[i], y[i], this);
}
}
Toolkit.getDefaultToolkit().sync();
} else {
gameOver(g);
}
}
public void gameOver(Graphics g) {
String msg = "Game Over!";
Font small = new Font("Helvetica", Font.BOLD, 14);
FontMetrics metr = getFontMetrics(small);
g.setColor(Color.white);
g.setFont(small);
g.drawString(msg, (WIDTH - metr.stringWidth(msg)) / 2, HEIGHT / 2);
}
private class TAdapter extends KeyAdapter {
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;
}
}
}
public static void main(String[] args) {
new SnakeGame();
}
}
```
该代码使用了 Java 的 Swing 组件和 AWT 绘图工具,可以直接在图形界面上运行。贪吃蛇的行动由定时器控制,用户通过键盘方向键控制贪吃蛇的移动方向。当贪吃蛇吃到苹果时,长度增加一格,游戏继续进行;当贪吃蛇碰到边界或自身时,游戏结束。