Java贪吃蛇小游戏
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.LinkedList;
import java.util.Random;
public class SnakeGame extends JFrame implements ActionListener, KeyListener {
private static final long serialVersionUID = 1L;
// 游戏区域的格子数和每个格子的大小
private static final int GRID_SIZE = 20;
private static final int CELL_SIZE = 20;
// 蛇的坐标集合,食物的坐标,蛇的移动方向,计时器
private LinkedList snake;
private Point food;
private char direction;
private Timer timer;
// 游戏窗口的构造函数
public SnakeGame() {
setTitle("Snake Game"); // 设置窗口标题
setSize(GRID_SIZE * CELL_SIZE, GRID_SIZE * CELL_SIZE); // 设置窗口大小
setResizable(false); // 设置窗口不可调整大小
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置默认关闭操作
setLocationRelativeTo(null); // 设置窗口居中显示
// 初始化蛇的起始位置,方向为向右
snake = new LinkedList<>();
snake.add(new Point(GRID_SIZE / 2, GRID_SIZE / 2));
direction = 'R';
// 生成初始食物
spawnFood();
// 设置计时器,用于触发蛇的移动
timer = new Timer(100, this);
timer.start();
// 添加键盘事件监听器
addKeyListener(this);
setFocusable(true);
}
// 生成食物的方法
public void spawnFood() {
Random rand = new Random();
int x = rand.nextInt(GRID_SIZE);
int y = rand.nextInt(GRID_SIZE);
food = new Point(x, y);
// 确保食物不会生成在蛇的身体上
while (snake.contains(food)) {
x = rand.nextInt(GRID_SIZE);
y = rand.nextInt(GRID_SIZE);
food.setLocation(x, y);
}
}
// 移动蛇的方法
public void move() {
Point head = snake.getFirst();
Point newHead = new Point(head);
switch (direction) {
case 'U':
newHead.translate(0, -1);
break;
case 'D':
newHead.translate(0, 1);
break;
case 'L':
newHead.translate(-1, 0);
break;
case 'R':
newHead.translate(1, 0);
break;
}
// 检查碰撞
if (newHead.equals(food)) {
snake.addFirst(food);
spawnFood();
} else {
snake.addFirst(newHead);
// 如果蛇没吃到食物,则移除尾部,否则保持尾部
snake.removeLast();
}
// 检查蛇是否撞到自己
if (snake.size() > 1 && snake.subList(1, snake.size()).contains(newHead)) {
gameOver();
}
// 检查蛇是否撞到墙壁
if (newHead.x < 0 || newHead.x >= GRID_SIZE || newHead.y < 0 || newHead.y >= GRID_SIZE) {
gameOver();
}
}
// 游戏结束的方法
public void gameOver() {
timer.stop();
JOptionPane.showMessageDialog(this, "Game Over", "Game Over", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
// 画图的方法,用于绘制蛇、食物和网格
public void paint(Graphics g) {
super.paint(g);
// 绘制蛇
g.setColor(Color.GREEN);
for (Point point : snake) {
g.fillRect(point.x * CELL_SIZE, point.y * CELL_SIZE, CELL_SIZE, CELL_SIZE);
}
// 绘制食物
g.setColor(Color.RED);
g.fillRect(food.x * CELL_SIZE, food.y * CELL_SIZE, CELL_SIZE, CELL_SIZE);
// 绘制网格线
g.setColor(Color.BLACK);
for (int i = 0; i < GRID_SIZE; i++) {
g.drawLine(i * CELL_SIZE, 0, i * CELL_SIZE, GRID_SIZE * CELL_SIZE);
g.drawLine(0, i * CELL_SIZE, GRID_SIZE * CELL_SIZE, i * CELL_SIZE);
}
}
// 主函数,创建SnakeGame对象并显示窗口
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
SnakeGame game = new SnakeGame();
game.setVisible(true);
});
}
// 计时器触发的事件处理
@Override
public void actionPerformed(ActionEvent e) {
move();
repaint();
}
// 键盘按下事件处理
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
if (direction != 'D') direction = 'U';
break;
case KeyEvent.VK_DOWN:
if (direction != 'U') direction = 'D';
break;
case KeyEvent.VK_LEFT:
if (direction != 'R') direction = 'L';
break;
case KeyEvent.VK_RIGHT:
if (direction != 'L') direction = 'R';
break;
}
}
// 未使用的键盘事件处理方法
@Override
public void keyTyped(KeyEvent e) {
}
// 未使用的键盘事件处理方法
@Override
public void keyReleased(KeyEvent e) {
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178