-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.java
More file actions
631 lines (607 loc) · 23.7 KB
/
Copy pathGame.java
File metadata and controls
631 lines (607 loc) · 23.7 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
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
import processing.core.PApplet;
import processing.core.PImage;
import processing.event.KeyEvent;
import processing.sound.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Game extends PApplet {
float tintNum = 210;
private static int coins;
private static int windowLength = 500;
private static int windowWidth = 650;
private static int gameState = 0;
private static int startingScreen = 0;
private static int instructScreen = 1;
private static int playScreen = 2;
private static int shopScreen = 3;
private static int endingScreen = 4;
private static int lastTickTime = 0;
private static int timer = 80;
private static int mazeWidth = 20;
private static int mazeLength = 20;
private static Cell[][] gameGrid;
private static int health = 1000;
private static int playerX = 0;
private static int playerY = 0;
private static int cellSize = 20;
private static int bishopMoves = 0;
private static boolean bishopBought = false;
private static boolean tpBought = false;
private static int boosterBought = 0;
private static boolean reductionBought = false;
final int healthBooster = 50;
private static int bandageReduction = 0;
private static int damage = 5;
private static int efficientSteps = 0;
private static int levelPassed = 1;
private static int timeDelay = 200;
private static int totalTime = 80;
private static int totalSteps = 0;
PImage background;
SoundFile music;
public void startingScreen()
{
tint(255, tintNum);
image(background, 0, 0, 650, 500);
fill(110);
rect(150, 300, 350, 80);
fill(255,0,0);
textSize(50);
text("Maze survivor", 180, 180);
textSize(30);
text("Instructions", 250, 350);
}
public void instructScreen()
{
tint(255, tintNum);
image(background, 0, 0, 650, 500);
fill(110);
rect(150, 300, 350, 80);
fill(255,0,0);
textSize(18);
text("You suffered a horrible injury that causes you to bleed whenever you move, \nbut you are stuck in this maze and you have to be precise with your movements and\nsteps. When you finish a maze, your remaining health converts to coins which can be\nused to purchase many powerups in the shop. GOODLUCK.\n\nControls are WAXD for vertical and horizontal\nQEZC for diagonal for bishop potion\nThere's random kits and clocks to boost you.\nCollect them if you can.", 10, 120);
textSize(30);
text("Play", 300, 350);
}
public void shopScreen()
{
tint(255, tintNum);
image(background, 0, 0, 650, 500);
fill(110);
rect(40, 280, 250, 70);
rect(360, 280, 250, 70);
rect(40, 390, 250, 70);
rect(360, 390, 250, 70);
fill(255,0,0);
textSize(50);
text("Coins: " + coins, 40, 80);
textSize(80);
text("SHOP", 230, 200);
textSize(28);
text("Healthboost $250", 55, 325);
text("Bandage $250", 400, 325);
text("Teleportation $100", 55, 435);
text("Bishop Potion $800", 365, 435);
fill(255, 0, 0);
rect(265, 220, 120, 40);
fill(255);
text("Exit", 305, 250);
textSize(20);
text("That maze could've taken "+ efficientSteps + " amount of steps", 10, 20);
}
public void gameScreenPlay()
{
if (health > 0 && (this.key == 'w' || this.key == 'W') && playerY > 0 && (!gameGrid[playerY][playerX].hasTopWall() || !gameGrid[playerY - 1][playerX].hasBottomWall())) {
if (gameGrid[playerY - 1][playerX].isExit()) {
coins += health;
gameState = shopScreen;
gameGrid[playerY][playerX].removePlayer();
timer = totalTime;
levelPassed++;
timeDelay += 20;
totalTime--;
totalSteps++;
}
else if(gameGrid[playerY - 1][playerX].hasHealthKit())
{
health += 80;
gameGrid[playerY - 1][playerX].changeHealthKit(false);
}
else if(gameGrid[playerY - 1][playerX].hasClock())
{
timer += 15;
gameGrid[playerY - 1][playerX].changeClock(false);
}
gameGrid[playerY][playerX].removePlayer();
playerY--;
health -= (damage - bandageReduction);
gameGrid[playerY][playerX].makePlayer();
totalSteps++;
delay(timeDelay);
}
if (health > 0 && (this.key == 'x' || this.key == 'X') && playerY < mazeLength - 1 && (!gameGrid[playerY][playerX].hasBottomWall() || !gameGrid[playerY + 1][playerX].hasTopWall())) {
if (gameGrid[playerY + 1][playerX].isExit()) {
System.out.println("i did it");
coins += health;
gameState = shopScreen;
gameGrid[playerY][playerX].removePlayer();
timer = totalTime;
levelPassed++;
timeDelay += 20;
totalTime--;
totalSteps++;
}
else if(gameGrid[playerY + 1][playerX].hasHealthKit())
{
health += 80;
gameGrid[playerY + 1][playerX].changeHealthKit(false);
}
else if(gameGrid[playerY + 1][playerX].hasClock())
{
timer += 15;
gameGrid[playerY + 1][playerX].changeClock(false);
}
gameGrid[playerY][playerX].removePlayer();
playerY++;
health -= (damage - bandageReduction);
gameGrid[playerY][playerX].makePlayer();
totalSteps++;
delay(timeDelay);
}
if (health > 0 && (this.key == 'd' || this.key == 'D') && playerX < mazeWidth - 1 && (!gameGrid[playerY][playerX].hasRightWall() || !gameGrid[playerY][playerX + 1].hasLeftWall())) {
if (gameGrid[playerY][playerX + 1].isExit()) {
System.out.println("i did it");
coins += health;
gameState = shopScreen;
gameGrid[playerY][playerX].removePlayer();
timer = totalTime;
levelPassed++;
timeDelay += 20;
totalTime--;
totalSteps++;
}
else if(gameGrid[playerY][playerX + 1].hasHealthKit())
{
health += 80;
gameGrid[playerY][playerX + 1].changeHealthKit(false);
}
else if(gameGrid[playerY][playerX + 1].hasClock())
{
timer += 15;
gameGrid[playerY][playerX + 1].changeClock(false);
}
gameGrid[playerY][playerX].removePlayer();
playerX++;
gameGrid[playerY][playerX].makePlayer();
health -= (damage - bandageReduction);
totalSteps++;
delay(timeDelay);
}
if (health > 0 && (this.key == 'a' || this.key == 'A') && playerX > 0 && (!gameGrid[playerY][playerX].hasLeftWall() || !gameGrid[playerY][playerX - 1].hasRightWall())) {
if (gameGrid[playerY][playerX - 1].isExit()) {
System.out.println("i did it");
coins += health;
gameState = shopScreen;
gameGrid[playerY][playerX].removePlayer();
timer = totalTime;
levelPassed++;
timeDelay += 20;
totalTime--;
totalSteps++;
}
else if(gameGrid[playerY][playerX - 1].hasHealthKit())
{
health += 80;
gameGrid[playerY][playerX - 1].changeHealthKit(false);
}
else if(gameGrid[playerY][playerX - 1].hasClock())
{
timer += 15;
gameGrid[playerY][playerX - 1].changeClock(false);
}
gameGrid[playerY][playerX].removePlayer();
playerX--;
gameGrid[playerY][playerX].makePlayer();
health -= (damage - bandageReduction);
totalSteps++;
delay(timeDelay);
}
if(health > 0 && (this.key == 'q' || this.key == 'Q') && bishopMoves > 0 && playerX > 0 && playerY > 0)
{
if(gameGrid[playerY - 1][playerX - 1].hasHealthKit())
{
health += 80;
gameGrid[playerY - 1][playerX - 1].changeHealthKit(false);
}
else if(gameGrid[playerY - 1][playerX - 1].hasClock())
{
timer += 15;
gameGrid[playerY - 1][playerX - 1].changeClock(false);
}
gameGrid[playerY][playerX].removePlayer();
playerX--;
playerY--;
gameGrid[playerY][playerX].makePlayer();
health -= (damage - bandageReduction);
bishopMoves--;
if(bishopMoves == 0)
{
bishopBought = false;
}
totalSteps++;
delay(timeDelay);
}
if(health > 0 && (this.key == 'e' || this.key == 'E') && bishopMoves > 0 && playerX < mazeWidth - 1 && playerY > 0)
{
if(gameGrid[playerY - 1][playerX + 1].hasHealthKit())
{
health += 80;
gameGrid[playerY - 1][playerX + 1].changeHealthKit(false);
}
else if(gameGrid[playerY - 1][playerX + 1].hasClock())
{
timer += 15;
gameGrid[playerY - 1][playerX + 1].changeClock(false);
}
gameGrid[playerY][playerX].removePlayer();
playerX++;
playerY--;
gameGrid[playerY][playerX].makePlayer();
health -= (damage - bandageReduction);
bishopMoves--;
if(bishopMoves == 0)
{
bishopBought = false;
}
totalSteps++;
delay(timeDelay);
}
if(health > 0 && (this.key == 'z' || this.key == 'Z') && bishopMoves > 0 && playerX > 0 && playerY < mazeLength - 1)
{
if(gameGrid[playerY + 1][playerX - 1].hasHealthKit())
{
health += 80;
gameGrid[playerY + 1][playerX - 1].changeHealthKit(false);
}
else if(gameGrid[playerY + 1][playerX - 1].hasClock())
{
timer += 15;
gameGrid[playerY + 1][playerX - 1].changeClock(false);
}
gameGrid[playerY][playerX].removePlayer();
playerX--;
playerY++;
gameGrid[playerY][playerX].makePlayer();
health -= (damage - bandageReduction);
bishopMoves--;
if(bishopMoves == 0)
{
bishopBought = false;
}
totalSteps++;
delay(timeDelay);
}
if(health > 0 && (this.key == 'c' || this.key == 'C') && bishopMoves > 0 && playerX < mazeWidth - 1 && playerY < mazeLength - 1)
{
if (gameGrid[playerY + 1][playerX + 1].isExit()) {
System.out.println("i did it");
coins += health;
gameState = shopScreen;
gameGrid[playerY][playerX].removePlayer();
timer = totalTime;
levelPassed++;
timeDelay += 20;
totalTime--;
totalSteps++;
}
else if(gameGrid[playerY + 1][playerX + 1].hasHealthKit())
{
health += 80;
gameGrid[playerY + 1][playerX + 1].changeHealthKit(false);
}
else if(gameGrid[playerY + 1][playerX + 1].hasClock())
{
timer += 15;
gameGrid[playerY + 1][playerX + 1].changeClock(false);
}
gameGrid[playerY][playerX].removePlayer();
playerX++;
playerY++;
gameGrid[playerY][playerX].makePlayer();
health -= (damage - bandageReduction);
bishopMoves--;
if(bishopMoves == 0)
{
bishopBought = false;
}
totalSteps++;
delay(timeDelay);
}
background(220);
tint(255, 255);
image(background, 0, 0, 650, 500);
for (Cell[] row : gameGrid) {
for (Cell c : row) {
c.display();
}
}
textSize(16);
fill(0, 255, 0);
text("Health: " + health, 515, 25);
fill(255, 215, 0);
text("Coins: " + coins, 515, 50);
fill(255);
text("Level: " + levelPassed, 515, 75);
text("Steps taken: " + totalSteps, 515, 100);
text("Timer: " + timer, 500, 500);
timerTick();
if(timer == 0 || health == 0)
{
gameState = endingScreen;
}
}
public void endingScreen()
{
fill(0);
rect(0,0,650,500);
textSize(100);
fill(255,0,0);
text("YOU DIED.", 100, 250);
}
public void draw() {
switch(gameState)
{
case 0:
startingScreen();
break;
case 1:
instructScreen();
break;
case 2:
background(255);
noTint();
gameScreenPlay();
break;
case 3:
shopScreen();
break;
case 4:
endingScreen();
break;
}
}
public void mousePressed()
{
if(gameState == startingScreen)
{
if(mouseX > 150 && mouseX < 500 && mouseY > 300 && mouseY < 380)
{
gameState = instructScreen;
}
}
else if(gameState == instructScreen)
{
if(mouseX > 150 && mouseX < 500 && mouseY > 300 && mouseY < 380)
{
gameState = playScreen;
setup();
}
}
else if(gameState == shopScreen)
{
if(mouseX > 40 && mouseX < 290 && mouseY > 280 && mouseY < 350)
{
if(coins >= 250)
{
boosterBought++;
coins -= 250;
System.out.println("I bought booster");
}
}
if(mouseX > 360 && mouseX < 610 && mouseY > 280 && mouseY < 350)
{
if(coins >= 250)
{
if(bandageReduction < 5 )
{
reductionBought = true;
coins -= 250;
bandageReduction++;
System.out.println("I bought bandage");
}
}
}
if(mouseX > 40 && mouseX < 290 && mouseY > 390 && mouseY < 460)
{
if(coins >= 100)
{
tpBought = true;
coins -= 100;
System.out.println("I bought tp");
}
}
if(mouseX > 360 && mouseX < 610 && mouseY > 390 && mouseY < 460)
{
if(coins >= 800)
{
bishopBought = true;
bishopMoves += 3;
coins -= 800;
System.out.println("I bought bishop");
}
}
if(mouseX > 265 && mouseX < 385 && mouseY > 220 && mouseY < 260)
{
gameState = playScreen;
playerX = 0;
playerY = 0;
efficientSteps = 0;
setup();
}
}
}
public void generateMaze(int x, int y) {
gameGrid[y][x].makeVisited(); // This is to mark that this cell is visited, prevents future visits to this cell
List<int[]> neighbors = new ArrayList<int[]>(); // This is to create a list to keep track of every neighboring cells
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
if (Math.abs(i) + Math.abs(j) == 1) // This goes through every single neighboring cell and makes sure its not checking diagonal cells
{
int newX = x + i;
int newY = y + j;
if (newX >= 0 && newY >= 0 && newX < mazeWidth && newY < mazeLength && !gameGrid[newY][newX].isVisited()) // I'm using this to check for boundaries (to see if the new coordinates can be there)
{
neighbors.add(new int[]{newX, newY}); // I'm adding the newCoordinates part of the visited list
}
}
}
}
Collections.shuffle(neighbors); // This will randomize the order of the neighbor list (basically making sure every maze will be random)
for (int i = 0; i < neighbors.size(); i++) { //Loops through the whole neighbor lists and gets their coordinates
int newX = neighbors.get(i)[0];
int newY = neighbors.get(i)[1];
if (!gameGrid[newY][newX].isVisited()) { //If the new coordinates are x different from the original coordinates, it will change which wall is blocked or not
if (newX == x + 1) {
gameGrid[y][x].changeRightWall(false);
gameGrid[newY][newX].changeLeftWall(false);
} else if (newX == x - 1) {
gameGrid[y][x].changeLeftWall(false);
gameGrid[newY][newX].changeRightWall(false);
} else if (newY == y + 1) {
gameGrid[y][x].changeBottomWall(false);
gameGrid[newY][newX].changeTopWall(false);
} else if (newY == y - 1) {
gameGrid[y][x].changeTopWall(false);
gameGrid[newY][newX].changeBottomWall(false);
}
generateMaze(newX, newY); //Recursive call to do it again
}
}
}
public int recursiveSteps(int x, int y, int steps, int bishopRemaining) {
if(x < 0 || x >= mazeWidth || y < 0 || y >= mazeLength || gameGrid[y][x].isVisited()) // Base case (if its out of bounds)
{
return Integer.MAX_VALUE;
}
if(gameGrid[y][x].isExit()) // Base case again (if exit is found)
{
return steps;
}
gameGrid[y][x].makeVisited(); // Mark visited to prevent future cycles
int[][] directions = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}; // This is all possible directions of neighboring cells could be
int minSteps = Integer.MAX_VALUE; // Takes into account if the maze ever would take max integer value (it won't)
for(int[] direction : directions) // Loops through every single possibilities of where the path should go
{
int newX = x + direction[0]; // Makes the new coordinates to easily use again later in the method
int newY = y + direction[1];
int currSteps;
if(newX >= 0 && newY >= 0 && newX < mazeWidth && newY < mazeLength) // Checks for the boundaries again
{
Cell currCell = gameGrid[y][x]; // Initializing these so its easier to access later in the method
Cell newCell = gameGrid[newY][newX];
if((direction[0] == 1 && direction[1] == 0 &&!currCell.hasRightWall() && !newCell.hasLeftWall())
|| (direction[0] == -1 && direction[1] == 0 && !currCell.hasLeftWall() && !newCell.hasRightWall()) ||
(direction[0] == 0 && direction[1] == 1 && !currCell.hasBottomWall() && !newCell.hasTopWall())
|| (direction[0] == 0 && direction[1] == -1 && !currCell.hasTopWall() && !newCell.hasBottomWall())
|| (bishopRemaining > 0 && Math.abs(direction[0]) == 1 && Math.abs(direction[1]) == 1)) // if its possible to move to certain place (walls considered)
{
int newSteps = steps + 1; // Adds in the new step
if(Math.abs(direction[0]) == 1 && Math.abs(direction[1]) == 1 && bishopRemaining > 0) // Checks if its moving diagonally
{
currSteps = recursiveSteps(newX, newY, newSteps, bishopRemaining - 1); // If it is diagonally moving, minus 1 bishop move
}
else{
currSteps = recursiveSteps(newX, newY, newSteps, bishopRemaining); // If horizontal or vertical movement, do this
}
if(currSteps < minSteps) // Checks if this new path is efficient and make it the temp. shortest path until a new one is found
{
minSteps = currSteps;
}
}
}
}
gameGrid[y][x].notVisited(); // Key backtracking call, so that when we go back, we can go back to this cell without rejecting it
return minSteps; // returns the shortest
}
public void gridStart() {
for (int row = 0; row < mazeWidth; row++) {
for (int col = 0; col < mazeLength; col++) {
gameGrid[row][col] = new Cell(this, col, row);
}
}
}
public void gridAllFalse()
{
for (int row = 0; row < mazeWidth; row++) {
for (int col = 0; col < mazeLength; col++) {
gameGrid[row][col].notVisited();
}
}
}
public void settings() {
size(windowWidth, windowLength);
pixelDensity(displayDensity());
}
public void setup() {
music = new SoundFile(this,"assets/music.mp3");
music.play();
size(650, 500);
background = loadImage("images/game background.jpeg");
tint(255, tintNum);
image(background, 0, 0, 650, 500);
gameGrid = new Cell[mazeWidth][mazeLength];
gridStart();
if(tpBought)
{
playerX = (int) (Math.random() * mazeWidth - 1);
playerY = (int) (Math.random() * mazeLength - 1);
gameGrid[playerY][playerX].makePlayer();
tpBought = false;
}
else{
gameGrid[0][0].makePlayer();
}
int ranX1 = (int)(Math.random() * mazeWidth - 1);
int ranY1 = (int)(Math.random() * mazeLength - 1);
int ranX2 = (int)(Math.random() * mazeWidth - 1);
int ranY2 = (int)(Math.random() * mazeLength - 1);
while((ranX1 == playerX && ranY1 == playerY) || (ranX1 == mazeWidth - 1) && (ranY1 == mazeLength - 1))
{
ranX1 = (int)(Math.random() * mazeWidth - 1);
ranY1 = (int)(Math.random() * mazeLength - 1);
}
while((ranX2 == playerX && ranY2 == playerY) || (ranX2 == mazeWidth - 1) && (ranY2 == mazeLength - 1))
{
ranX2 = (int)(Math.random() * mazeWidth - 1);
ranY2 = (int)(Math.random() * mazeLength - 1);
}
gameGrid[ranY1][ranX1].changeHealthKit(true);
gameGrid[ranY2][ranX2].changeClock(true);
gameGrid[mazeLength - 1][mazeWidth - 1].makeExit();
health = 1000 + (boosterBought * 150);
generateMaze(0, 0);
gridAllFalse();
if(bishopBought)
{
System.out.println("hello");
efficientSteps = recursiveSteps(playerX, playerY, 0, bishopMoves);
}
else{
System.out.println("hello");
efficientSteps = recursiveSteps(playerX, playerY, 0, 0);
}
}
public void timerTick() {
if (millis() - lastTickTime >= 1000) {
timer--;
lastTickTime = millis();
if (timer <= 0) {
health = 0;
}
}
}
public static void main(String[] args) {
PApplet.main("Game");
}
}