-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
246 lines (212 loc) · 6.97 KB
/
game.js
File metadata and controls
246 lines (212 loc) · 6.97 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
kaboom({
global: true,
fullscreen: true,
scale: 2,
debug: true,
clearColor: [0, 0, 0, 1],
})
const MOVE_SPEED = 120
const JUMP_FORCE = 400
const BIG_JUMP_FORCE = 550
let CURRENT_JUMP_FORCE = JUMP_FORCE
let isJumping = true
const FALL_DEATH = 400
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')
//background
loadSprite('bg', '0fNdo9h.png')
//background sound / not working....
// loadSound("sound", "download.wav");
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()],// body adds gravity to this object
'!': [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('Coins: ' + score),
pos(0, 20),
layer('ui'), solid(),
{
value: score,
}
])
add([text('level ' + parseInt(level + 1)), pos(0, 00)])
add([sprite('bg'), layer('bg'), scale(2)])
// play('sound')
function big(){
let timer = 0;
let isBig = false;
return {
update(){
if(isBig) {
CURRENT_JUMP_FORCE = BIG_JUMP_FORCE
timer -=dt();//dt is a kaboom method
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),//initial position
body(),
big(),
origin('bot'),
])
//action to keep moving mushroom
action('mushroom', (m) => {
m.move(50, 0)
})
//player section
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 = 'Coins: ' + scoreLabel.value;
// player.biggify(6)
})
const ENEMY_SPEED = 20
action('dangerous', (d) => {
d.move(-ENEMY_SPEED , 0)
})
player.collides('dangerous', (d) => {
if(isJumping){
destroy(d)
} else {
//this function will load a new screen, code out of this 'scene'
go('lose', {score: scoreLabel.value})
}
})
player.action(() => {
//fix camera position on player position
camPos(player.pos)
//show game over if user falls
if(player.pos.y >= FALL_DEATH) {
go('lose', { text: 'Game Over! Points: ', score: scoreLabel.value})
}
})
player.collides('pipe', () => {
keyPress('down', () => {
go('game', {
level: (level + 1) % maps.length,
score: scoreLabel.value
})
})
})
//---------- end player section -----------
keyDown('left', () => {
player.move(-MOVE_SPEED, 0)
})
keyDown('right', (obj) => {
player.move(MOVE_SPEED, 0)
})
// keyUp('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('Game Over! Points: ' + score, 32), origin('center'), pos(width()/2, height()/2)])
})
start("game", { level: 0, score: 0 })