forked from hackclub/sprig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoomed.js
1417 lines (1333 loc) · 35.5 KB
/
Doomed.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
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
@title: Doomed
@author: m5kro
@tags: ['action']
@addedOn: 2024-07-24
*/
const playerUp = "u";
const playerDown = "d";
const playerLeft = "l";
const playerRight = "r";
const bulletUp = "b";
const bulletDown = "n";
const bulletLeft = "e";
const bulletRight = "f";
const wall = "w";
const demonUp = "m";
const demonDown = "o";
const demonLeft = "p";
const demonRight = "q";
const door = "j";
const ground = "g";
const bossDoor1 = "z";
const bossDoor2 = "y";
const bossDoor3 = "x";
const bossDoor4 = "t";
const fireballUp = "a";
const fireballDown = "c";
const fireballLeft = "h";
const fireballRight = "v";
const demonBUP = "i";
const demonBDown = "k";
const demonBLeft = "s";
const demonBRight = "]";
const bossUp = "[";
const bossDown = "{";
const bossLeft = "}";
const bossRight = "<";
let score = 0;
let playerLives = 5;
let bossHP = 15;
let isGameOver = false; // Prevent player from breaking game over screen
setLegend(
[playerUp, bitmap`
.........00.....
........0110....
........0110....
........0110....
....00000000....
...0555555550...
..055500005550..
.0D050LLLL050D0.
.0DD00000000DD0.
.0DDD0DDDD0DDD0.
.0DD0DDDDDD0DD0.
.0DD0DDDDDD0DD0.
.0DD0DDDDDD0DD0.
..0DD0DDDD0DD0..
...00DDDDDD00...
....00000000....`],
[playerDown, bitmap`
....00000000....
...00DDDDDD00...
..0DD0DDDD0DD0..
.0DD0DDDDDD0DD0.
.0DD0DDDDDD0DD0.
.0DD0DDDDDD0DD0.
.0DDD0DDDD0DDD0.
.0DD00000000DD0.
.0D050LLLL050D0.
..055500005550..
...0555555550...
....00000000....
....0110........
....0110........
....0110........
.....00.........`],
[playerLeft, bitmap`
................
.......000000...
......0DDDDDD0..
.....050DDDDDD0.
.00005550D000D00
0111055000DDD0D0
0111050L0DDDDDD0
.000050L0DDDDDD0
....050L0DDDDDD0
....050L0DDDDDD0
....055000DDD0D0
....05550D000D00
.....050DDDDDD0.
......0DDDDDD0..
.......000000...
................`],
[playerRight, bitmap`
................
...000000.......
..0DDDDDD0......
.0DDDDDD050.....
00D000D05550....
0D0DDD000550....
0DDDDDD0L050....
0DDDDDD0L050....
0DDDDDD0L050000.
0DDDDDD0L0501110
0D0DDD0005501110
00D000D05550000.
.0DDDDDD050.....
..0DDDDDD0......
...000000.......
................`],
[bulletUp, bitmap`
................
................
................
................
................
................
.........6......
.........6......
.........6......
................
................
................
................
................
................
................`],
[bulletDown, bitmap`
................
................
................
................
................
................
................
......6.........
......6.........
......6.........
................
................
................
................
................`],
[bulletLeft, bitmap`
................
................
................
................
................
................
......666.......
................
................
................
................
................
................
................
................
................`],
[bulletRight, bitmap`
................
................
................
................
................
................
................
................
.......666......
................
................`],
[wall, bitmap`
0000000000000000
3333003333333333
3333003333333333
3333003333333333
3333003333333333
3333003333333333
3333003333333333
0000000000000000
0000000000000000
3333333333003333
3333333333003333
3333333333003333
3333333333003333
3333333333003333
3333333333003333
0000000000000000`],
[ground, bitmap`
1111111001111111
1111110LL0111111
111110LLLL011111
11110LLLLLL01111
1110LLLLLLLL0111
110LLLLLLLLLL011
10LLLLLLLLLLLL01
0LLLLLLLLLLLLLL0
0LLLLLLLLLLLLLL0
10LLLLLLLLLLLL01
110LLLLLLLLLL011
1110LLLLLLLL0111
11110LLLLLL01111
111110LLLL011111
1111110LL0111111
1111111001111111`],
[demonUp, bitmap`
......0..0......
..00.020020.00..
.03302200220330.
.03300000000330.
.03036300363030.
.00336300363300.
.03033033033030.
.03300333300330.
.00333000033300.
.03033333333030.
.03300000000330.
.03030333303030.
..033300003330..
...0303333030...
....00000000....
................`],
[demonDown, bitmap`
................
....00000000....
...0303333030...
..033300003330..
.03030333303030.
.03300000000330.
.03033333333030.
.00333000033300.
.03300333300330.
.03033033033030.
.00336300363300.
.03036300363030.
.03300000000330.
.03302200220330.
..00.020020.00..
......0..0......`],
[demonLeft, bitmap`
................
..0000000000....
.033303303330...
.0330303303030..
..0033303303330.
.02066303300300.
022033030303030.
.00000330303030.
.00000330303030.
022033030303030.
.02066303300300.
..0033303303330.
.0330303303030..
.033303303330...
..0000000000....
................`],
[demonRight, bitmap`
................
....0000000000..
...033303303330.
..0303033030330.
.0333033033300..
.00300330366020.
.030303030330220
.03030303300000.
.03030303300000.
.030303030330220
.00300330366020.
.0333033033300..
..0303033030330.
...033303303330.
....0000000000..
................`],
[door, bitmap`
0000000000000000
0LLLLLLLLLLLLLL0
0LLLL100001LLLL0
0LLL01000010LLL0
0LL1010000101LL0
0L010100001010L0
0L010100001010L0
0L010100001010L0
0L010100001010L0
0L010100001010L0
0L010100001010L0
0L010100001010L0
0L010100001010L0
0L010100001010L0
0L010100001010L0
0000000000000000`],
[bossDoor1, bitmap`
0000000000000000
0LLLLLLLLLLLLLLL
0L222LLLLLLLLLLL
023232LL01100110
0L222LL001100110
0L222L1001100110
0LLLL11001100110
0LLL011001130110
0LL0011001133110
0LL0011001100110
0LL0011001100110
0LL0011001100110
0LL0011001100110
0LL0011001100110
0LL0011001100110
0LL0011001100110`],
[bossDoor2, bitmap`
0000000000000000
LLLLLLLLLLLLLLL0
LLLLLLLLLLL222L0
01100110LL232320
011001100LL222L0
0110011001L222L0
01100110011LLLL0
011031100110LLL0
0113311001100LL0
0110011001100LL0
0110011001100LL0
0110011001100LL0
0110011001100LL0
0110011001100LL0
0110011001100LL0
0110011001100LL0`],
[bossDoor3, bitmap`
0LL0011000000110
0LL0011003330110
0LL0011030000110
0LL0011033330110
0LL0011030000110
0LL0011033330110
0LL0011030000110
0LL0011003330110
0LL0011000000110
0LL0011001100110
0LL0011001100110
0LL0011001100110
0LL0011001100110
0LL0011001100110
0LL0011001100110
0000000000000000`],
[bossDoor4, bitmap`
0110000001100LL0
0110333001100LL0
0110000301100LL0
0110333301100LL0
0110000301100LL0
0110333301100LL0
0110000301100LL0
0110333001100LL0
0110000001100LL0
0110011001100LL0
0110011001100LL0
0110011001100LL0
0110011001100LL0
0110011001100LL0
0110011001100LL0
0000000000000000`],
[fireballUp, bitmap`
......9999......
.....996699.....
....39666693....
...3966226693...
...3962222693...
...3962222693...
...3962222693...
...3962222693...
....39622693....
.....396693.....
.....396693.....
.....396693.....
......3993......
......3993......
.......33.......
.......33.......`],
[fireballDown, bitmap`
.......33.......
.......33.......
......3993......
......3993......
.....396693.....
.....396693.....
.....396693.....
....39622693....
...3962222693...
...3962222693...
...3962222693...
...3962222693...
...3966226693...
....39666693....
.....996699.....
......9999......`],
[fireballLeft, bitmap`
................
................
................
...33333........
..3999993.......
.99666669333....
99662222699933..
9662222226669933
9662222226669933
99662222699933..
.99666669333....
..3999993.......
...33333........
................
................
................`],
[fireballRight, bitmap`
................
................
................
........33333...
.......3999993..
....33396666699.
..33999622226699
3399666222222669
3399666222222669
..33999622226699
....33396666699.
.......3999993..
........33333...
................
................
................`],
[demonBUP, bitmap`
..000000000000..
...0LLLLLLLL0...
....0LLLLLL0....
....0LLLLLL0....
...030LLLL030...
..03600LL00630..
.0L0009009000L0.
.0LL09900990LL0.
.0L0990990990L0.
.00990099009900.
.0090L0990L0900.
.000LL0990LL000.
..0LLL0990LLL0..
...0LL0990LL0...
....00000000....
................`],
[demonBDown, bitmap`
................
....00000000....
...0LL0990LL0...
..0LLL0990LLL0..
.000LL0990LL000.
.0090L0990L0900.
.00990099009900.
.0L0990990990L0.
.0LL09900990LL0.
.0L0009009000L0.
..03600LL00630..
...030LLLL030...
....0LLLLLL0....
....0LLLLLL0....
...0LLLLLLLL0...
..000000000000..`],
[demonBLeft, bitmap`
................
......000000....
0....0LLL0000...
00..030L0990L0..
0L003600990LLL0.
0LLL000990LLLL0.
0LLLL0990000000.
0LLLLL009999990.
0LLLLL009999990.
0LLLL0990000000.
0LLL000990LLLL0.
0L003600990LLL0.
00..030L0990L0..
0....0LLL0000...
......000000....
................`],
[demonBRight, bitmap`
................
....000000......
...0000LLL0....0
..0L0990L030..00
.0LLL099006300L0
.0LLLL099000LLL0
.0000000990LLLL0
.099999900LLLLL0
.099999900LLLLL0
.0000000990LLLL0
.0LLLL099000LLL0
.0LLL099006300L0
..0L0990L030..00
...0000LLL0....0
....000000......
................`],
[bossUp, bitmap`
.0............0.
030.00000000.030
0330222222220330
.00236322363200.
.02233222233220.
.00232222223200.
.0L0000000000L0.
.0LL03999930LL0.
.0LL03399330LL0.
.00LL033330LL00.
.030LL0330LL030.
.03300399300330.
..033399993330..
...0999999990...
....00000000....
................`],
[bossDown, bitmap`
................
....00000000....
...0999999990...
..033399993330..
.03300399300330.
.030LL0330LL030.
.00LL033330LL00.
.0LL03399330LL0.
.0LL03999930LL0.
.0L0000000000L0.
.00232222223200.
.02233222233220.
.00236322363200.
0330222222220330
030.00000000.030
.0............0.`],
[bossLeft, bitmap`
.00.............
033000000000....
.03020LLL0330...
..02220LLL0330..
.02333000LL0390.
.026320330L0390.
.02322093303990.
.02222099339990.
.02222099339990.
.02322093303990.
.026320330L0390.
.02333000LL0390.
..02220LLL0330..
.03020LLL0330...
033000000000....
.00.............`],
[bossRight, bitmap`
.............00.
....000000000330
...0330LLL02030.
..0330LLL02220..
.0930LL00033320.
.0930L033023620.
.09930339022320.
.09993399022220.
.09993399022220.
.09930339022320.
.0930L033023620.
.0930LL00033320.
..0330LLL02220..
...0330LLL02030.
....000000000330
.............00.`]
);
setBackground(ground);
setSolids([
playerUp,
playerDown,
playerLeft,
playerRight,
wall,
demonUp,
demonDown,
demonLeft,
demonRight,
demonBUP,
demonBDown,
demonBLeft,
demonBRight,
bossUp,
bossDown,
bossLeft,
bossRight
]);
let level = 0;
const levels = [
map`
wwwwwwwwww
w........j
w........w
w........w
w........w
w........w
w........w
wwwwwwwwww`,
map`
wwwwwwwwww
j..w.....w
w..w.....w
w..w.....w
w..w..w..w
w..w..w..w
w.....w..j
wwwwwwwwww`,
map`
wwwwwwwwjw
w........w
w.w....w.w
w........w
w........w
w.w....w.w
j........w
wwwwwwwwww`,
map`
wwwwwwwwww
w........j
w.w..wwwww
w.w..w...w
w.w..w.w.w
w.wwww.w.w
w......w.w
wwwwwwwwjw`,
map`
wwwwwwwwww
wwwwzywwww
wwwwxtwwww
w........w
w........w
wwwwwwww.w
j........w
wwwwwwwwww`,
map`
wwwwwwwwww
w........w
w........w
w..w..w..w
w..w..w..w
w........w
w........w
wwwwwwwwww`
];
// The Only Thing They Fear Is You - Mick Gordon (I tried)
const melody = tune`
315.7894736842105: E5/315.7894736842105,
315.7894736842105: E4/315.7894736842105,
315.7894736842105: E4/315.7894736842105,
315.7894736842105: E4/315.7894736842105,
315.7894736842105: D5/315.7894736842105,
315.7894736842105: F5/315.7894736842105,
315.7894736842105: D5/315.7894736842105,
315.7894736842105: E5/315.7894736842105,
315.7894736842105: E4/315.7894736842105,
315.7894736842105: E4/315.7894736842105,
315.7894736842105: E4/315.7894736842105,
315.7894736842105: F5/315.7894736842105,
315.7894736842105: D5/315.7894736842105,
315.7894736842105: E5/315.7894736842105,
315.7894736842105: E4/315.7894736842105,
315.7894736842105: E4/315.7894736842105,
315.7894736842105: E4/315.7894736842105,
315.7894736842105: D5/315.7894736842105,
315.7894736842105: F5/315.7894736842105,
315.7894736842105: D5/315.7894736842105,
315.7894736842105: E5/315.7894736842105,
315.7894736842105: E4/315.7894736842105,
315.7894736842105: E4/315.7894736842105,
315.7894736842105: D5/315.7894736842105,
315.7894736842105: E5/315.7894736842105,
315.7894736842105: D5/315.7894736842105,
315.7894736842105: E5/315.7894736842105,
315.7894736842105,
315.7894736842105: E5/315.7894736842105,
315.7894736842105: E5/315.7894736842105,
315.7894736842105: E5/315.7894736842105,
315.7894736842105-`
// The repeating melody will kill you before the demons will
const playbackMelody = playTune(melody, Infinity)
setMap(levels[level]);
setPushables({
[playerUp]: [],
[playerDown]: [],
[playerLeft]: [],
[playerRight]: []
});
let lastMoveTime = 0;
const moveCooldown = 300; // 0.3 seconds
const bulletSpeed = 100; // 0.1 seconds
let lastFireTime = 0;
const fireCooldown = 100; // 0.1 seconds
let demonSpawnRate = 3000; // 3 seconds
let demonMoveSpeed = 800; // 0.8 seconds
let demonBMoveSpeed = 800; // 0.8 seconds
const bossMoveSpeed = 500; // 0.5 seconds
let lastBossFireTime = 0;
addText(`Score:${score}`, {
x: 1,
y: 0,
color: color`4`
});
addText(`HP:${playerLives}`, {
x: 1,
y: 15,
color: color`4`
});
setMap(levels[level]);
addSprite(1, 6, playerRight); // Spawn the player
function movePlayer(dx, dy) {
// Limit movement speed
const currentTime = Date.now();
if (isGameOver || currentTime - lastMoveTime < moveCooldown) return;
const player = getFirst(playerUp) || getFirst(playerDown) || getFirst(playerLeft) || getFirst(playerRight);
if (player) {
const newX = player.x + dx;
const newY = player.y + dy;
const targetTile = getTile(newX, newY);
// Check for door interaction
const doorTile = targetTile.find(sprite => sprite.type === door);
if (doorTile) {
if (level === 0 && newX === 9 && newY === 1) {
level = 1; // Move to level 1
setMap(levels[level]);
addSprite(1, 1, playerRight);
} else if (level === 1 && newX === 0 && newY === 1) {
level = 0; // Move back to level 0
setMap(levels[level]);
addSprite(8, 1, playerLeft);
} else if (level === 1 && newX === 9 && newY === 6) {
level = 2; // Move to level 2
setMap(levels[level]);
addSprite(1, 6, playerRight);
} else if (level === 2 && newX === 0 && newY === 6) {
level = 1; // Move back to level 1
setMap(levels[level]);
addSprite(8, 6, playerLeft);
} else if (level === 2 && newX === 8 && newY === 0) {
level = 3 //Move to level 3
setMap(levels[level]);
addSprite(8, 6, playerUp);
} else if (level === 3 && newX === 8 && newY === 7) {
level = 2; // Move back to level 2
setMap(levels[level]);
addSprite(8, 1, playerDown);
} else if (level === 3 && newX === 9 && newY === 1) {
level = 4; // Move to level 4
setMap(levels[level]);
addSprite(1, 6, playerRight);
} else if (level === 4 && newX === 0 && newY === 6) {
level = 3; // Move back to level 3
setMap(levels[level]);
addSprite(8, 1, playerLeft);
}
return;
}
// Check for boss door interaction
const bossDoorTile = targetTile.find(sprite => sprite.type === bossDoor1 || sprite.type === bossDoor2 || sprite.type === bossDoor3 || sprite.type === bossDoor4);
if (bossDoorTile) {
if (level === 4 && (newX === 4 || newX === 5) && newY === 2) {
level = 5; // Move to level 5
setMap(levels[level]);
addSprite(1, 6, playerRight);
addSprite(8, 6, bossUp); // Spawn the boss
// Add boss HP
addText(`Boss HP:${bossHP}`, {
x: 10,
y: 15,
color: color`4`
});
}
return;
}
// Move player
player.x = newX;
player.y = newY;
lastMoveTime = currentTime;
}
}
function changeDirection(newDirection) {
if (isGameOver) return;
const player = getFirst(playerUp) || getFirst(playerDown) || getFirst(playerLeft) || getFirst(playerRight);
if (player) {
player.type = newDirection;
}
}
function updateScoreAndHPText() {
clearText();
addText(`Score:${score}`, {
x: 1,
y: 0,
color: color`4`
});
addText(`HP:${playerLives}`, {
x: 1,
y: 15,
color: color`4`
});
if (level === 5) {
addText(`Boss HP:${bossHP}`, {
x: 10,
y: 15,
color: color`4`
});
}
}
function fireBullet(bulletType, dx, dy) {
// Limit to firing rate
const currentTime = Date.now();
if (isGameOver || currentTime - lastFireTime < fireCooldown) return;
lastFireTime = currentTime;
const player = getFirst(playerUp) || getFirst(playerDown) || getFirst(playerLeft) || getFirst(playerRight);
if (player) {
// Shooting sound effect
const shot = tune`
42.857142857142854: C5/42.857142857142854,
1328.5714285714284`
playTune(shot)
const bulletX = player.x + dx;
const bulletY = player.y + dy;
// Check if there's a demon right in front of the player
const initialTile = getTile(bulletX, bulletY);
const demon = initialTile.find(sprite => sprite.type === demonUp || sprite.type === demonDown || sprite.type === demonLeft || sprite.type === demonRight || sprite.type === demonBUP || sprite.type === demonBDown || sprite.type === demonBLeft || sprite.type === demonBRight);
if (demon) {
demon.remove();
score += demon.type.startsWith("demonB") ? 200 : 100;
updateScoreAndHPText();
return;
}
// Check if there's a boss right in front of the player
const boss = initialTile.find(sprite => sprite.type === bossUp || sprite.type === bossDown || sprite.type === bossLeft || sprite.type === bossRight);
if (boss) {
bossHP--;
if (bossHP <= 0) {
boss.remove();
score += 1000;
gameOver();
}
updateScoreAndHPText();
return;
}
// Check if the initial position for the bullet is obstructed by a wall
if (initialTile.some(sprite => sprite.type === wall)) return;
addSprite(bulletX, bulletY, bulletType);
const bulletTile = getTile(bulletX, bulletY);
const bullet = bulletTile.find(sprite => sprite.type === bulletType);
const moveBullet = setInterval(() => {
bullet.x += dx;
bullet.y += dy;
// Check for collision with demons
const tile = getTile(bullet.x, bullet.y);
const demon = tile.find(sprite => sprite.type === demonUp || sprite.type === demonDown || sprite.type === demonLeft || sprite.type === demonRight || sprite.type === demonBUP || sprite.type === demonBDown || sprite.type === demonBLeft || sprite.type === demonBRight);
if (demon) {
clearTile(bullet.x, bullet.y)
clearInterval(moveBullet);
score += demon.type.startsWith("demonB") ? 200 : 100;
updateScoreAndHPText();
return;
}
// Check for collision with boss
const boss = tile.find(sprite => sprite.type === bossUp || sprite.type === bossDown || sprite.type === bossLeft || sprite.type === bossRight);
if (boss) {
bullet.remove();
clearInterval(moveBullet);
bossHP--;
if (bossHP <= 0) {
boss.remove();
score += 1000;
gameOver();
}
updateScoreAndHPText();
return;
}
// Stop moving bullet if it hits a wall or goes out of bounds
if (tile.some(sprite => sprite.type === wall) || bullet.x < 0 || bullet.y < 0 || bullet.x >= width() || bullet.y >= height()) {
clearInterval(moveBullet);
bullet.remove();
}
}, bulletSpeed);
}
}
function fireFireball(fireballType, dx, dy, startX, startY) {
// Reduce floating fireballs
const currentTime = Date.now();
if (isGameOver || currentTime - lastFireTime < fireCooldown) return;
lastFireTime = currentTime;
// Fireball sound effect
const fire = tune`
42.857142857142854: C4-42.857142857142854,
1328.5714285714284`
playTune(fire)
// Check if the player is already at the given startX and startY
const startTile = getTile(startX, startY);
const playerAtStart = startTile.find(sprite => sprite.type === playerUp || sprite.type === playerDown || sprite.type === playerLeft || sprite.type === playerRight);
if (playerAtStart) {
playerLives--;
updateScoreAndHPText();
if (playerLives <= 0) {
gameOver();
}
return;
}
// Check if the initial position for the fireball is obstructed by a wall or other demon
if (startTile.some(sprite => sprite.type === wall || sprite.type === demonUp || sprite.type === demonDown || sprite.type === demonLeft || sprite.type === demonRight || sprite.type === demonBUP || sprite.type === demonBDown || sprite.type === demonBLeft || sprite.type === demonBRight)) return;
// Add the fireball sprite
addSprite(startX, startY, fireballType);
// Find the added fireball sprite
const fireballTile = getTile(startX, startY);
const fireball = fireballTile.find(sprite => sprite.type === fireballType);
// Move the fireball
const moveFireball = setInterval(() => {
// Move the fireball
fireball.x += dx;
fireball.y += dy;
// Check the tile after moving the fireball
const nextTile = getTile(fireball.x, fireball.y);
const player = nextTile.find(sprite => sprite.type === playerUp || sprite.type === playerDown || sprite.type === playerLeft || sprite.type === playerRight);
const wallOrDoor = nextTile.find(sprite => sprite.type === wall || sprite.type === door || sprite.type === bossDoor1 || sprite.type === bossDoor2 || sprite.type === bossDoor3 || sprite.type === bossDoor4);
const demon = nextTile.find(sprite => sprite.type === demonUp || sprite.type === demonDown || sprite.type === demonLeft || sprite.type === demonRight || sprite.type === demonBUP || sprite.type === demonBDown || sprite.type === demonBLeft || sprite.type === demonBRight);
// Check for collision with walls, doors, or demons
if (wallOrDoor || demon) {
clearInterval(moveFireball);
fireball.remove();
return; // Stop moving the fireball if it hits something
}
// Check for collision with player
if (player) {
clearInterval(moveFireball);
fireball.remove();
playerLives--;
updateScoreAndHPText();
if (playerLives <= 0) {
gameOver();
}
return; // Stop moving the fireball if it hits the player
}
// Stop moving the fireball if it goes out of bounds
if (fireball.x < 0 || fireball.y < 0 || fireball.x >= width() || fireball.y >= height()) {
clearInterval(moveFireball);
fireball.remove();
}
}, bulletSpeed); // Adjust the speed to match the bullet speed
}
function getRandomEmptyPosition() {
const emptyPositions = [];
const player = getFirst(playerUp) || getFirst(playerDown) || getFirst(playerLeft) || getFirst(playerRight);
for (let x = 0; x < width(); x++) {
for (let y = 0; y < height(); y++) {
const tile = getTile(x, y);
const distanceToPlayer = Math.abs(x - player.x) + Math.abs(y - player.y);