-
Notifications
You must be signed in to change notification settings - Fork 209
/
game.js
230 lines (199 loc) · 5.67 KB
/
game.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
//IMPORTANT: Make sure to use Kaboom version 0.5.0 for this game by adding the correct script tag in the HTML file.
kaboom({
global: true,
fullscreen: true,
scale: 2,
debug: true,
clearColor: [0, 0, 0, 1],
})
// Speed identifiers
const MOVE_SPEED = 120
const JUMP_FORCE = 360
const BIG_JUMP_FORCE = 550
let CURRENT_JUMP_FORCE = JUMP_FORCE
const FALL_DEATH = 400
const ENEMY_SPEED = 20
// Game logic
let isJumping = true
loadRoot('https://i.imgur.com/')
loadSprite('coin', 'wbKxhcd.png')
loadSprite('evil-shroom', 'KPO3fR9.png')
loadSprite('brick', 'pogC9x5.png')
loadSprite('block', 'M6rwarW.png')
loadSprite('mario', 'Wb1qfhK.png')
loadSprite('mushroom', '0wMd92p.png')
loadSprite('surprise', 'gesQ1KP.png')
loadSprite('unboxed', 'bdrLpi6.png')
loadSprite('pipe-top-left', 'ReTPiWY.png')
loadSprite('pipe-top-right', 'hj2GK4n.png')
loadSprite('pipe-bottom-left', 'c1cYSbt.png')
loadSprite('pipe-bottom-right', 'nqQ79eI.png')
loadSprite('blue-block', 'fVscIbn.png')
loadSprite('blue-brick', '3e5YRQd.png')
loadSprite('blue-steel', 'gqVoI2b.png')
loadSprite('blue-evil-shroom', 'SvV4ueD.png')
loadSprite('blue-surprise', 'RMqCc1G.png')
scene("game", ({ level, score }) => {
layers(['bg', 'obj', 'ui'], 'obj')
const maps = [
[
' ',
' ',
' ',
' ',
' ',
' % =*=%= ',
' ',
' -+ ',
' ^ ^ () ',
'============================== =====',
],
[
'£ £',
'£ £',
'£ £',
'£ £',
'£ £',
'£ @@@@@@ x x £',
'£ x x x £',
'£ x x x x x -+£',
'£ z z x x x x x x ()£',
'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!',
]
]
const levelCfg = {
width: 20,
height: 20,
'=': [sprite('block'), solid()],
'$': [sprite('coin'), 'coin'],
'%': [sprite('surprise'), solid(), 'coin-surprise'],
'*': [sprite('surprise'), solid(), 'mushroom-surprise'],
'}': [sprite('unboxed'), solid()],
'(': [sprite('pipe-bottom-left'), solid(), scale(0.5)],
')': [sprite('pipe-bottom-right'), solid(), scale(0.5)],
'-': [sprite('pipe-top-left'), solid(), scale(0.5), 'pipe'],
'+': [sprite('pipe-top-right'), solid(), scale(0.5), 'pipe'],
'^': [sprite('evil-shroom'), solid(), 'dangerous'],
'#': [sprite('mushroom'), solid(), 'mushroom', body()],
'!': [sprite('blue-block'), solid(), scale(0.5)],
'£': [sprite('blue-brick'), solid(), scale(0.5)],
'z': [sprite('blue-evil-shroom'), solid(), scale(0.5), 'dangerous'],
'@': [sprite('blue-surprise'), solid(), scale(0.5), 'coin-surprise'],
'x': [sprite('blue-steel'), solid(), scale(0.5)],
}
const gameLevel = addLevel(maps[level], levelCfg)
const scoreLabel = add([
text(score),
pos(30, 6),
layer('ui'),
{
value: score,
}
])
add([text('level ' + parseInt(level + 1) ), pos(40, 6)])
function big() {
let timer = 0
let isBig = false
return {
update() {
if (isBig) {
CURRENT_JUMP_FORCE = BIG_JUMP_FORCE
timer -= dt()
if (timer <= 0) {
this.smallify()
}
}
},
isBig() {
return isBig
},
smallify() {
this.scale = vec2(1)
CURRENT_JUMP_FORCE = JUMP_FORCE
timer = 0
isBig = false
},
biggify(time) {
this.scale = vec2(2)
timer = time
isBig = true
}
}
}
const player = add([
sprite('mario'), solid(),
pos(30, 0),
body(),
big(),
origin('bot')
])
action('mushroom', (m) => {
m.move(20, 0)
})
player.on("headbump", (obj) => {
if (obj.is('coin-surprise')) {
gameLevel.spawn('$', obj.gridPos.sub(0, 1))
destroy(obj)
gameLevel.spawn('}', obj.gridPos.sub(0,0))
}
if (obj.is('mushroom-surprise')) {
gameLevel.spawn('#', obj.gridPos.sub(0, 1))
destroy(obj)
gameLevel.spawn('}', obj.gridPos.sub(0,0))
}
})
player.collides('mushroom', (m) => {
destroy(m)
player.biggify(6)
})
player.collides('coin', (c) => {
destroy(c)
scoreLabel.value++
scoreLabel.text = scoreLabel.value
})
action('dangerous', (d) => {
d.move(-ENEMY_SPEED, 0)
})
player.collides('dangerous', (d) => {
if (isJumping) {
destroy(d)
} else {
go('lose', { score: scoreLabel.value})
}
})
player.action(() => {
camPos(player.pos)
if (player.pos.y >= FALL_DEATH) {
go('lose', { score: scoreLabel.value})
}
})
player.collides('pipe', () => {
keyPress('down', () => {
go('game', {
level: (level + 1) % maps.length,
score: scoreLabel.value
})
})
})
keyDown('left', () => {
player.move(-MOVE_SPEED, 0)
})
keyDown('right', () => {
player.move(MOVE_SPEED, 0)
})
player.action(() => {
if(player.grounded()) {
isJumping = false
}
})
keyPress('space', () => {
if (player.grounded()) {
isJumping = true
player.jump(CURRENT_JUMP_FORCE)
}
})
})
scene('lose', ({ score }) => {
add([text(score, 32), origin('center'), pos(width()/2, height()/ 2)])
})
start("game", { level: 0, score: 0})