159.261 Gaming Programming 游戏编程
文字笔记记录于此,代码请见 仓库
要求
- 文件名小写
- 标题首字母大写
- 所需文件以Onedrive直链接形式嵌入
This is the multi-page printable view of this section. Click here to print.
by Hamhuo
This project is a classic Snake game implemented using Java AWT and a custom GameEngine.
The player controls a snake moving on a 25×25 grid, growing longer by eating apples.
The game ends if the snake hits the wall or collides with itself.
if ((direction.getValue() ^ d.getValue()) != 2) {
this.direction = d;
}
Before starting the game, a settings dialog allows players to customize:
public void init(int length, Direction d, Double s) {
speed = 1 / s;
// Set the snake's initial length
initLength = length;
// Initialize the snake's body as an ArrayList of Points
snakeBody = new ArrayList<>();
// Add points to the snake's body, starting at position (3, 5)
for (int i = 3; i < initLength + 3; i++) {
snakeBody.add(new Point(i, 5)); // Add each segment of the snake to the body
}
// Set the snake's head to be the first point in the body
snakeHead = snakeBody.get(0);
direction = d;
}
Separate sprite images are used for:
Snake
class handles snake logic (movement, growth, shrink).MainGame
class manages overall game state, rendering, and input.R
after game over will reset and restart the game.apple.png
, dot.png
, head.png
) are correctly placed under src/main/resources/
.MainGame.java
.本指南帮助用户为《Slapocalypse》游戏创建自定义地图。以下是地图中使用的障碍物贴图类型及其对应的图像。每个贴图编号对应 GameController.java
中的障碍物类型,用于在地图文件(如 map5.txt
)中指定地形。地板贴图(编号 0)由系统随机选择 floorTile1
或 floorTile2
,此处仅列出障碍物贴图(1-10)。
地图使用以下贴图编号(1-10)表示障碍物(如墙壁、连接器、角落)。请在地图文件中使用这些编号来设计布局。
描述:垂直方向的墙壁,用于分隔区域,阻挡移动。
描述:水平方向的墙壁下方房间,用于分隔区域,阻挡移动。
描述:水平墙的左侧端点,用于墙壁的起始点。
描述:连接墙壁的右侧部分,通常用于墙壁的延续或连接。
描述:墙壁的右上角,用于角落转折。
描述:垂直墙(基于1号贴图旋转90度),用于特定方向的阻挡。
描述:连接墙壁的左侧部分(基于4号贴图旋转180度)。
描述:墙壁的右下角(基于5号贴图旋转90度)。
描述:墙壁的左下角(基于5号贴图旋转180度)。
描述:墙壁的左上角(基于5号贴图旋转270度)。
以下表格总结了障碍物贴图的编号和描述,供快速参考:
贴图编号 | 描述 |
---|---|
1 | 垂直墙 |
2 | 水平墙下方 |
3 | 水平墙端点(左侧) |
4 | 连接器(右侧) |
5 | 右上角 |
6 | 水平墙(旋转90度) |
7 | 连接器(左侧) |
8 | 右下角 |
9 | 左下角 |
10 | 左上角 |
src/main/resources/map5.txt
)是一个文本文件,表示地图的网格布局。0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 1 2 3 4 5 0
0 1 2 3 4 5 0
0 1 2 3 4 5 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0