用Java实现一个贪吃蛇小游戏,实现基本的游戏操作
时间: 2023-06-13 20:02:35 浏览: 56
好的,下面是一个简单的贪吃蛇小游戏的Java实现,你可以参考一下:
```java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.LinkedList;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class SnakeGame extends JPanel implements KeyListener {
private static final long serialVersionUID = 1L;
private static final int ROWS = 30; // 行数
private static final int COLS = 30; // 列数
private static final int BLOCK_SIZE = 20; // 方块大小
private static final int INTERVAL = 200; // 刷新间隔时间(毫秒)
private static final int INIT_SNAKE_LEN = 3; // 初始蛇的长度
private static final int INIT_SNAKE_X = 10; // 初始蛇头的x坐标
private static final int INIT_SNAKE_Y = 10; // 初始蛇头的y坐标
private static final int INIT_FOOD_X = 5; // 初始食物的x坐标
private static final int INIT_FOOD_Y = 5; // 初始食物的y坐标
private LinkedList<Point> snake; // 蛇的身体,存储每个方块的坐标
private Point food; // 食物的坐标
private int direction; // 蛇头的运动方向,0:上,1:下,2:左,3:右
private boolean isGameOver; // 游戏是否结束
public SnakeGame() {
snake = new LinkedList<Point>();
for (int i = 0; i < INIT_SNAKE_LEN; i++) {
snake.add(new Point(INIT_SNAKE_X - i, INIT_SNAKE_Y));
}
food = new Point(INIT_FOOD_X, INIT_FOOD_Y);
direction = 3;
isGameOver = false;
JFrame frame = new JFrame("贪吃蛇");
frame.setSize(ROWS * BLOCK_SIZE, COLS * BLOCK_SIZE);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.addKeyListener(this);
frame.add(this);
frame.setVisible(true);
new Thread(new SnakeThread()).start(); // 启动游戏线程
}
/**
* 画出游戏界面
*/
@Override
public void paint(Graphics g) {
super.paint(g);
// 画出蛇的身体
for (Point p : snake) {
g.setColor(Color.GREEN);
g.fillRect(p.x * BLOCK_SIZE, p.y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE);
}
// 画出食物
g.setColor(Color.RED);
g.fillOval(food.x * BLOCK_SIZE, food.y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE);
// 画出边框
g.setColor(Color.BLACK);
for (int i = 0; i <= ROWS; i++) {
g.drawLine(i * BLOCK_SIZE, 0, i * BLOCK_SIZE, COLS * BLOCK_SIZE);
}
for (int i = 0; i <= COLS; i++) {
g.drawLine(0, i * BLOCK_SIZE, ROWS * BLOCK_SIZE, i * BLOCK_SIZE);
}
// 画出游戏结束的提示信息
if (isGameOver) {
g.setColor(Color.RED);
g.drawString("Game Over!", (ROWS * BLOCK_SIZE - 60) / 2, (COLS * BLOCK_SIZE) / 2);
}
}
/**
* 检查游戏是否结束
*/
private void checkGameOver() {
// 蛇头碰到墙壁,游戏结束
if (snake.peekFirst().x < 0 || snake.peekFirst().x >= ROWS
|| snake.peekFirst().y < 0 || snake.peekFirst().y >= COLS) {
isGameOver = true;
return;
}
// 蛇头碰到身体,游戏结束
for (int i = 1; i < snake.size(); i++) {
if (snake.peekFirst().equals(snake.get(i))) {
isGameOver = true;
return;
}
}
isGameOver = false;
}
/**
* 蛇移动
*/
private void move() {
Point head = new Point(snake.peekFirst()); // 蛇头
switch (direction) {
case 0: // 上
head.y--;
break;
case 1: // 下
head.y++;
break;
case 2: // 左
head.x--;
break;
case 3: // 右
head.x++;
break;
}
snake.offerFirst(head); // 把新的蛇头添加到链表头部
if (head.equals(food)) { // 如果吃到了食物
createFood(); // 重新生成食物
} else {
snake.pollLast(); // 把链表尾部的方块弹出,相当于蛇整体向前移动了一格
}
}
/**
* 创建新的食物
*/
private void createFood() {
Random random = new Random();
int x, y;
do {
x = random.nextInt(ROWS);
y = random.nextInt(COLS);
} while (snake.contains(new Point(x, y))); // 如果随机生成的坐标已经被占用了,就重新生成
food.setLocation(x, y);
}
/**
* 游戏线程
*/
private class SnakeThread implements Runnable {
@Override
public void run() {
while (!isGameOver) {
move();
checkGameOver();
repaint(); // 重绘游戏界面
try {
Thread.sleep(INTERVAL); // 等待一段时间
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/**
* 键盘事件
*/
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
if (direction != 1) {
direction = 0;
}
break;
case KeyEvent.VK_DOWN:
if (direction != 0) {
direction = 1;
}
break;
case KeyEvent.VK_LEFT:
if (direction != 3) {
direction = 2;
}
break;
case KeyEvent.VK_RIGHT:
if (direction != 2) {
direction = 3;
}
break;
}
}
@Override
public void keyReleased(KeyEvent e) {}
@Override
public void keyTyped(KeyEvent e) {}
public static void main(String[] args) {
new SnakeGame();
}
}
```
这个程序使用了Java的Swing图形界面库,实现了一个简单的贪吃蛇小游戏。你可以运行一下看看效果。