-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main
1064 lines (975 loc) · 30.4 KB
/
Main
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
// Import sounds and create sound variables. fill(255, 255, 0);
import processing.sound.*;
SoundFile coinSound, spikeSound, dieSound, walkSound, flashSound, menuSong;
playSong menuTheme, gameThemeA;
soundEffect walking;
class playSong {
boolean hasPlayed = false;
SoundFile usedSound;
float soundTime;
int m = millis();
public playSong(SoundFile soundPicked) {
usedSound = soundPicked;
soundTime = usedSound.duration();
}
public void play() {
if(soundTime > 0){
soundTime -= 0.0175;
}
if(hasPlayed == false){
usedSound.play();
hasPlayed = true;
} else if (soundTime <= 0){
hasPlayed = false;
soundTime = usedSound.duration();
}
}
public void debug() {
if(soundTime > 0){
soundTime -= 0.0175;
}
fill(255, 255, 0);
text(soundTime, 0, 100);
}
}
class soundEffect {
boolean hasPlayed = false;
SoundFile usedSound;
float soundTime;
public soundEffect(SoundFile soundPicked) {
usedSound = soundPicked;
soundTime = usedSound.duration() * 1.5;
}
public void play() {
if(soundTime > 0){
soundTime-=0.05;
}
if(hasPlayed == false){
usedSound.play();
usedSound.amp(0.1);
hasPlayed = true;
} else if (soundTime <= 0){
hasPlayed = false;
soundTime = usedSound.duration();
}
}
}
void playSound(SoundFile soundPicked) {
soundPicked.play();
soundPicked.amp(0.1);
}
// A function for collision
boolean colliding(float x1, float y1, int w1, int h1, float x2, float y2, int w2, int h2) {
return abs(x1 - x2) * 2 < w1 + w2 && abs(y1 - y2) * 2 < h1 + h2;
}
// The current gamescene. Menu, Paused, Game.
String scene = "Menu";
// Menu Title
float fadeIn = 0;
// Menu Flashing Text variables
int tillFlash = 550;
int flashTime = 30;
color flashCol = color(255);
// The array that stores the current "room"
int[] curLevel = {0, 0};// Level, Dimension
// Creates a ProcessingFont so that the font can be used in the game.
PFont BIT;//This is actually Press Start 2P
// Creates a lot of ProcessingImages so that images can be displayed in the game.
PImage pf1, pf2, pf3, ps1, ps2, ps3, pn1, pn2, pn3, spike, spike_guy, spike_guy_l, spike_guy_r, spike_guy_d, bb1, bb2, cf, cl, cs, cr;
// Creates a class ("Tile") to use as the basis for all the tiles in the game.
class Tile {
PVector pos;
int width;
int height;
public void draw() {
noStroke();
fill(100);
rect(pos.x - width/2, pos.y - height/2, width, height);
}
}
// A "Block" class. An extension of the Tile class.
class Block extends Tile {
public Block(int x, int y, int w, int h) {
pos = new PVector(x, y);
width = w;
height = h;
}
public Block(int x, int y) {
pos = new PVector(x, y);
width = height = 40;
}
}
// A "Spikes" class. Another extension of the Tile class.
class Spikes extends Tile {
public Spikes(int x, int y, int w, int h) {
pos = new PVector(x, y);
width = w;
height = h;
}
public Spikes(int x, int y) {
pos = new PVector(x, y);
width = height = 40;
}
public void draw() {
imageMode(CENTER);
image(spike, pos.x, pos.y-2.5, width, height+5);
}
}
// A "Hole" class. Also an extension of the Tile class.
class Hole extends Tile {
public Hole(int x, int y, int w, int h) {
pos = new PVector(x, y);
width = w;
height = h;
}
public Hole(int x, int y) {
pos = new PVector(x, y);
width = height = 40;
}
public void draw() {
noStroke();
fill(0);
ellipse(pos.x, pos.y, width, height);
}
}
// A "Portal" class. I think you already know it's an extension of the tile class.
class Portal extends Tile {
boolean isHit = false;
public Portal(int x, int y, int w, int h) {
pos = new PVector(x, y);
width = w;
height = h;
}
public Portal(int x, int y) {
pos = new PVector(x, y);
width = height = 40;
}
public void draw() {
fill(0, 155, 255);
rect(pos.x - width/2, pos.y - height/2, width, height);
}
}
// Arrays to hold all the "Tile"s in the game.
ArrayList<Block> blocks = new ArrayList<Block>();
ArrayList<Spikes> spikes = new ArrayList<Spikes>();
ArrayList<Hole> holes = new ArrayList<Hole>();
ArrayList<Portal> portals = new ArrayList<Portal>();
// A base "Enemy" class.
class Enemy {
PVector pos;
float dmg;
int width;
int height;
}
// A "SpikeGuy" extension of the Enemy class.
// The Spike Guy is a floating spike that flies around randomly
class SpikeGuy extends Enemy {
int img = 0;
int imgTime = 4;
int dir = 0;// | 0 = UL | 1 = UR | 2 = DL | 3 = DR |
int dirCTime = 20;
public SpikeGuy(int x, int y) {
pos = new PVector(x, y);
width = height = 40;
}
public void draw() {
for(int i = 0; i < 75; i++){
fill(255, 255, 255, i);
ellipse(pos.x, pos.y, 75-i, 75-i);
}
switch(img) {
case 0: image(spike_guy, pos.x, pos.y, 40, 40); break;
case 1: image(spike_guy_r, pos.x, pos.y, 40, 40); break;
case 2: image(spike_guy_d, pos.x, pos.y, 40, 40); break;
case 3: image(spike_guy_l, pos.x, pos.y, 40, 40); break;
}
if(imgTime > 0) {
imgTime--;
} else if(img < 3) {
img++;
imgTime = 4;
} else {
img = 0;
imgTime = 4;
}
}
public void runCollisions() {
// Player/Block collisions.
for(Block that: blocks) {
// If the player is hitting the block
if(colliding(pos.x, pos.y, width, height, that.pos.x, that.pos.y, that.width, that.height)) {
// And the player is hitting one of the sides
if(abs(pos.x - that.pos.x)/(width + that.width) > abs(pos.y - that.pos.y)/(height + that.height)) {
if(pos.x > that.pos.x) {// If the player x is greater than the block x.
pos.x = that.pos.x + width/2 + that.width/2;// Stop the player.
}
if(pos.x < that.pos.x) {// If the player x is less than the block x.
pos.x = that.pos.x - width/2 - that.width/2;// Stop the player.
}
}
// (Or) And the player is hitting the top or bottom
else if(abs(pos.x - that.pos.x)/(width + that.width) < abs(pos.y - that.pos.y)/(height + that.height)) {
if(pos.y > that.pos.y) {// If the player y is greater than the block y.
pos.y = that.pos.y + height/2 + that.height/2;// Stop the player.
}
if(pos.y < that.pos.y) {// If the player y is less than the block y.
pos.y = that.pos.y - height/2 - that.height/2;// Stop the player.
}
}
}
}
// Player/Hole collisions.
for(Hole that: holes) {
// If the player and the hole are colliding.
if(colliding(pos.x, pos.y, width, height, that.pos.x, that.pos.y, that.width, that.height)) {
// And the player is hitting the sides of the hole.
if(abs(pos.x - that.pos.x)/(width + that.width) > abs(pos.y - that.pos.y)/(height + that.height)) {
if(pos.x > that.pos.x) {// If the player x is greater than the hole x.
pos.x = that.pos.x + width/2 + that.width/2;// Stop the player.
}
if(pos.x < that.pos.x) {// If the player x is less than the hole x.
pos.x = that.pos.x - width/2 - that.width/2;// Stop the player.
}
}
// (Or) And the player is hitting the top or bottom of the hole.
else if(abs(pos.x - that.pos.x)/(width + that.width) < abs(pos.y - that.pos.y)/(height + that.height)) {
if(pos.y > that.pos.y) {// If the player y is greater than the hole y.
pos.y = that.pos.y + height/2 + that.height/2;// Stop the player.
}
if(pos.y < that.pos.y) {// If the player y is less than the hole y.
pos.y = that.pos.y - height/2 - that.height/2;// Stop the player.
}
}
}
}
// Player/Spike collisions.
}
public void update() {
if (dir == 0) {
pos.x -= 5;
pos.y -= 5;
}
else if (dir == 1) {
pos.x += 5;
pos.y -= 5;
}
else if (dir == 2) {
pos.x -= 5;
pos.y += 5;
}
else if (dir == 3) {
pos.x += 5;
pos.y += 5;
}
if (dirCTime > 0) {
dirCTime --;
} else {
dir = round(random(0, 4));
dirCTime = 20;
}
pos.set(constrain(pos.x, -300 + width/2, 300 - width/2), constrain(pos.y, -300 + height/2, 300 - height/2));
}
}
// A "BloodyBanana" extension of the Enemy class.
// The Bloody Banana is a roughly tiger-like monster that roams around randomly, attacking you when near.
class BloodyBanana extends Enemy {
int imgTime = 20;
int curImg = 1;
int dir = 0;
int dirTime = 10;
boolean[] canDir = {true, true, true, true};
public BloodyBanana(int x, int y){
pos = new PVector(x, y);
width = height = 120;
}
public void runCollisions() {
for(Block that: blocks) {
if(colliding(pos.x, pos.y, width, height, that.pos.x, that.pos.y, that.width, that.height)) {
if(abs(pos.x - that.pos.x)/(width + that.width) > abs(pos.y - that.pos.y)/(height + that.height)) {
if(pos.x > that.pos.x) {
canDir[2] = false;
}else{
canDir[2] = true;
}
if(pos.x < that.pos.x) {
canDir[0] = false;
}else{
canDir[0] = true;
}
}
else if(abs(pos.x - that.pos.x)/(width + that.width) < abs(pos.y - that.pos.y)/(height + that.height)) {
if(pos.y > that.pos.y) {
canDir[3] = false;
}else{
canDir[3] = true;
}
if(pos.y < that.pos.y) {
canDir[1] = false;
}else{
canDir[1] = true;
}
}
}
}
}
public void draw() {
if(imgTime > 0){
imgTime--;
}else if(curImg < 2){
curImg++;
imgTime = 20;
}else{
curImg = 1;
imgTime = 20;
}
if(curImg == 1){
image(bb1, pos.x, pos.y, width, height);
}
if(curImg == 2){
image(bb2, pos.x, pos.y, width, height);
}
}
public void update() {
if(dirTime > 0){
dirTime--;
}else{
dir = floor(random(0, 4));
dirTime = 10;
}
if(dir == 0 && canDir[0] == true){
pos.x -= 2;
}
if(dir == 1 && canDir[1] == true){
pos.y -= 2;
}
if(dir == 2 && canDir[2] == true){
pos.x += 2;
}
if(dir == 3 && canDir[3] == true){
pos.y += 2;
}
}
}
// Arrays to hold all the "Enemy"s in the game.
ArrayList<SpikeGuy> spikeGuys = new ArrayList<SpikeGuy>();
ArrayList<BloodyBanana> bloodyBananas = new ArrayList<BloodyBanana>();
// A base "Item" class.
class Item {
PVector pos;
int width;
int height;
}
// A "Coin" "Item".
class Coin extends Item {
int cImg = 1;
int cImgTime = 3;
boolean isHit = false;
public Coin(int x, int y) {
pos = new PVector(x, y);
width = height = 40;
}
public void draw() {
if(cImgTime > 0){
cImgTime -= 1;
}else if(cImg < 4){
cImg += 1;
cImgTime = 3;
}else{
cImg = 1;
cImgTime = 3;
}
if(cImg == 1){
image(cf, pos.x, pos.y, 40, 40);
}
if(cImg == 2){
image(cl, pos.x, pos.y, 40, 40);
}
if(cImg == 3){
image(cs, pos.x, pos.y, 40, 40);
}
if(cImg == 4){
image(cr, pos.x, pos.y, 40, 40);
}
}
}
// A "HealthPack" "Item". For refilling player health.
class HealthPack extends Item {/*PLACEHOLDER*/}
// Arrays to hold all the "Item"s in the game.
ArrayList<Coin> coins = new ArrayList<Coin>();
ArrayList<HealthPack> healthPacks = new ArrayList<HealthPack>();
// The "Player" class. Be warned, it's quite large.
class Player {
PVector pos;// Player x and y
PVector vel;// Player x and y movement velocity
int img = 1;// The current player image displayed
int imgCat = 1;// The current player image category displayed
int imgTime = 2;// The time between image switching
int width;// The width of the player
int height;// The height of the player
int health = 100;// The players health or HP.
int hurtTimer = 0;// The timer that tells the program when the player can be invulnerable.
int score = 0;// The player's score.(Coins)
// Sets objects of the player's variables.
public Player() {
pos = new PVector(200, 200);
vel = new PVector(0, 0);
width = 40;
height = 60;
}
public Player(int x, int y) {
pos = new PVector(x, y);
vel = new PVector(0, 0);
width = 40;
height = 60;
}
// Collisions between the player and other items.
public void runCollisions() {
// Player/Block collisions.
for(Block that: blocks) {
// If the player is hitting the block
if(colliding(pos.x, pos.y, width, height, that.pos.x, that.pos.y, that.width, that.height)) {
// And the player is hitting one of the sides
if(abs(pos.x - that.pos.x)/(width + that.width) > abs(pos.y - that.pos.y)/(height + that.height)) {
vel.x = 0;// Set the x velocity of the player to 0.
if(pos.x > that.pos.x) {// If the player x is greater than the block x.
pos.x = that.pos.x + width/2 + that.width/2;// Stop the player.
}
if(pos.x < that.pos.x) {// If the player x is less than the block x.
pos.x = that.pos.x - width/2 - that.width/2;// Stop the player.
}
}
// (Or) And the player is hitting the top or bottom
else if(abs(pos.x - that.pos.x)/(width + that.width) < abs(pos.y - that.pos.y)/(height + that.height)) {
vel.y = 0;// Set the player y velocity to 0.
if(pos.y > that.pos.y) {// If the player y is greater than the block y.
pos.y = that.pos.y + height/2 + that.height/2;// Stop the player.
}
if(pos.y < that.pos.y) {// If the player y is less than the block y.
pos.y = that.pos.y - height/2 - that.height/2;// Stop the player.
}
}
}
}
// Player/Hole collisions.
for(Hole that: holes) {
// If the player and the hole are colliding.
if(colliding(pos.x, pos.y, width, height, that.pos.x, that.pos.y, that.width, that.height)) {
// And the player is hitting the sides of the hole.
if(abs(pos.x - that.pos.x)/(width + that.width) > abs(pos.y - that.pos.y)/(height + that.height)) {
vel.x = 0;// set the player's x velocity to 0.
if(pos.x > that.pos.x) {// If the player x is greater than the hole x.
pos.x = that.pos.x + width/2 + that.width/2;// Stop the player.
}
if(pos.x < that.pos.x) {// If the player x is less than the hole x.
pos.x = that.pos.x - width/2 - that.width/2;// Stop the player.
}
}
// (Or) And the player is hitting the top or bottom of the hole.
else if(abs(pos.x - that.pos.x)/(width + that.width) < abs(pos.y - that.pos.y)/(height + that.height)) {
vel.y = 0;// set the player y velocity to 0.
if(pos.y > that.pos.y) {// If the player y is greater than the hole y.
pos.y = that.pos.y + height/2 + that.height/2;// Stop the player.
}
if(pos.y < that.pos.y) {// If the player y is less than the hole y.
pos.y = that.pos.y - height/2 - that.height/2;// Stop the player.
}
}
}
}
// Player/Spike collisions.
for(Spikes that: spikes) {
// If the player is touching the spike.
if(colliding(pos.x, pos.y, width, height, that.pos.x, that.pos.y, that.width, that.height)) {
if (hurtTimer == 0) {// And the player hasn't been hurt within the last 100 frames.
health -= 15;// take away some health from the player
hurtTimer = 100;// and tell the program that the player was just hurt.
playSound(spikeSound);// As well as playing the sound that plays when a player hits the spike.
}
// Same as block and hole. Difference is that velocity is set to 9 instead.
if(abs(pos.x - that.pos.x)/(width + that.width) > abs(pos.y - that.pos.y)/(height + that.height)) {
if(pos.x > that.pos.x) {
vel.x = 9;
pos.x = that.pos.x + width/2 + that.width/2;
}
if(pos.x < that.pos.x) {
vel.x = -9;
pos.x = that.pos.x - width/2 - that.width/2;
}
}
else if(abs(pos.x - that.pos.x)/(width + that.width) < abs(pos.y - that.pos.y)/(height + that.height)) {
if(pos.y > that.pos.y) {
vel.y = 9;
pos.y = that.pos.y + height/2 + that.height/2;
}
if(pos.y < that.pos.y) {
vel.y = -9;
pos.y = that.pos.y - height/2 - that.height/2;
}
}
}
}
// Player/Coin collisions.
for(Coin that: coins){
// If the player is colliding with the coin.
if(colliding(pos.x, pos.y, width, height, that.pos.x, that.pos.y, that.width, that.height)) {
if(!that.isHit){// And the coin hasn't been collected yet. :P
score += 1;// Increment the player's score
playSound(coinSound);// And play a coin sound.
}
that.isHit = true;// Tell the program the coin has been collected.
}
}
for(SpikeGuy that: spikeGuys) {
if(colliding(pos.x, pos.y, width, height, that.pos.x, that.pos.y, that.width, that.height)) {
if (hurtTimer == 0) {// And the player hasn't been hurt within the last 100 frames.
health -= 15;// take away some health from the player
hurtTimer = 100;// and tell the program that the player was just hurt.
playSound(spikeSound);// As well as playing the sound that plays when a player hits the spike.
}
// Same as block and hole. Difference is that velocity is set to 9 instead.
if(abs(pos.x - that.pos.x)/(width + that.width) > abs(pos.y - that.pos.y)/(height + that.height)) {
if(pos.x > that.pos.x) {
vel.x = 9;
pos.x = that.pos.x + width/2 + that.width/2;
}
if(pos.x < that.pos.x) {
vel.x = -9;
pos.x = that.pos.x - width/2 - that.width/2;
}
}
else if(abs(pos.x - that.pos.x)/(width + that.width) < abs(pos.y - that.pos.y)/(height + that.height)) {
if(pos.y > that.pos.y) {
vel.y = 9;
pos.y = that.pos.y + height/2 + that.height/2;
}
if(pos.y < that.pos.y) {
vel.y = -9;
pos.y = that.pos.y - height/2 - that.height/2;
}
}
}
}
for(Portal that: portals){
// If the player is colliding with the coin.
if(colliding(pos.x, pos.y, width, height, that.pos.x, that.pos.y, that.width, that.height)) {
if(!that.isHit){// And the coin hasn't been collected yet. :P
curLevel[0]++;
}else{
//setLevel();
}
that.isHit = true;// Tell the program the coin has been collected.
}
}
}
// Draw the player.
public void draw() {
noStroke();
imageMode(CORNER);
if(hurtTimer <= 0){
for(int i = 0; i < 50; i++){
fill(255, 255, 255, i);
ellipse(pos.x, pos.y-height/4, height*2-i*2, height*2-i*2);
}
}
if(img == 1 && imgCat == 1){
image(pf1, pos.x - width/2, pos.y - height, width, height*1.5);
}
if(img == 2 && imgCat == 1){
image(pf2, pos.x - width/2, pos.y - height, width, height*1.5);
}
if(img == 3 && imgCat == 1){
image(pf3, pos.x - width/2, pos.y - height, width, height*1.5);
}
if(img == 1 && imgCat == 2){
image(ps1, pos.x - width/2, pos.y - height, width, height*1.5);
}
if(img == 2 && imgCat == 2){
image(ps2, pos.x - width/2, pos.y - height, width, height*1.5);
}
if(img == 3 && imgCat == 2){
image(ps3, pos.x - width/2, pos.y - height, width, height*1.5);
}
if(img == 1 && imgCat == 3){
pushMatrix();
scale(-1, 1);
translate(-pos.x*2, 0);
image(ps1, pos.x - width/2, pos.y - height, width, height*1.5);
popMatrix();
}
if(img == 2 && imgCat == 3){
pushMatrix();
scale(-1, 1);
translate(-pos.x*2, 0);
image(ps2, pos.x - width/2, pos.y - height, width, height*1.5);
popMatrix();
}
if(img == 3 && imgCat == 3){
pushMatrix();
scale(-1, 1);
translate(-pos.x*2, 0);
image(ps3, pos.x - width/2, pos.y - height, width, height*1.5);
popMatrix();
}
if(img == 1 && imgCat == 4){
image(pn1, pos.x - width/2, pos.y - height, width, height*1.5);
}
if(img == 2 && imgCat == 4){
image(pn2, pos.x - width/2, pos.y - height, width, height*1.5);
}
if(img == 3 && imgCat == 4){
image(pn3, pos.x - width/2, pos.y - height, width, height*1.5);
}
fill(214, 17, 17);
rect(-285, -285, health, 15);
fill(0);
textSize(15);
textFont(BIT, 15);
text("SCORE: " + score, -120, -278);
}
public void update() {
vel.mult(0.9);
pos.add(vel);
if(keyPressed&&keyCode==38) {
walking.play();
vel.y -= 0.1;
pos.y -= 4;
imgCat = 4;
if(imgTime > 0){
imgTime--;
}else if(img < 3){
img++;
imgTime = 2;
}else{
img = 1;
imgTime = 2;
}
}else if(keyCode!=40){
vel.y = 0;
}
if(keyPressed&&keyCode==37) {
walking.play();
vel.x -= 0.1;
pos.x -= 4;
imgCat = 3;
if(imgTime > 0){
imgTime--;
}else if(img < 3){
img++;
imgTime = 2;
}else{
img = 1;
imgTime = 2;
}
}else if(keyCode!=39){
vel.x = 0;
}
if(keyPressed&&keyCode==40) {
walking.play();
vel.y += 0.2;
pos.y += 4;
imgCat = 1;
if(imgTime > 0){
imgTime--;
}else if(img < 3){
img++;
imgTime = 2;
}else{
img = 1;
imgTime = 2;
}
}else if(keyCode!=38){
vel.y = 0;
}
if(keyPressed&&keyCode==39) {
walking.play();
vel.x += 0.1;
pos.x += 4;
imgCat = 2;
if(imgTime > 0){
imgTime--;
}else if(img < 3){
img++;
imgTime = 2;
}else{
img = 1;
imgTime = 2;
}
}else if(keyCode!=37){
vel.x = 0;
}
if (hurtTimer > 0) {
hurtTimer--;
}
pos.set(constrain(pos.x, -300 + width/2, 300 - width/2), constrain(pos.y, -300 + height/2, 300 - height/2));
}
public void display() {
runCollisions();
update();
draw();
}
}
// An object of the "Player" class.
Player player;
// A 4D arrray of levels
int[][][][] levels = {
// Level 1
{
// Dimension 1
{
{6,0,0,0,0,0,0,0,0,0,0,0,0,0,8},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,1,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,1,1,0,0,0,0,0,0},
{0,0,0,0,0,0,1,1,0,0,0,0,0,0,0},
{0,0,0,0,0,0,2,1,0,0,0,0,0,0,0},
{0,0,0,0,0,0,2,1,0,0,0,0,0,0,0},
{0,0,0,0,0,0,1,1,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,1,0,1,0,0,0,0,0},
{0,0,0,0,0,0,0,1,1,1,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,6,0,0,4,0,0,6},
},
// Dimension 2
{
{6,0,0,0,0,0,0,0,0,0,0,0,0,0,8},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,1,1,1,2,0,0,0,0,0,0,0},
{0,0,0,0,1,0,0,1,0,0,0,0,0,0,0},
{0,0,0,0,1,0,0,0,0,0,0,4,0,0,0},
{0,0,0,0,2,1,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,6,0,0,0,0,0,6},
}
},
// Level 2
{
{
{6,0,0,1,1,1,1,1,1,1,1,1,0,0,8},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,1,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,1,1,0,0,0,0,0,0},
{0,0,0,0,0,0,1,1,0,0,0,0,0,0,0},
{0,0,0,0,0,0,2,1,0,0,0,0,0,0,0},
{0,0,0,0,0,0,2,1,0,0,0,0,0,0,0},
{0,0,0,0,0,0,1,1,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,1,0,1,0,0,0,0,0},
{0,0,0,0,0,0,0,1,1,1,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,6,0,0,4,0,0,6},
},
}
};
// A function that reads the 4D array and translates it into objects.
void buildLevel(int level) {
int dimension = floor(random(levels[level].length)); // Chooses a random level-layer
for(int y = 0;y < levels[level][dimension].length;y++) {// Iterate through the level's dimension's arrays
for(int x = 0;x < levels[level][dimension][y].length;x++) { // Iterate through each array's items.
switch(levels[level][dimension][y][x]) {
case 1: blocks.add(new Block(x * 40 - 280, y * 40 - 280)); break; // Add blocks
case 2: spikes.add(new Spikes(x * 40 - 280, y * 40 - 280)); break; // Add spikes
case 3: holes.add(new Hole(x * 40 - 280, y * 40 - 280)); break; // Add holes
case 4: spikeGuys.add(new SpikeGuy(x * 40 - 280, y * 40 - 280)); break; // Add Spike Guys
case 5: bloodyBananas.add(new BloodyBanana(x * 40 - 280, y * 40 - 280)); break; // Add Bloody Bananas
case 6: coins.add(new Coin(x * 40 - 280, y * 40 - 280)); break; // Add coins
case 8: portals.add(new Portal(x * 40 - 280, y * 40 - 280)); // Adds the exit
}
}
}
}
// A function for re-reading levels
void setLevel() {
buildLevel(curLevel[0]);
};
// The setup function.
void setup() {
// Makes the game full screen.
fullScreen();
frameRate(60);
// sets the player location.
player = new Player(-200, -200);
// The sounds.
coinSound = new SoundFile(this, "coin.wav");
spikeSound = new SoundFile(this, "metal-small3.wav");
dieSound = new SoundFile(this, "spell.wav");
walkSound = new SoundFile(this, "cloth.wav");
flashSound = new SoundFile(this, "metal-ringing.wav");
menuSong = new SoundFile(this, "Menu_Theme.wav");
menuTheme = new playSong(menuSong);
gameThemeA = new playSong(new SoundFile(this, "Game_Theme_A.wav"));
walking = new soundEffect(walkSound);
// The font.
BIT = createFont("PressStart2P-Regular.ttf", 25);
// The player images.
pf1 = loadImage("f1.png");
pf2 = loadImage("f2.png");
pf3 = loadImage("f3.png");
ps1 = loadImage("l1.png");
ps2 = loadImage("l2.png");
ps3 = loadImage("l3.png");
pn1 = loadImage("n1.png");
pn2 = loadImage("n2.png");
pn3 = loadImage("n3.png");
// The item/tile images.
spike = loadImage("spike.png");
cf = loadImage("coin_f.png");
cl = loadImage("coin_l.png");
cr = loadImage("coin_r.png");
cs = loadImage("coin_s.png");
// The enemy images.
spike_guy = loadImage("spike_guy.png");
spike_guy_l = loadImage("spike_guy_l.png");
spike_guy_d = loadImage("spike_guy_d.png");
spike_guy_r = loadImage("spike_guy_r.png");
bb1 = loadImage("bb1.png");
bb2 = loadImage("bb2.png");
// Reads a level
buildLevel(curLevel[0]);
// Makes the images look good.
noSmooth();
}
// The draw function. For animation.
void draw() {
//Scaling
pushMatrix();
translate(width/2, height/2);// Centers the screen
scale(0.1);// shrinks the screen to 1/10 of the size.
scale(0.1);// shrinks the screen to 1/10 again (even smaller).
int s = min(width/8, height/6);// creates a variable that sets the final size of the screen.
scale(s);// implements the variable that sets the final size of the screen. (screen is now fullscreen).
switch(scene){
case "Menu": {
menuTheme.play();
background(0);
textAlign(CENTER, CENTER);
textFont(BIT);
textSize(width/24);
fill(fadeIn);
text("DUNGEON\nDIMENSIONS", 0, -100);
if(tillFlash <= 0) {
fill(flashCol);
textSize(width/42);
text("PRESS ENTER TO START", 0, 100);
}
if(fadeIn < 255) {
fadeIn += 0.5;
}
if(tillFlash > 0) {
tillFlash--;
}else{
if(flashTime>0){
flashTime--;
}else if(flashCol == color(255)){
flashCol = color(0);
flashTime = 30;
}else{
flashCol = color(255, 255, 255);
playSound(flashSound);
flashTime = 30;
}
if(keyPressed && key == ENTER){
scene = "Game";
}
}
break;
}
case "Paused": {break;}
case "Game": {
gameThemeA.play();
// A black background for the game.
background(0);
fill(0);//White Background Color
noStroke();//Gets rid of ugly lines.
rect(-300, -300, 600, 600);// White Background
// Draws spike objects.
for(Spikes that: spikes) {
that.draw();
}
// Draws block objects.
for(Block that: blocks) {
that.draw();
}
// Draws hole objects.
for(Hole that: holes) {
that.draw();
}
// Draws Spike Guy objects.
for(SpikeGuy that: spikeGuys){
that.draw();
that.runCollisions();
that.update();
}
// Draws and runs Bloody Banana objects.
for(BloodyBanana that: bloodyBananas){
that.draw();
that.runCollisions();
that.update();
}
// Draws Coin objects (if they haven't been collected yet).
for(Coin that: coins){
if(!that.isHit){