forked from hackclub/sprig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCaveEscape.js
398 lines (384 loc) · 7.25 KB
/
CaveEscape.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
@title: CaveEscape
@author: Michael Taylor
@tags: ["strategy" , "action" , "real-time"]
@addedOn: 2024-02-16
First time? Check out the tutorial game:
https://sprig.hackclub.com/gallery/getting_started
use WASD to move the cursor and J to select and use an action, the enemy will automatically deal damage to you after every action you take
*/
const winLevel = 6
const playerSprite = "s"
const enemy = "e"
const enemy2 = "E"
const menudiv = "d"
const player = "p"
const button1 = "1"
const button2 = "2"
const button3 = "3"
const deathBackground = "B"
const winBackground = "b"
const grass = "G"
const graveStone = "g"
const cave = "c"
var playerHealth = 5
var playerMana = 0
var playerAction = null
var enemyHealth = 10
setLegend(
[playerSprite, bitmap`
.....000000.....
.....003333.....
.....000000.....
................
.....000000.....
....0022220.....
....0020220.0...
....00202200....
....0020220.....
....0020220.....
....0020220.....
.....022220.....
.....022220.....
.....000000.....
.....0....0.....
.....0....0.....`],
[enemy, bitmap`
................
................
................
................
................
................
................
................
................
................
................
.....00000......
..000444430.....
.044444444400...
.0444344444440..
0444444444444400`],
[enemy2, bitmap`
................
................
................
................
.....000........
....00000.......
....30300.......
....00000.......
.....000........
.......0000.....
..00..000.00....
.0..000.0..0....
...00...0..0....
..0.0.0....0....
..0..0...00.0...
........0....0..`],
[menudiv, bitmap`
0000000000000000
................
................
................
................
................
................
................
................
................
................
................
................
................
................
................`],
[player, bitmap`
000..........000
0..............0
0..............0
................
................
................
................
................
................
................
................
................
................
0..............0
0..............0
000..........000`],
[button1, bitmap`
..333333333333..
.33333333333333.
3333333333311333
3333333333112333
3333333331112333
3333333311112333
3333333111123333
3333331111233333
3333CC1111333333
33333CC113333333
3333CCCC33333333
333CCC3C33333333
333CC33333333333
3333333333333333
.33333333333333.
..333333333333..`],
[button3, bitmap`
..660000000066..
.60000000000006.
6060000000000606
6000000000000006
0000000000000000
0000020000200000
0000002222000000
0000002020000000
0000002222000000
0000000202000000
0000020000200000
0000000000000000
6000000000000006
6060000000000606
.60000000000006.
..660000000066..`],
[button2, bitmap`
..444444444444..
.44444444444444.
444CC44444444444
444334444444D444
44432444444D4444
4443344444D44444
444324444D444444
44432444D4444444
4444444D444CC444
444444D444477444
44444D4444472444
4444D44444777244
444D444444777744
4444444444477444
.44444444444444.
..444444444444..`],
[deathBackground, bitmap`
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000`],
[graveStone, bitmap`
................
................
................
................
....LL111111....
....LL11LL11....
....LL111111....
....LL1LL1L1....
....LL111111....
....LL1L1LL1....
....LL111111....
....LL111114....
..4CLCC1CD14....
...4CLCCC1DC.C..
.CCCCCCCCCCCCCC.
................`],
[grass, bitmap`
DDDDDDDDDDD4DD44
DD3DDDDDDDDDDDDD
D363DDDDDDDDDDDD
DD3DDDDDD4DDDDDD
DD4DDD4D4DDDDDDD
DD4DDDD44DDDDD4D
DDDDDDDDDDDDDDDD
DDDDD4DDDDDD3DDD
DDDD4DDDDDD363DD
DDD444DDD4D43DDD
DDDDDDDDDD44DDDD
DDDDDDDDDDD4D4DD
D4DDDDDDDD444DDD
DD4DDDDDDDDDDDDD
D44DDDD4DDDDDDDD
DDDDDDD44DDD4DDD`],
[cave, bitmap`
.......L1111....
.....LLL1L111...
....LLLLL111LL..
....LLL111111L..
...LLL11110001L.
...LL1111000001.
...LL1100000001.
..LL1L100030301.
..LL11100000001.
..LL11100000001L
..L1110000033031
.LLL110000000001
.LLL110303000001
.LL1110000000001
LLL1100000000001
LL11100000303001`]
)
let level = 1
const levels = [
map`
g`,
map`
.............
.s.........e.
ddddddddddddd
1............
2.p..........
3............
.............`,
map`
.............
..s........e.
ddddddddddddd
1............
2.p..........
3............
.............`,
map`
.............
...s.......e.
ddddddddddddd
1............
2.p..........
3............
.............`,
map`
..............
....s......E..
dddddddddddddd
1.............
2.p...........
3.............
..............`,
map`
.............
.s.....E.....
ddddddddddddd
1............
2.p..........
3............
.............`,
map`
.............
.............
.............
....c..s.....
.............
.............
.............`,
map`
......p......
.............
.............
......g......
.............
.............
.............`
]
setMap(levels[level])
function globalUpdate() {
if (playerHealth >= 0 & level != winLevel) {
clearText()
addText("mp" + playerMana, {x: 0, y: 6, color: color`5`})
addText("hp" + playerHealth, {x: 4, y: 6, color: color`3`})
addText("hp" + enemyHealth, {x: 16, y: 6, color: color`3`})
addText("dmg" + level, {x: 16, y: 7, color: color`6`})
playerAction = buttonHover(getFirst(player).x, getFirst(player).y)
if(enemyHealth <= 0) {
level += 1
setMap(levels[level])
enemyHealth = 6 + level * 2
}
} else if(level != winLevel) {
level = 7
setMap(levels[level])
clearText()
addText("YOU DIED", {x: 6, y: 5, color: color`3`})
setBackground("B")
} else {
clearText()
addText("YOU ESCAPED!", {x: 4, y: 5, color: color`6`})
setBackground("G")
}
}
function buttonHover(xPos, yPos) {
if (xPos == 0) {
if (yPos == 3) {
addText(" Stab: \n Damage enemy equal \n to health", { x: 1, y: 7, color: color`3`})
return "stab"
} else if (yPos == 4) {
addText(" Gamble: \n Gain mana 25% \n Gain health 75%.", { x: 1, y: 7, color: color`4`})
return "gamble"
} else if (yPos == 5) {
addText(" Hex: \n Spend 3 mana to \n erase the enemy \n from existance.", { x: 1, y: 7, color: color`0`})
return "hex"
} else {
return null
}
}
}
const playerStateMachine = {
stab: function () {
if (playerAction == "stab") {
enemyHealth -= playerHealth
}
},
gamble: function () {
if (Math.round(Math.random(4)) == 1 & playerAction == "gamble") {
playerMana += 1
} else if (playerAction == "gamble"){
playerHealth += level + 1
}
},
hex: function () {
if (playerMana >= 3 & playerAction == "hex") {
enemyHealth = 0
playerMana -= 3
} else {
playerHealth += 1
}
}
}
onInput("w", () => {
getFirst(player).y -= 1
})
onInput("a", () => {
getFirst(player).x -= 1
})
onInput("s", () => {
getFirst(player).y += 1
})
onInput("d", () => {
getFirst(player).x += 1
})
onInput("j", () => {
switch (playerAction) {
case "stab":
playerStateMachine.stab()
case "gamble":
playerStateMachine.gamble()
case "hex":
playerStateMachine.hex()
}
playerHealth -= level
})
afterInput(() => {
})
setInterval(globalUpdate, 250)