From d86b3b08f4d8cebe83e2c3281026fc721abff1b5 Mon Sep 17 00:00:00 2001 From: huangyuhao Date: Fri, 15 Nov 2024 09:56:59 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=BA=90=E7=A0=81src=E5=92=8Cmoon=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/main.mbt" | 255 ++++++++++++++++++ .../src/main/moon.pkg.json" | 21 ++ 2 files changed, 276 insertions(+) create mode 100644 "teams/\345\257\273\346\211\276\346\226\260\345\244\247\351\231\206/src/main/main.mbt" create mode 100644 "teams/\345\257\273\346\211\276\346\226\260\345\244\247\351\231\206/src/main/moon.pkg.json" diff --git "a/teams/\345\257\273\346\211\276\346\226\260\345\244\247\351\231\206/src/main/main.mbt" "b/teams/\345\257\273\346\211\276\346\226\260\345\244\247\351\231\206/src/main/main.mbt" new file mode 100644 index 0000000..4215b31 --- /dev/null +++ "b/teams/\345\257\273\346\211\276\346\226\260\345\244\247\351\231\206/src/main/main.mbt" @@ -0,0 +1,255 @@ +// fly bird game +struct BirdModel { //bird + mut x : Int + mut y : Int + width : Int + height : Int + mut birdVelocity : Int //y轴向下方向速度 +} + +struct PipesItem { // 管道 + mut x : Int + top : Int + bottom : Int + width : Int +} + +struct GameStateModel { //游戏状态 + mut score : Int + mut gameTitle : String + mut isGameOver : Bool +} + +struct CoinModel{ // 金币 +10分 + mut x : Int + y : Int + width : Int + height : Int + mut isGet:Bool //是否获取到 +} + +let gravity = 1 //默认重力 + +let bird : BirdModel = { //bird初始化 + x: 10, + y: 0, + width: 15, + height: 15, + birdVelocity: 1 / 2, +} + +let pipes : Array[PipesItem] = [{ x: 160, top: 60, bottom: 40, width: 15 }] // 管道初始化 + +let coins:Array[CoinModel]=[{x:200,y:80,width:10,height:10,isGet:false}] //金币道具初始化 + +let pipesSpeed = 1 // 障碍间隔速度 + +let pipeGap = 60 // 上下障碍之间的空间 + +let gameState : GameStateModel = { // 游戏状态初始化 + score: 0, + gameTitle: "start", + isGameOver: false, +} + +let random : @random.Rand = @random.new() + +let screen_size = 160 + +//绘制bird +pub fn drawBird() -> Unit { + @wasm4.set_draw_colors(2) + let blit_flag : @wasm4.BlitFlag = { + one_bit_per_pixel: true, + flip_x: false, + flip_y:false, + rotate:false, + } + @wasm4.blit( + @wasm4.sprite(b"\xff\xff\x7f\x87\x3f\x00\x3f\x30\x0f\x33\x03\x03\x00\x03\x80\x03\x80\x07\x80\x07\x80\x0f\xc0\x1f\xe0\x3f\x00\x3f\x80\xff\xff\xff"), + bird.x, + bird.y, + 16, + 16, + blit_flag, + ) +} + +//控制bird +pub fn controlBird() -> Unit { + if @wasm4.get_gamepad(index=1).button_right && bird.x < 160 { + bird.x += 1 + } else if @wasm4.get_gamepad(index=1).button_down && bird.y < 160 { + bird.y += 3 + } else if @wasm4.get_gamepad(index=1).button_left && bird.x >= 0 { + bird.x -= 1 + } else if @wasm4.get_gamepad(index=1).button_up && bird.y >= 0 { + bird.y -= 3 + } + drawBird() +} + +// 更新bird +pub fn updateBird() -> Unit { + bird.y += gravity + if bird.y + bird.height > screen_size { + gameState.gameTitle = "gameOver" + } else if bird.y < 0 { + bird.y = screen_size / 2 + bird.birdVelocity = 0 + } +} + + +//绘制障碍物 +pub fn drawPipes() -> Unit { + for pipe in pipes { + //上方柱子 + @wasm4.set_draw_colors(3) + @wasm4.rect(pipe.x, 0, pipe.width, pipe.top) + + // 底部柱子 + @wasm4.set_draw_colors(3) + @wasm4.rect(pipe.x, screen_size - pipe.bottom, pipe.width, pipe.bottom) + + + } +} + +// 循环生成障碍物 +pub fn addPipe() -> Unit { + let gapTop = random.int(limit=10) * (screen_size - pipeGap) / 10 + let gapBottom = screen_size - gapTop - pipeGap + pipes.push({ x: 160, top: gapTop, bottom: gapBottom, width: 15 }) +} + +// 障碍物坐标更新 +pub fn updatePipes() -> Unit { + for pipe in pipes { + if(gameState.score>=30){ // 当得分超过30时,升级难度,管道移动速度加快 + pipe.x -= pipesSpeed+1 + }else{ + pipe.x -= pipesSpeed + } + if pipe.x + pipe.width < 0 { + gameState.score += 1 + let a = pipes.pop() + addPipe() + } + } +} + + +//绘制金币 获取到金币,得分+10 +pub fn drawCoin()->Unit{ + for coin in coins { + @wasm4.set_draw_colors(2) + @wasm4.oval(coin.x,coin.y,10,10) + } +} + +// 循环生成金币 +pub fn addCoin() -> Unit { + let gapTop = random.int(limit=10) * (screen_size - pipeGap) / 10 + coins.push({x:200,y:gapTop+30, width:10,height:10,isGet:false}) +} + +// 金币坐标更新 +pub fn updateCoins() -> Unit { + for index,coin in coins { + coin.x -= pipesSpeed + + if(coin.isGet){ + gameState.score += 10 + let b=coins.remove(index) + } + + if coin.x + coin.width < 0 { + let c=coins.remove(index) + addCoin() + } + } +} + + +// 检查碰撞 +pub fn checkCollisions() -> Unit { + for pipe in pipes { + // Check if the bird is within the horizontal bounds of the pipe + if bird.x < pipe.x + pipe.width && bird.x + bird.width > pipe.x { + // Check if the bird is within the vertical bounds of the top or bottom part of the pipe + if bird.y < pipe.top || bird.y + bird.width > screen_size - pipe.bottom { + // 添加音效 + @wasm4.tone( + (2000, 0), + @wasm4.ADSR::new(5), + @wasm4.ADSRVolume::new(100), + @wasm4.ToneFlag::new(), + ) + gameOver() + } + } + } +} + +pub fn getCoin() -> Unit { + for index,coin in coins{ + if(bird.xcoin.x && bird.ycoin.y){ + coin.isGet=true + // 获得金币,添加音效 + @wasm4.tone( + (2000, 0), + @wasm4.ADSR::new(5), + @wasm4.ADSRVolume::new(100), + @wasm4.ToneFlag::new(), + ) + }else{ + coin.isGet=false + } + } +} + +pub fn gameOver()->Unit{ + gameState.gameTitle = "gameover" + gameState.isGameOver = true + @wasm4.set_draw_colors(4) + @wasm4.text("Game Over!", 40, 80) +} + +pub fn drawScore() -> Unit { + @wasm4.set_draw_colors(4) + @wasm4.text("score: "+gameState.score.to_string(), 50, 0) +} + + +pub fn start() -> Unit { + @wasm4.set_palette(1, @wasm4.rgb(0xE0F8CF)) //背景 天空色 6fc5cc + @wasm4.set_palette(2, @wasm4.rgb(0x86C06C)) // 小鸟 //黄色 E5AD20 + @wasm4.set_palette(3, @wasm4.rgb(0x86C06C)) // 柱子 绿色 + @wasm4.set_palette(4, @wasm4.rgb(0x666666)) //默认背景色 + +} + +// 游戏实时更新 每秒60 的刷新频次 +pub fn update() -> Unit { + if(gameState.isGameOver==false){ + updateBird() + controlBird() + // 绘制障碍物 + drawPipes() + drawScore() + updatePipes() + + //绘制金币 + drawCoin() + updateCoins() + + // 检查碰撞 + getCoin() + checkCollisions() + }else{ + drawScore() + gameOver() + } +} + diff --git "a/teams/\345\257\273\346\211\276\346\226\260\345\244\247\351\231\206/src/main/moon.pkg.json" "b/teams/\345\257\273\346\211\276\346\226\260\345\244\247\351\231\206/src/main/moon.pkg.json" new file mode 100644 index 0000000..a8606e6 --- /dev/null +++ "b/teams/\345\257\273\346\211\276\346\226\260\345\244\247\351\231\206/src/main/moon.pkg.json" @@ -0,0 +1,21 @@ +{ + // "is-main": true, + "import": ["moonbitlang/wasm4"], + "link": { + "wasm-gc": { + "exports": ["start", "update"], + "import-memory": { + "module": "env", + "name": "memory" + } + }, + "wasm": { + "exports": ["start", "update"], + "import-memory": { + "module": "env", + "name": "memory" + }, + "heap-start-address": 6590 + } + } +} From 0f7d10b0029e8fa82aeda44e77af4206da5fa2a5 Mon Sep 17 00:00:00 2001 From: huangyuhao Date: Fri, 15 Nov 2024 10:03:38 +0800 Subject: [PATCH 2/2] --- .../moon_version.txt" | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 "teams/\345\257\273\346\211\276\346\226\260\345\244\247\351\231\206/moon_version.txt" diff --git "a/teams/\345\257\273\346\211\276\346\226\260\345\244\247\351\231\206/moon_version.txt" "b/teams/\345\257\273\346\211\276\346\226\260\345\244\247\351\231\206/moon_version.txt" new file mode 100644 index 0000000..00c01f3 --- /dev/null +++ "b/teams/\345\257\273\346\211\276\346\226\260\345\244\247\351\231\206/moon_version.txt" @@ -0,0 +1,3 @@ +moon 0.1.20241106 (79e45ae 2024-11-06) ~\.moon\bin\moon.exe +moonc v0.1.20241106+8f17a3fc7 ~\.moon\bin\moonc.exe +moonrun 0.1.20241106 (79e45ae 2024-11-06) ~\.moon\bin\moonrun.exe \ No newline at end of file