-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameDriver.java
405 lines (295 loc) · 10.9 KB
/
GameDriver.java
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
/*
* GameDriver.java
* Collaborators: Tamir Poindexter, Nancy Chen, John Castillo, Ohene Nkansah,
* Mark Zheng, Lawer Nyako
* Catch It, Recycle It
* Started: 2/17/2024
* Finished: 2/18/2024
* Purpose: The GameDriver class extends the GameGUI class and serves the purpose of
* handling the gameplay of Catch It, Recycle It. This class is capable of
* handling user input during runtime, displaying custom sprites, as well as
* managing game variables. See README for GPT documentation.
*/
package hackathon;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import java.io.FileWriter;
import java.io.PrintWriter;
public class GameDriver extends JPanel {
//Miscellaneous Game Variables
private static int score = 0;
private Timer timer;
private int addSpeed = 0;
private volatile int healthPoints = 3;
private volatile boolean lost = false;
private volatile int fallSpeed = 10;
//Frame Attributes
public final int WIDTH = 400;
public final int HEIGHT = 600;
public final Image bgImage = Toolkit.getDefaultToolkit().createImage("src/hackathon/images/frame.png");
public final Image bgImage_scaled = bgImage.getScaledInstance(WIDTH, HEIGHT, Image.SCALE_DEFAULT);
//Trash Image Attributes
private final int TRASH_SIZE = 50;
private volatile int trashY = 0;
private volatile boolean trashMoving = false;
private final String[] TRASH_IMAGE_PATH = {"src/hackathon/images/paper1.png", "src/hackathon/images/paper2.png",
"src/hackathon/images/paper3.png", "src/hackathon/images/plastic1.png",
"src/hackathon/images/plastic2.png", "src/hackathon/images/plastic3.png", "src/hackathon/images/glass1.png",
"src/hackathon/images/glass2.png", "src/hackathon/images/glass3.png", "src/hackathon/images/can1.png",
"src/hackathon/images/can2.png", "src/hackathon/images/can3.png", "src/hackathon/images/elec1.png",
"src/hackathon/images/elec2.png", "src/hackathon/images/elec3.png"};
private volatile int trashX;
private BufferedImage trashImage;
private volatile int trashType;
//Bin Image Attributes
private final int TRASH_BINS = 5;
private final int BIN_HEIGHT = 100;
private final String[] BIN_IMAGE_PATH = {"src/hackathon/images/paperBin.png",
"src/hackathon/images/plasticBin.png", "src/hackathon/images/glassBin.png",
"src/hackathon/images/canBin.png", "src/hackathon/images/elecBin.png",
"src/hackathon/images/xBin.png"};
private final int[] binX = new int[TRASH_BINS];
/**
* @name: GameDriver()
* @purpose: Parameterized constructor for the GameDriver() class.
* @arguments: A boolean specifying whether to run the game
* @returns: None
* @effects: Executes the main game given that run_game is true
*/
public GameDriver(boolean run_game) {
if (!run_game) return;
//Executes & Terminates Catch It, Recycle It
this.startGame();
this.endGame();
}
public void startGame() {
lost = false;
// Create JPanel interface
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setBackground(Color.BLACK);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
// Set the x-position of each bin
for (int i = 0; i < TRASH_BINS; i++) {
binX[i] = (WIDTH / TRASH_BINS) * i;
}
// Uses timer to respawn a new trash element
timer = new Timer(100, e -> {
//Terminates timer if the player has lost
if (lost) {
saveScoreToFile();
setVisible(false);
//this.endGame();
//timer.stop();
}
//Creates trash if none is on screen
if (!trashMoving) {
int section = new Random().nextInt(TRASH_BINS); // Choose a section randomly
trashX = section * (WIDTH / TRASH_BINS) + (WIDTH / TRASH_BINS - TRASH_SIZE) / 2;
trashY = 0;
trashMoving = true;
fallSpeed = 10 + addSpeed;
trashType = new Random().nextInt(TRASH_BINS);
loadTrashImage(trashType);
}
//Moves the trash
else {
trashY += fallSpeed;
if (trashY >= HEIGHT - (TRASH_SIZE/2 + BIN_HEIGHT)) {
trashMoving = false;
checkTrashBinCollision();
}
}
repaint();
if (healthPoints == 0) lost = true;
setVisible(true);
});
timer.start();
}
/**
* @name: endGame()
* @purpose: Remove the current frame when the game ends
* @arguments: None
* @returns: None
* @effects: Sets the current frame's visibility to false
*/
public void endGame() {
setVisible(false);
}
/**
* @name: loadTrashImage()
* @purpose: Load in and set the trashImage variable
* @arguments: An integer which represents the current trashType
* @returns: None
* @effects: Throws an exception if the photo path name in TRASH_IMAGE_PATH
* is invalid, but does not terminate the function.
*/
private void loadTrashImage(int trashType) {
int randImg = new Random().nextInt(3);
String imagePath = TRASH_IMAGE_PATH[trashType * 3 + randImg];
try {
trashImage = ImageIO.read(new File(imagePath));
}
catch (IOException e) {
System.err.println("ERROR: Cannot open image file path \"" + imagePath + "\".\n");
throw new RuntimeException(e.getMessage());
}
}
/**
* @name: paintComponent()
* @purpose: Paint/Draw the components of the game;
* @arguments: A graphics object, g
* @returns: None
* @effects: Calls other functions to draw game components
*/
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(bgImage_scaled, 0, 0, null);
drawTrash(g);
drawTrashBins(g);
}
/**
* @name: drawTrashBins()
* @purpose: Handle the drawing of the trash bins
* @arguments: A graphics object, g
* @returns: None
* @effects: None
*/
private void drawTrashBins(Graphics g) {
BufferedImage binImage;
int BIN_WIDTH = WIDTH / TRASH_BINS;
for (int i = 0; i < TRASH_BINS; i++) {
try {
binImage = ImageIO.read(new File(BIN_IMAGE_PATH[i]));
g.drawImage(binImage, binX[i], HEIGHT - BIN_HEIGHT, BIN_WIDTH, BIN_HEIGHT, this);
}
catch (IOException e) {
System.err.println("Cannot find image path \"" + BIN_IMAGE_PATH[i] + "\".\n");
throw new RuntimeException(e.getMessage());
}
}
}
/**
* @name: drawTrash()
* @purpose: Handle the drawing of the trash
* @arguments: A graphics object, g
* @returns: None
* @effects: Only draws if there is a trash on screen & an image for it
*/
private void drawTrash(Graphics g) {
if (trashMoving && trashImage != null) {
g.drawImage(trashImage, trashX, trashY, TRASH_SIZE, TRASH_SIZE, this);
}
}
/**
* @name: checkTrashBinCollision()
* @purpose: Handles trash bin and trash collision
* @arguments: A graphics object, g
* @returns: None
* @effects: None
*/
private void checkTrashBinCollision() {
int binIndex = trashX / (WIDTH / TRASH_BINS);
if (binIndex == trashType) {
score++;
GameGUI.GamePanel.updateScore();
}
else {
healthPoints--;
String binName;
if (trashType == 0) binName = "paper";
else if (trashType == 1) binName = "plastic";
else if (trashType == 2) binName = "glass";
else if (trashType == 3) binName = "cans/aluminum";
else binName = "electric waste";
System.out.println("Wrong! That trash belongs in the " + binName + " bin!");
}
if (score > 0 && score % 5 == 0) fallSpeed += 5;
}
/**
* @name: moveLeft()
* @purpose: Handles left arrow key inputs
* @arguments: None
* @returns: None
* @effects: Repaints assets when called
*/
public void moveLeft() {
if (trashY <= HEIGHT - (BIN_HEIGHT + TRASH_SIZE)) {
if (trashX > 0) {
if (!(trashX - 80 < 0)) trashX -= 80;
}
}
repaint();
}
/**
* @name: moveRight()
* @purpose: Handle right arrow key inputs
* @arguments: None
* @returns: None
* @effects: Repaints assets when called
*/
public void moveRight() {
if (trashY <= HEIGHT - (BIN_HEIGHT + TRASH_SIZE)) {
if (trashX < WIDTH - TRASH_SIZE) {
if (!(trashX + 80 > WIDTH)) trashX += 80;
}
}
repaint();
}
/**
* @name: moveDown()
* @purpose: Handles down arrow key inputs
* @arguments: None
* @returns: None
* @effects: Repaints assets when called
*/
public void moveDown() {
if (trashY <= HEIGHT - (BIN_HEIGHT + TRASH_SIZE)) fallSpeed += 5;
repaint();
}
/**
* @name: getScore()
* @purpose: Returns the score
* @arguments: None
* @returns: An integer, representing the score
* @effects: None
*/
public static int getScore() {
return score;
}
/**
* @name: stillPlaying()
* @purpose: Return whether the play() function is active
* @arguments: None
* @returns: A boolean, specifying if the play() function is active
* @effects: None
*/
public boolean stillPlaying() {
return !lost;
}
/**
* @name: saveScoreToFile()
* @purpose: Save the current score to the specified file
* @arguments: A string, representing the name of the file to save the score to
* @returns: None
* @effects: Writes to, or creates a file within the current file structure.
* Throws an exception if the file could not be opened/written to.
*/
private void saveScoreToFile() {
try (PrintWriter writer = new PrintWriter(new FileWriter("Scores.txt", true))) {
writer.println(score);
writer.close();
System.out.println("Score saved to file.");
}
catch (IOException e) {
System.err.println("Error saving score to file \"Scores.txt\"\n");
throw new RuntimeException(e.getMessage());
}
}
}