|
| 1 | + |
| 2 | +let maze : Array[Array[Int]]= [ |
| 3 | + // 第一关地图 16*16 地图 |
| 4 | + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], |
| 5 | + [1, 2, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1], |
| 6 | + [1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1], |
| 7 | + [1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1], |
| 8 | + [1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1], |
| 9 | + [1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1], |
| 10 | + [1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1], |
| 11 | + [1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1], |
| 12 | + [1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1], |
| 13 | + [1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1], |
| 14 | + [1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1], |
| 15 | + [1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1], |
| 16 | + [1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1], |
| 17 | + [1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1], |
| 18 | + [1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1], |
| 19 | + [1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1], |
| 20 | + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], |
| 21 | + ] |
| 22 | + |
| 23 | + |
| 24 | +let tile_size = 6 //瓦片尺寸 |
| 25 | + |
| 26 | +// 玩家坐标——类型 |
| 27 | +struct playerModel { |
| 28 | + mut x : Int |
| 29 | + mut y : Int |
| 30 | +} |
| 31 | + |
| 32 | +//游戏状态-类型 |
| 33 | +struct gameStateModel { |
| 34 | + mut isGameOver : Bool |
| 35 | +} |
| 36 | + |
| 37 | +// 当前关卡-类型 |
| 38 | +struct currentLevelModle { |
| 39 | + data : Array[Array[Int]] // 当前关卡地图数据 |
| 40 | + mut levelCompleted : Bool //当前关卡是否完成标志 |
| 41 | +} |
| 42 | + |
| 43 | +// 当前关卡 |
| 44 | +let currentLevel : currentLevelModle = { |
| 45 | + data: maze.copy(), |
| 46 | + levelCompleted: false, |
| 47 | +} |
| 48 | + |
| 49 | +// 玩家 |
| 50 | +let player : playerModel = { x: 1, y: 1 } |
| 51 | + |
| 52 | +// 游戏状态 |
| 53 | +let gameState : gameStateModel = { isGameOver: false} |
| 54 | + |
| 55 | +// 加载当前关卡 |
| 56 | +pub fn loadLevel() -> Unit { |
| 57 | + player.x = findPlayerIndex(currentLevel.data)[0] |
| 58 | + player.y = findPlayerIndex(currentLevel.data)[1] |
| 59 | + gameState.isGameOver = false |
| 60 | + currentLevel.levelCompleted = false |
| 61 | + drawLevel() |
| 62 | +} |
| 63 | + |
| 64 | +// 绘制关卡 |
| 65 | +pub fn drawLevel() -> Unit { |
| 66 | + for y = 0; y < currentLevel.data.length(); y = y + 1 { |
| 67 | + for x = 0; x < currentLevel.data[y].length(); x = x + 1 { |
| 68 | + let tile = currentLevel.data[y][x] |
| 69 | + drawTile(x, y, tile) |
| 70 | + } |
| 71 | + } |
| 72 | + // 如果游戏结束,显示胜利信息 |
| 73 | + // if (gameOver) { |
| 74 | + // ctx.fillStyle = '#00FF00'; // 绿色胜利信息 |
| 75 | + // ctx.font = '24px Arial'; |
| 76 | + // ctx.fillText('You Win!', canvas.width / 2 - 50, canvas.height / 2); |
| 77 | + // } |
| 78 | +} |
| 79 | + |
| 80 | +//绘制单个瓦片 |
| 81 | +pub fn drawTile(x : Int, y : Int, tile : Int) -> Unit { |
| 82 | + if tile == 0 { |
| 83 | + @wasm4.set_draw_colors(1) |
| 84 | + @wasm4.rect(x * tile_size, y * tile_size, tile_size, tile_size) |
| 85 | + } else if tile == 1 { |
| 86 | + @wasm4.set_draw_colors(2) |
| 87 | + @wasm4.rect( |
| 88 | + x * tile_size, |
| 89 | + y * tile_size, |
| 90 | + tile_size , |
| 91 | + tile_size , |
| 92 | + ) |
| 93 | + } else if tile == 2 { |
| 94 | + @wasm4.set_draw_colors(3) |
| 95 | + @wasm4.rect( |
| 96 | + x * tile_size, |
| 97 | + y * tile_size, |
| 98 | + tile_size , |
| 99 | + tile_size , |
| 100 | + ) |
| 101 | + }else if tile == 3 { |
| 102 | + @wasm4.set_draw_colors(4) |
| 103 | + @wasm4.rect(x * tile_size, y * tile_size, tile_size, tile_size) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +// 在地图上查找玩家的初始位置 |
| 108 | +pub fn findPlayerIndex(level : Array[Array[Int]]) -> Array[Int] { |
| 109 | + for y = 0; y < level.length(); y = y + 1 { |
| 110 | + for x = 0; x < level[y].length(); x = x + 1 { |
| 111 | + if level[y][x] == 2 { |
| 112 | + return [x, y] |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + return [-1, -1] // 如果没有找到玩家,返回[-1, -1](理论上不应该发生) |
| 117 | +} |
| 118 | + |
| 119 | +// 移动玩家 (0,1) |
| 120 | +pub fn movePlayer(dx : Int, dy : Int) -> Unit { |
| 121 | + let mut newX = 0 |
| 122 | + let mut newY = 0 |
| 123 | + |
| 124 | + newX = (player.x + dx) |
| 125 | + newY = (player.y + dy) |
| 126 | + if newX >= 0 && |
| 127 | + newX < currentLevel.data.length() && |
| 128 | + newY >= 0 && |
| 129 | + newY < currentLevel.data.length() { |
| 130 | + let tile = currentLevel.data[newY][newX] |
| 131 | + if tile == 0 { |
| 132 | + currentLevel.data[player.y][player.x] = 0 |
| 133 | + currentLevel.data[newY][newX] = 2 |
| 134 | + player.x = newX |
| 135 | + player.y = newY |
| 136 | + } else if tile == 3 { |
| 137 | + currentLevel.data[player.y][player.x] = 0 |
| 138 | + currentLevel.data[newY][newX] = 3 |
| 139 | + player.x = newX |
| 140 | + player.y = newY |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + // 检查胜利条件 |
| 145 | + if checkWin() { |
| 146 | + gameState.isGameOver = true |
| 147 | + @wasm4.text("Game Victory!", 30, 120) |
| 148 | + @wasm4.tone( |
| 149 | + (2000, 0), |
| 150 | + @wasm4.ADSR::new(5), |
| 151 | + @wasm4.ADSRVolume::new(100), |
| 152 | + @wasm4.ToneFlag::new(), |
| 153 | + ) |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +// 检查是否胜利 |
| 158 | + |
| 159 | +pub fn checkWin()-> Bool { |
| 160 | + if(player.x == -1 && player.y == -1) { |
| 161 | + currentLevel.levelCompleted = true |
| 162 | + return true |
| 163 | + } |
| 164 | + return false |
| 165 | +} |
| 166 | + |
| 167 | + |
| 168 | +pub fn controlPlayer() -> Unit { |
| 169 | + if gameState.isGameOver == false && currentLevel.levelCompleted == false { |
| 170 | + if @wasm4.get_gamepad().button_right { |
| 171 | + movePlayer(1, 0) |
| 172 | + } else if @wasm4.get_gamepad().button_down { |
| 173 | + movePlayer(0, 1) |
| 174 | + } else if @wasm4.get_gamepad().button_left { |
| 175 | + movePlayer(-1, 0) |
| 176 | + } else if @wasm4.get_gamepad().button_up { |
| 177 | + movePlayer(0, -1) |
| 178 | + } |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +pub fn start() -> Unit { |
| 183 | + loadLevel() |
| 184 | +} |
| 185 | + |
| 186 | + |
| 187 | +struct Position { |
| 188 | + mut x : Int |
| 189 | + mut y : Int |
| 190 | +} |
| 191 | + |
| 192 | +let pos : Position = { x: 0, y: 0 } |
| 193 | +pub fn update() -> Unit { |
| 194 | + controlPlayer() |
| 195 | + loadLevel() |
| 196 | +} |
0 commit comments