Skip to content
This repository was archived by the owner on Nov 22, 2024. It is now read-only.

Commit 19d05f3

Browse files
author
chao
committed
更新code
1 parent 4640843 commit 19d05f3

File tree

3 files changed

+169
-0
lines changed

3 files changed

+169
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
moon 0.1.20241106 (79e45ae 2024-11-06) ~\.moon\bin\moon.exe
2+
moonc v0.1.20241106+8f17a3fc7 ~\.moon\bin\moonc.exe
3+
moonrun 0.1.20241106 (79e45ae 2024-11-06) ~\.moon\bin\moonrun.exe
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
// race-car game
2+
3+
struct CarModel { //car
4+
mut x : Int
5+
mut y : Int
6+
width : Int
7+
height : Int
8+
mut isDown:Bool
9+
}
10+
11+
struct ObstacleItem { //障碍
12+
x : Int
13+
mut y : Int
14+
width : Int
15+
height : Int
16+
obstacleSpawnInterval:Int //生成速度 时间间隔 // Time interval in milliseconds
17+
}
18+
19+
struct GameStateModel { // 游戏状态
20+
mut score : Int
21+
mut gameTitle : String
22+
mut isGameOver : Bool
23+
}
24+
25+
let car : CarModel = {
26+
x: 80,
27+
y: 140,
28+
width: 20,
29+
height: 40,
30+
isDown:false
31+
}
32+
33+
let obstacle : Array[ObstacleItem] = [{ x: 160, y: 60, width: 20, height: 40,obstacleSpawnInterval:2000 }] // 管道初始化
34+
35+
let obstacleSpeed = 1
36+
37+
let gameState : GameStateModel = {
38+
score: 0,
39+
gameTitle: "start",
40+
isGameOver: false,
41+
}
42+
43+
let random : @random.Rand = @random.new()
44+
let screen_size = 160
45+
46+
//绘制car
47+
pub fn drawcar() -> Unit {
48+
@wasm4.set_draw_colors(2)
49+
@wasm4.rect(car.x, car.y, 20, 20)
50+
}
51+
52+
//控制car
53+
pub fn controlcar() -> Unit {
54+
if @wasm4.get_gamepad(index=1).button_right && car.x < 160 {
55+
car.x += 1
56+
} else if @wasm4.get_gamepad(index=1).button_down && car.y < 160 {
57+
} else if @wasm4.get_gamepad(index=1).button_left && car.x >= 0 {
58+
car.x -= 2
59+
} else if @wasm4.get_gamepad(index=1).button_up && car.y >= 0 {
60+
}
61+
drawcar()
62+
}
63+
64+
// 更新car
65+
pub fn updatecar() -> Unit {
66+
if car.y + car.height > screen_size {
67+
gameState.gameTitle = "gameOver"
68+
car.y=140
69+
} else if car.y < 0 {
70+
car.y=140
71+
}
72+
}
73+
74+
//绘制障碍物
75+
pub fn drawObstacle() -> Unit {
76+
for item in obstacle {
77+
@wasm4.set_draw_colors(3)
78+
@wasm4.rect(item.x, item.y, item.width, item.height)
79+
}
80+
}
81+
82+
// 循环生成障碍物
83+
pub fn addPipe() -> Unit {
84+
let x = random.int(limit=10) * (screen_size - 20)/10;
85+
let y = -20;
86+
obstacle.push({ x: x, y: y, width: 20, height:40,obstacleSpawnInterval:2000 })
87+
}
88+
89+
// 障碍物坐标更新
90+
pub fn updateobstacle() -> Unit {
91+
for item in obstacle {
92+
item.y += obstacleSpeed
93+
if item.y > screen_size {
94+
gameState.score += 1
95+
let a = obstacle.pop()
96+
addPipe()
97+
}
98+
}
99+
}
100+
101+
// 检查碰撞
102+
pub fn checkCollisions() -> Unit {
103+
for item in obstacle {
104+
if car.x < item.x + item.width && car.x + car.width > item.x {
105+
if car.y < item.y+item.height && car.y+car.height> item.y {
106+
gameOver()
107+
}
108+
}
109+
}
110+
}
111+
112+
pub fn gameOver()->Unit{
113+
gameState.gameTitle = "gameover"
114+
gameState.isGameOver = true
115+
@wasm4.set_draw_colors(3)
116+
@wasm4.text("Game Over!", 40, 80)
117+
}
118+
119+
pub fn drawScore() -> Unit {
120+
@wasm4.set_draw_colors(3)
121+
@wasm4.text("score: "+gameState.score.to_string(), 50, 0)
122+
}
123+
124+
125+
126+
pub fn start() -> Unit {
127+
@wasm4.set_palette(1, @wasm4.rgb(0xF7F7F7)) //背景 白色
128+
@wasm4.set_palette(2, @wasm4.rgb(0xffc000)) //汽车 黄色
129+
@wasm4.set_palette(3, @wasm4.rgb(0x19A15F)) //障碍物 黑色
130+
@wasm4.set_palette(4, @wasm4.rgb(0xFFFFFF)) //背景 白色
131+
}
132+
133+
pub fn update() -> Unit {
134+
if(gameState.isGameOver==false){
135+
updatecar()
136+
controlcar()
137+
drawObstacle()
138+
drawScore()
139+
updateobstacle()
140+
checkCollisions()
141+
}else{
142+
drawScore()
143+
gameOver()
144+
}
145+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
// "is-main": true,
3+
"import": ["moonbitlang/wasm4"],
4+
"link": {
5+
"wasm-gc": {
6+
"exports": ["start", "update"],
7+
"import-memory": {
8+
"module": "env",
9+
"name": "memory"
10+
}
11+
},
12+
"wasm": {
13+
"exports": ["start", "update"],
14+
"import-memory": {
15+
"module": "env",
16+
"name": "memory"
17+
},
18+
"heap-start-address": 6590
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)