forked from hackclub/sprig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmazegame2.js
767 lines (731 loc) · 15.2 KB
/
mazegame2.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
/*
@title: Maze
@author: Darshan Choudhary
@tags: []
@addedOn: 2024-00-00
First time? Check out the tutorial game:
https://sprig.hackclub.com/gallery/getting_started
*/
const script = document.createElement('script');
script.src = 'https://cdn.jsdelivr.net/gh/hackclub/[email protected]/dist/sprig.min.js';
document.head.appendChild(script);
const player = "p"
const border = "b"
const coin = "c"
const exit = "e"
const bg = "f"
const grass = "g"
const water = "w"
const white = "h"
const desert = "d"
setLegend(
[player, bitmap`
................
................
.......000......
.......020......
......0220......
......02220.0...
....0003230.0...
....0.0222000...
....0.05550.....
......02220.....
.....022220.....
.....02220......
......000.......
......0.0.......
.....00.00......
................`],
[border, bitmap`
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC`],
[coin, bitmap`
................
....66666666....
...6666666666...
..666CCCCCC666..
.666CCCCCCCC666.
.66CCCCCCCCCC66.
.66CCC6666CCC66.
.66CCC6CCCCCC66.
.66CCC6CCCCCC66.
.66CCC6666CCC66.
.66CCCCCCCCCC66.
.666CCCCCCCC666.
..666CCCCCC666..
...6666666666...
....66666666....
................`],
[exit, bitmap`
................
................
................
................
................
................
................
................
................
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD`],
[bg, bitmap`
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL`],
[grass, bitmap`
DD44DDDDDDDDD4DD
D44D4DD44DDDDD4D
D44DD44DD4DD4D4D
D444DDD444444D4D
D444DD444DDD4D4D
44D4DDDD44D44D44
4DD4DD4D44D4D444
4D4DDD4D44D4D44D
DD4DDD4D4DD4D44D
DD4D4D4D4D44D44D
DD4D4DDD4D44D44D
D4DD4DD44D4444D4
DDDDDD4D4D4D44D4
4DDDDDD4D4DDD444
4DD4444444D444D4
DDDDDDDDD44DDDDD`],
[water, bitmap`
5555555555555555
5222555777555555
5577555552257775
5555775552255555
5555755775577555
5755755555555555
5555555557555755
5555552257757555
5577555555555222
5552225777755555
5555555555557755
7775575755557755
5755575757775555
5577775555555555
5555777775777755
5555555555555555`],
[desert, bitmap`
F66F9999999FF6FF
FFF6FFF9FFF996FF
FF996FFFFF9969FF
F999FF66999FCC9F
F99FF9FFFCCC6F99
9FFF9FFC6FFFF99F
9FF9FFF999999F66
99FFC9F9699966FF
FF9C66999F66CFFF
99C9999F66FCF9F6
FCC99FF99FFFFFFF
F99CCFFF6996FFFF
FFFFFF9FFFF999FF
F9FFFFF9CCFF99FF
F9FFF6CCFFFFFF9F
F699999999999999`],
[white , bitmap`
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222`]
)
let n_enough_coins = map`
fffffffffffff
fffffffffffff
fffffffffffff
fffffffffffff
fffffffffffff
fffffffffffff
fffffffffffff
fffffffffffff
fffffffffffff
fffffffffffff
fffffffffffff
fffffffffffff`
let success_buy = map`
fffffffffffffffff
fffffffffffffffff
fffffffffffffffff
fffffffffffffffff
fffffffffffffffff
fffffffffffffffff
fffffffffffffffff
fffffffffffffffff
fffffffffffffffff
fffffffffffffffff
fffffffffffffffff
fffffffffffffffff`
let shop_map = map`
fffffffffffffffffff
fffffffffffffffffff
ffffffffffffffcffff
fffffffffffffffffff
fffffffffffffffffff
fggffffffffffffffff
fggffffffffffcfffff
fffffffffffffffffff
fwwffffffffffcfffff
fwwffffffffffffffff
fffffffffffffffffff
fddffffffffffcfffff
fddffffffffffffffff
fffffffffffffffffff
fffffffffffffffffff`
setSolids([player, border])
let level = 0
let current_map = level;
const levels = [
map`
ffffffffff
ffffffffff
ffffffffff
ffffffffff
ffffffffff
ffffffffff
ffffffffff
ffffffffff`,
map`
hhhhhhhhhhhhhh
hhhhhhhhhhhhhh
bbbbbbbbbbbbbb
b......b.....b
b.c....b.bbbbb
b..bbb.b.b...b
b....b.b.b.b.b
bbbb.b.b.b.b.b
.....b.b...b.b
p....b.b.c.b.b
bbbbbb.bbb.b.b
b......b...b.b
b.bbbbbb.bbbcb
b.b......b...b
b.b..c...b.b.b
b.bbbbb..b.b.b
b...c....b.b.b
bbbbbbbbbb.bbb
hhhhhhhhhhhhhh
hhhhhhhhhhhhhh`,
map`
hhhhhhhhhhhhhhhhhhhhh
hhhhhhhhhhhhhhhhhhhhh
bp.bbbbbbbbbbbbbbbbbb
b...cb.....b.......cb
b.bb.b.....b........b
b.b..b.bbb...bbbbb..b
b.b.cb..cb...bcb....b
b.bbbbbb.bbb.b.b.bbbb
b......b...b...b.bc.b
bbbb...b...bbbbb.bb.b
bc...bbb.b...b......b
b..bbb...b..cb......b
b.bb...b.bbbbb..bbbbb
b......b...b....c...b
b..cb..bc..b....b...b
bbbbbbbbbbbbbbbbbb..b
hhhhhhhhhhhhhhhhhhhhh
hhhhhhhhhhhhhhhhhhhhh`,
map`
hhhhhhhhhhhhhhhhhhh
hhhhhhhhhhhhhhhhhhh
bpbbbbbbbbbbbbbbbbb
b.b...c.b...cb...cb
b.b.bbb.bbbb.b.bb.b
b.c.bc..b....b.b..b
b.bbb.bbbb.bbb.bcbb
b.b...b...c....b..b
bbb.bbb.bbbbbbbbb.b
bc..b.bcb...c.....b
b.bbb.b.bb.b.b.bb.b
b.b.....b..b.bbbbbb
b.b.bbbbb..b.....cb
b.b.bcb....bbbbbb.b
b.bcb.bbbb.b..b...b
b...b...c..b.c...cb
bbbbbbbbbbbbbbbbb.b
hhhhhhhhhhhhhhhhhhh
hhhhhhhhhhhhhhhhhhh`,
map`
fffffffffffffff
fffffffffffffff
fffffffffffffff
fffffffffffffff
fffffffffffffff
fffffffffffffff
fffffffffffffff
fffffffffffffff
fffffffffffcfff
fffffffffffffff
fffffffffffffff
fffffffffffffff`
]
setMap(levels[level])
onInput("s", () => {
getFirst(player).y += 1
})
onInput("w", () => {
if (current_map == "shop") {
level = 0;
console.log(level);
clearText();
setMap(levels[level]);
new_game();
} else {
getFirst(player).y -= 1
}
})
onInput("a", () => {
getFirst(player).x -= 1
})
onInput("d", () => {
getFirst(player).x += 1
})
onInput("k", () => {
if (level == 0) {
clearText()
next_level()
} else {
handlePurchase(16, "Water");
}
});
onInput("l", () => { handlePurchase(25, "Desert") });
/*code to reset the current level*/
onInput("j", () => {
if (current_map == "shop") {
handlePurchase(10, "Grass")
} else {
current_map = level;
setMap(levels[level]);
}
})
function new_game() {
if (level == 0) {
addText("Maze Game", {
x: 6,
y: 2,
size: 1,
color: color`6`
})
addText("Goal is to come out\n of maze and also \nearn COINS in this\n journey", {
x: 1,
y: 5,
size: 0.5,
color: color`2`
})
addText("\nPress 'k' to start", {
x: 1,
y: 10,
size: 0.5,
color: color`4`
})
addText("-Darshan", {
x: 12,
y: 15,
size: 0.5,
color: color`2`
})
}
}
new_game();
let buy = 1
function success(background) {
clearText();
current_map = "success_buy";
setMap(success_buy);
addText("SUCCESSFULLY bought\n background", {
x: 1,
y: 4,
size: 1,
color: color`2`
})
addText("Current background=\n", {
x: 0,
y: 8,
size: 1,
color: color`2`
})
if (background == "Grass") {
setBackground(grass);
addText(background, {
x: 2,
y: 5,
size: 1,
color: color`D`
})
addText(background, {
x: 6,
y: 9,
size: 1,
color: color`D`
})
} else if (background == "Water") {
setBackground(water);
addText(background, {
x: 2,
y: 5,
size: 1,
color: color`7`
})
addText(background, {
x: 6,
y: 9,
size: 1,
color: color`7`
})
} else if (background == "Desert") {
setBackground(desert);
addText(background, {
x: 2,
y: 5,
size: 1,
color: color`9`
})
addText(background, {
x: 6,
y: 9,
size: 1,
color: color`9`
})
}
}
function shop() {
// onInput("i", () => {
current_map = "shop";
clearText();
setMap(shop_map);
addText("SHOP " + totalCoins, {
x: 8,
y: 2,
size: 1,
color: color`6`
});
addText("Backgrounds Price", {
x: 0,
y: 4,
size: 1,
color: color`2`
});
addText("Grass", {
x: 4,
y: 6,
size: 1,
color: color`2`
});
addText("10(j)", {
x: 15,
y: 6,
size: 1,
color: color`2`
});
addText("Water", {
x: 4,
y: 9,
size: 1,
color: color`2`
});
addText("16(k)", {
x: 15,
y: 9,
size: 1,
color: color`2`
});
addText("Desert", {
x: 4,
y: 12,
size: 1,
color: color`2`
});
addText("25(l)", {
x: 15,
y: 12,
size: 1,
color: color`2`
});
addText("press 'w' to play", {
x: 1,
y: 15,
size: 1,
color: color`6`
});
}
function handlePurchase(price, background) {
// return () => {
// if (totalCoins >= price) {
// buy += 1;
// totalCoins = totalCoins - price;
// console.log("here:", totalCoins);
// success();
// setTimeout(shop, 2000)
// // shop();
// } else if(totalCoins<price) {
// // if (buy > 1) {
// // if (totalCoins >= price) {
// // totalCoins = totalCoins - price;
// // console.log("not here:", totalCoins);
// // success();
// // setTimeout(shop, 2000)
// // }
// // else {
// clearText();
// setMap(n_enough_coins);
// console.log("else", totalCoins);
// addText("not enough Coins", {
// x: 2,
// y: 7,
// size: 1,
// color: color`2`
// });
// // addText("press 'i' to\n go back", {
// // x: 4,
// // y: 9,
// // size: 1,
// // color: color`6`
// // });
// setTimeout(shop, 2000)
// // shop();
// // }
// }
// };
if (totalCoins >= price) {
success(background);
totalCoins -= price;
setTimeout(shop, 2000)
} else {
clearText();
current_map = n_enough_coins;
setMap(n_enough_coins);
console.log("else", totalCoins);
addText("not enough Coins", {
x: 2,
y: 7,
size: 1,
color: color`2`
});
setTimeout(shop, 2000)
}
}
//funct to set new level
function next_level() {
if (level != 0) {
playback.end()
playTune(win, 1)
playTune(bgm, Infinity)
}
clearText()
totalCoins += coins_earned
coins_earned = 0
level += 1
current_map = level;
setMap(levels[level])
if (level == 1) {
addText("LEVEL=" + level, {
x: 8,
y: 15,
size: 0.5,
color: color`0`
})
} else if (level == 2) {
addText("LEVEL=" + level, {
x: 10,
y: 15,
size: 0.5,
color: color`0`
})
} else if (level == 3) {
addText("LEVEL=" + level, {
x: 10,
y: 15,
size: 0.5,
color: color`0`
})
} else if (level == 4) {
addText("Congratulations!! \n\nYou successfully \ncame out of maze.", {
x: 1,
y: 5,
size: 1,
color: color`2`
})
addText("Total Coins = " + totalCoins, {
x: 1,
y: 11,
size: 1,
color: color`6`
})
addText(" press 'i' to\n open shop", {
x: 3,
y: 13,
size: 1,
color: color`4`
})
onInput("i", () => {
shop()
})
}
}
//music for different events
const bgm = tune`
82.41758241758242: D5-82.41758241758242 + undefined/82.41758241758242,
82.41758241758242: undefined/82.41758241758242 + D5-82.41758241758242,
82.41758241758242: undefined/82.41758241758242 + C5-82.41758241758242,
82.41758241758242,
82.41758241758242: C5-82.41758241758242,
82.41758241758242: undefined/82.41758241758242,
82.41758241758242: undefined/82.41758241758242,
82.41758241758242: B4-82.41758241758242 + undefined/82.41758241758242,
82.41758241758242: undefined/82.41758241758242,
82.41758241758242: undefined/82.41758241758242,
82.41758241758242: undefined/82.41758241758242,
82.41758241758242: undefined/82.41758241758242,
82.41758241758242,
82.41758241758242: undefined/82.41758241758242,
82.41758241758242: G4-82.41758241758242 + undefined/82.41758241758242,
82.41758241758242: A4-82.41758241758242 + undefined/82.41758241758242,
82.41758241758242: undefined/82.41758241758242 + A4-82.41758241758242,
164.83516483516485,
82.41758241758242: undefined/82.41758241758242 + B4-82.41758241758242,
82.41758241758242: C5-82.41758241758242,
82.41758241758242: undefined/82.41758241758242,
82.41758241758242,
82.41758241758242: D5-82.41758241758242 + undefined/82.41758241758242,
82.41758241758242: undefined/82.41758241758242,
82.41758241758242: D4-82.41758241758242 + undefined/82.41758241758242,
82.41758241758242: D4-82.41758241758242 + undefined/82.41758241758242,
82.41758241758242: D4-82.41758241758242 + undefined/82.41758241758242,
82.41758241758242: undefined/82.41758241758242,
82.41758241758242: undefined/82.41758241758242,
82.41758241758242: G4-82.41758241758242,
82.41758241758242: G4-82.41758241758242`;
const win = tune`
208.33333333333334: C4-208.33333333333334 + undefined/208.33333333333334,
208.33333333333334: D4-208.33333333333334,
208.33333333333334: E4-208.33333333333334 + undefined/208.33333333333334,
208.33333333333334: F4-208.33333333333334 + undefined/208.33333333333334,
208.33333333333334: G4-208.33333333333334,
208.33333333333334: A4-208.33333333333334 + undefined/208.33333333333334,
208.33333333333334: G4-208.33333333333334 + undefined/208.33333333333334,
208.33333333333334: C5-208.33333333333334,
208.33333333333334: B4-208.33333333333334 + undefined/208.33333333333334,
208.33333333333334: C5-208.33333333333334 + undefined/208.33333333333334,
4583.333333333334`;
const coin_music = tune`
164.83516483516485: E5-164.83516483516485,
164.83516483516485: A5-164.83516483516485,
4945.054945054945`;
const playback = playTune(bgm, Infinity)
let totalCoins = 0
let coins_earned = 0
afterInput(() => {
if (level == 1) {
addText("Coins : " + coins_earned, {
x: 6,
y: 0,
size: 0.1,
color: color`0`
})
} else if (level == 2) {
addText("Coins : " + coins_earned, {
x: 8,
y: 0,
size: 0.1,
color: color`0`
})
} else if (level == 3) {
addText("Coins : " + coins_earned, {
x: 8,
y: 0,
size: 0.1,
color: color`0`
})
}
let playerSprite = getFirst(player);
if (playerSprite) {
let player_tile_x = playerSprite.x;
let player_tile_y = playerSprite.y;
console.log(player_tile_x, player_tile_y);
let items = getTile(player_tile_x, player_tile_y);
console.log(items);
if (items.length > 1) {
if (items[1].type == coin) {
playTune(coin_music, 1);
items[1].remove();
coins_earned += 1;
}
}
//checking if the player reached end - level 1
if (level == 1) {
if (player_tile_x == 10 && player_tile_y == 17) {
let winning_msg = addText("You Won!!!", {
x: 5,
y: 7,
size: 5,
color: color`0`
})
setTimeout(next_level, 2000)
}
} else if (level == 2) {
if ([player_tile_x, player_tile_y].toString() == [18, 15].toString() || [player_tile_x, player_tile_y].toString() == [19, 15].toString()) {
let winning_msg = addText("You Won!!", {
x: 6,
y: 7,
size: 5,
color: color`0`
})
setTimeout(next_level, 2000)
}
} else if (level == 3) {
if (player_tile_x == 17 && player_tile_y == 16) {
let winning_msg = addText("You Won!!!", {
x: 5,
y: 7,
size: 5,
color: color`0`
})
setTimeout(next_level, 2000)
current_map = "shop";
}
}
}
})