时间:2025-03-11 17:50
人气:
作者:admin
生成游戏网格
坐标系定义
网格状态定义
enum GridStatus {
notUse,
snakeHead,
snakeBody,
food,
}
generateGrids(){
......
for (let rowIndex=0; rowIndex<this.row; rowIndex++){
this.gridList.push([])
for (let columnIndex=0; columnIndex<this.column; columnIndex++){
let gridData: GridDataInterface = {
name: `Grid-${rowIndex}-${columnIndex}`,
status: GridStatus.notUse,
gridNode: null,
row: rowIndex,
column: columnIndex,
}
this.gridList[rowIndex].push(gridData)
// 生成网格UI节点
......
}
}
}
蛇的初始化
enum Direction {
UP,
DOWN,
LEFT,
RIGHT
}
public currentDirection = Direction.LEFT
public nextDirection = Direction.LEFT
定义蛇的初始位置:行数/2取整、列数/2取整,得到网格居中位置。
修改居中位置网格数据,把网格状态改为蛇头,而当前移动方向的反方向设置一定数量的网格状态为蛇身。
generateSnake(){
// 起始位置
const startRow = Math.floor(this.row / 2)
const startColumn = Math.floor(this.column / 2)
// 蛇头
const headGridData = this.gridList[startRow][startColumn]
headGridData.status = GridStatus.snakeHead
this.snakeGridList.push(headGridData)
// 蛇身
for (
let columnIndex=startColumn+1;
columnIndex<=startColumn+this.startBodyNum;
columnIndex++
){
const nowGridData = this.gridList[startRow][columnIndex]
nowGridData.status = GridStatus.snakeBody
this.snakeGridList.push(nowGridData)
}
// 更新UI显示
this.updateGridsUI()
}
蛇朝一个方向移动
点击开始游戏后,搞一个定时器,如每0.5秒调用一次,作为蛇的移动回调。
下次移动方向为向左,那么就让蛇头的列数-1,如果方向往上则让蛇头的行数+1,可得到蛇头新的网格位置。
有了新的蛇头网格位置后,就可以让蛇尾巴网格数据换到新蛇头网格位置,巧妙的实现蛇的移动。
snakeMove(){
if (this.gameStatus !== GameStatus.running) return
const move = {row: 0, column: 0}
if (this.nextDirection === Direction.LEFT){
move.column = -1
}
else if (this.nextDirection === Direction.RIGHT){
move.column = 1
}
else if (this.nextDirection === Direction.UP){
move.row = 1
}
else if (this.nextDirection === Direction.DOWN){
move.row = -1
}
const headGridData = this.snakeGridList[0]
const newHeadGridData = this.gridList[headGridData.row+move.row][headGridData.column+move.column]
// 移动
headGridData.status = GridStatus.snakeBody
newHeadGridData.status = GridStatus.snakeHead
this.snakeGridList.unshift(newHeadGridData)
const tailGridData = this.snakeGridList.pop()
tailGridData.status = GridStatus.notUse
// 更新UI显示
this.updateGridsUI()
// 下次移动
this.snakeMoveSchedule()
}
蛇改变方向移动
点击上、下、左、右按键时,记录蛇的下次移动方向。
下次移动方向的限制:不能反方向移动。如蛇的当前移动方向是往左,那么下次移动方向就不能往右。
onBtnClick(event: Event, key: string){
if (key === 'begin' && this.gameStatus === GameStatus.ready){
this.readDifficulty()
this.setGameStauts(GameStatus.running)
this.generateSnake()
this.generateFood()
this.snakeMoveSchedule()
}
......
// 移动方向更改
else if (key === 'left' && this.gameStatus === GameStatus.running && this.currentDirection !== Direction.RIGHT){
this.nextDirection = Direction.LEFT
}
else if (key === 'right' && this.gameStatus === GameStatus.running && this.currentDirection !== Direction.LEFT){
this.nextDirection = Direction.RIGHT
}
else if (key === 'up' && this.gameStatus === GameStatus.running && this.currentDirection !== Direction.DOWN){
this.nextDirection = Direction.UP
}
else if (key === 'down' && this.gameStatus === GameStatus.running && this.currentDirection !== Direction.UP){
this.nextDirection = Direction.DOWN
}
}
生成食物
点击开始游戏后,在生成蛇后再生成食物。
食物的位置是一个随机位置,遍历网格数据提取出未使用的网格数据,随机一个网格数据,让网格状态改为食物即可。
蛇吃食物
游戏结束
游戏成功
加速功能
当长按加速按钮时,使蛇的移动速度加快。
只需在点击加速时更改蛇移动回调的间隔时间,缩短即可实现加速。
获取完整小游戏源码