贪吃蛇Java运行源代码
时间: 2023-07-31 18:09:00 浏览: 242
以下是一个使用 Java Swing 编写的贪吃蛇小游戏的源代码示例:
```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.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class GamePanel extends JPanel implements ActionListener, KeyListener {
private static final long serialVersionUID = 1L;
public static final int WIDTH = 600;
public static final int HEIGHT = 600;
public static final int UNIT_SIZE = 20;
public static final int GAME_UNITS = (WIDTH * HEIGHT) / (UNIT_SIZE * UNIT_SIZE);
public static final int DELAY = 75;
public static final int[] X_OFFSET = { 0, 0, -1, 1 };
public static final int[] Y_OFFSET = { -1, 1, 0, 0 };
private ArrayList<Point> snake;
private Point food;
private int score;
private boolean running;
private int direction;
private Timer timer;
private Random random;
public GamePanel() {
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setBackground(Color.black);
setFocusable(true);
addKeyListener(this);
initGame();
}
private void initGame() {
snake = new ArrayList<Point>();
addSnakeUnit(new Point(WIDTH / 2, HEIGHT / 2));
addSnakeUnit(new Point(WIDTH / 2, HEIGHT / 2 + UNIT_SIZE));
addSnakeUnit(new Point(WIDTH / 2, HEIGHT / 2 + 2 * UNIT_SIZE));
food = generateRandomFood();
score = 0;
running = true;
direction = 0;
timer = new Timer(DELAY, this);
timer.start();
random = new Random();
}
private Point generateRandomFood() {
int x = random.nextInt(WIDTH / UNIT_SIZE) * UNIT_SIZE;
int y = random.nextInt(HEIGHT / UNIT_SIZE) * UNIT_SIZE;
return new Point(x, y);
}
private void addSnakeUnit(Point p) {
snake.add(p);
}
private void removeSnakeUnit(Point p) {
snake.remove(p);
}
private void moveSnake() {
Point head = snake.get(0);
int newX = head.x + X_OFFSET[direction] * UNIT_SIZE;
int newY = head.y + Y_OFFSET[direction] * UNIT_SIZE;
Point newHead = new Point(newX, newY);
if (checkCollision(newHead)) {
endGame();
} else {
addSnakeUnit(newHead);
if (newHead.equals(food)) {
score++;
food = generateRandomFood();
} else {
removeSnakeUnit(snake.get(snake.size() - 1));
}
}
}
private boolean checkCollision(Point p) {
return p.x < 0 || p.x >= WIDTH || p.y < 0 || p.y >= HEIGHT || snake.contains(p);
}
private void endGame() {
running = false;
timer.stop();
}
@Override
public void actionPerformed(ActionEvent e) {
if (running) {
moveSnake();
}
repaint();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
drawSnake(g);
drawFood(g);
drawScore(g);
}
private void drawSnake(Graphics g) {
for (int i = 0; i < snake.size(); i++) {
Point p = snake.get(i);
g.setColor(Color.white);
g.fillRect(p.x, p.y, UNIT_SIZE, UNIT_SIZE);
}
}
private void drawFood(Graphics g) {
g.setColor(Color.red);
g.fillRect(food.x, food.y, UNIT_SIZE, UNIT_SIZE);
}
private void drawScore(Graphics g) {
g.setColor(Color.white);
g.setFont(new Font("Arial", Font.BOLD, 20));
g.drawString("Score: " + score, 20, 30);
}
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT && direction != 1) {
direction = 0;
} else if (key == KeyEvent.VK_RIGHT && direction != 0) {
direction = 1;
} else if (key == KeyEvent.VK_UP && direction != 3) {
direction = 2;
} else if (key == KeyEvent.VK_DOWN && direction != 2) {
direction = 3;
}
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
}
```
这个程序定义了一个 GamePanel 类,它继承自 JPanel,并实现了 ActionListener 和 KeyListener 接口。在 GamePanel 中,我们定义了一些常量,例如窗口的宽度和高度、方格的大小、刷新时间间隔等等。我们还定义了一些变量,例如蛇的坐标、食物的坐标、玩家的得分等等。
在 initGame 方法中,我们初始化了游戏的状态,包括蛇的初始位置、食物的随机位置、得分、运行状态等等。在 moveSnake 方法中,我们实现了蛇的移动逻辑,包括头部的移动、身体的增加与删除、碰撞检测等等。在 endGame 方法中,我们结束了游戏。
在 actionPerformed 方法中,我们在游戏运行时不断刷新界面,在 paintComponent 方法中,我们绘制了蛇、食物、得分等界面元素。在 keyPressed 方法中,我们处理了键盘事件,根据按下的方向键来改变蛇的运动方向。
如果想要运行这个程序,需要将 GamePanel.java 和 SnakeGame.java 两个文件保存到同一个文件夹中,并使用 JDK 编译器将它们编译成 class 文件。然后,在命令行中进入这个文件夹,并执行以下命令:
```
java SnakeGame
```
这样,就可以启动贪吃蛇小游戏了。
阅读全文