-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameOverScene.java
More file actions
500 lines (395 loc) · 12.8 KB
/
Copy pathGameOverScene.java
File metadata and controls
500 lines (395 loc) · 12.8 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
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.io.File;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.BufferedWriter;
/**
The game over scene - shows the score, high score, title,
and play again button
@author Jack Humphries
@version 4/15/2014
*/
public class GameOverScene extends JComponent implements MouseListener
{
/** the amount of space in pixels between the score title
and the score numbers */
public static final int SCORE_SPACE = 60;
/** the amount of space in pixels between the score title
and the high score title */
public static final int SCORE_TITLE_SPACE = 10;
/** the amount of time in milliseconds that the timer waits before
moving each object on screen (changing this number will speed up
or slow down the animation) */
public static final int MOVEMENT_TIME = 5;
/** the amount of time in milliseconds that the timer waits before
redrawing all of the objects on the screen */
public static final int ANIMATION_TIME = 20;
/** the bottom possible y coordinate of the top of clouds */
public static final int BOTTOM_CLOUD_Y = 30;
/** the amount of red in the color of the background (RGB) */
public static final double BACKGROUND_COLOR_R = 0;
/** the amount of green in the color of the background (RGB) */
public static final double BACKGROUND_COLOR_G = 198.0;
/** the amount of blue in the color of the background (RGB) */
public static final double BACKGROUND_COLOR_B = 255.0;
/** the timer that coordinates when objects will be moved on screen */
private Timer movementTimer;
/** the timer that coordinates when objects will be redrawn on screen */
private Timer animationTimer;
/** the path to the file where the high score is stored */
private String highScoreFilePath = "high_score.txt";
/** the width of the scene */
private int width;
/** the height of the scene */
private int height;
/** the user's score from the last play */
private int score;
/** the user's high score */
private int highScore;
/** the background of the scene */
private Rectangle background;
/** the game over title image (Game Over) */
private BufferedImage gameOverImage;
/** the x coordinate of the game over title */
private int gameOverX;
/** the y coordinate of the game over title */
private int gameOverY;
/** the score title image (Score) */
private BufferedImage scoreTitleImage;
/** the high score title image (High Score) */
private BufferedImage highScoreTitleImage;
/** the x coordinate of the score title image */
private int scoreTitleX;
/** the y coordinate of the score title image */
private int scoreTitleY;
/** the x coordinate of the high score title image */
private int highScoreTitleX;
/** the y coordinate of the high score title image */
private int highScoreTitleY;
/** the score display */
private ScoreDisplay scoreDisplay;
/** the high score display */
private ScoreDisplay highScoreDisplay;
/** the replay button image (Replay) */
private BufferedImage replayButtonImage;
/** the x coordinate of the replay button */
private int replayButtonX;
/** the y coordinate of the replay button */
private int replayButtonY;
/** the clouds in the scene */
private Clouds clouds;
/** the main class */
private Main mainClass;
/**
draws the scene
@param g the graphics object
*/
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
float backgroundRed = (float)(BACKGROUND_COLOR_R/255.0);
float backgroundGreen = (float)(BACKGROUND_COLOR_G/255.0);
float backgroundBlue = (float)(BACKGROUND_COLOR_B/255.0);
Color backgroundColor = new Color(backgroundRed,
backgroundGreen, backgroundBlue);
g2.setColor(backgroundColor);
g2.fill(background);
clouds.redrawClouds(g2);
g2.drawImage(gameOverImage, gameOverX, gameOverY, null);
g2.drawImage(scoreTitleImage, scoreTitleX, scoreTitleY, null);
int digitSpace = scoreTitleImage.getWidth() + SCORE_SPACE;
scoreDisplay.drawScore(g2, scoreTitleX + digitSpace, scoreTitleY);
g2.drawImage(highScoreTitleImage, highScoreTitleX, highScoreTitleY,
null);
int highScoreDigitSpace = highScoreTitleImage.getWidth() + SCORE_SPACE;
highScoreDisplay.drawScore(g2, highScoreTitleX + highScoreDigitSpace,
highScoreTitleY);
g2.drawImage(replayButtonImage, replayButtonX, replayButtonY, null);
}
/**
moves/updates the objects on screen
*/
private void moveAndUpdateObjects()
{
clouds.moveClouds();
}
/**
starts animating the game over scene
*/
private void animate()
{
Thread movementThread = new Thread(
new Runnable()
{
public void run()
{
startMovingObjects();
}
});
Thread animationThread = new Thread(
new Runnable()
{
public void run()
{
startAnimating();
}
});
movementThread.start();
animationThread.start();
}
/**
responsible for moving objects every MOVEMENT_TIME milliseconds
*/
private void startMovingObjects()
{
movementTimer = new Timer(MOVEMENT_TIME,
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
moveAndUpdateObjects();
}
});
movementTimer.start();
}
/**
responsible for calling the repaint method
every ANIMATION_TIME milliseconds
*/
private void startAnimating()
{
animationTimer = new Timer(ANIMATION_TIME,
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
repaint();
}
});
animationTimer.start();
}
/**
generates the background of the scene
*/
private void generateBackground()
{
background = new Rectangle(0, 0, width, height);
}
/**
generates the game over title
*/
private void generateGameOverTitle()
{
File gameOverPath = new File("images/game_over.png");
try
{
gameOverImage = ImageIO.read(gameOverPath);
}
catch (Exception exception)
{
}
int gameOverWidth = gameOverImage.getWidth();
int gameOverHeight = gameOverImage.getHeight();
//center the game over title horizontally
gameOverX = (width / 2) - (gameOverWidth / 2);
//position the game over title one fifth of the way down the scene
gameOverY = ((height / 5) * 1) - (gameOverHeight / 2);
}
/**
generates the score title and the score digits
*/
private void generateScore()
{
File scoreTitlePath = new File("images/score_title.png");
try
{
scoreTitleImage = ImageIO.read(scoreTitlePath);
}
catch (Exception exception)
{
}
int totalScoreWidth = scoreTitleImage.getWidth();
scoreDisplay = new ScoreDisplay(score);
//there will be a space of SCORE_SPACE pixels between the score
//title and the score numbers
totalScoreWidth += SCORE_SPACE;
totalScoreWidth += scoreDisplay.getWidth();
scoreTitleX = (width / 2) - (totalScoreWidth / 2);
scoreTitleY = (height / 2) - (scoreTitleImage.getHeight() / 2);
}
/**
generates the high score title and the score digits
*/
private void generateHighScore()
{
File highScoreTitlePath = new File("images/high_score_title.png");
try
{
highScoreTitleImage = ImageIO.read(highScoreTitlePath);
}
catch (Exception exception)
{
}
int totalScoreWidth = highScoreTitleImage.getWidth();
getHighScore();
highScoreDisplay = new ScoreDisplay(highScore);
//there will be a space of SCORE_SPACE pixels between the high score
//title and the score numbers
totalScoreWidth += SCORE_SPACE;
totalScoreWidth += highScoreDisplay.getWidth();
highScoreTitleX = (width / 2) - (totalScoreWidth / 2);
highScoreTitleY = scoreTitleY + scoreTitleImage.getHeight()
+ SCORE_TITLE_SPACE;
}
/**
gets the current high score and replaces it with the user's last score
if the last score is higher
*/
private void getHighScore()
{
File scoreFile = new File(highScoreFilePath);
try
{
Scanner input = new Scanner(scoreFile);
while (input.hasNextInt())
{
highScore = input.nextInt();
}
if (score > highScore)
{
highScore = score;
setHighScore(score);
}
}
catch (Exception exception)
{
highScore = score;
setHighScore(score);
}
}
/**
saves the new high score
@param newHighScore the new high score to save
*/
private void setHighScore(int newHighScore)
{
File scoreFile = new File(highScoreFilePath);
String scoreOutput = Integer.toString(newHighScore);
BufferedWriter scoreWriter = null;
try
{
FileWriter scoreFileWriter = new FileWriter(scoreFile);
scoreWriter = new BufferedWriter(scoreFileWriter);
scoreWriter.write(scoreOutput);
}
catch (Exception exception)
{
}
finally
{
try
{
scoreWriter.close();
}
catch (Exception exception)
{
}
}
}
/**
generates the replay button
*/
private void generateReplayButton()
{
File playButtonPath = new File("images/replay_button.png");
try
{
replayButtonImage = ImageIO.read(playButtonPath);
}
catch (Exception exception)
{
}
//center the replay button horizontally
replayButtonX = (width / 2) - (replayButtonImage.getWidth() / 2);
//position the replay button seven eighths of the way down the scene
replayButtonY = ((height / 8) * 7)
- (replayButtonImage.getHeight() / 2);
}
/**
generates the clouds
*/
private void generateClouds()
{
int topCloudY = height - BOTTOM_CLOUD_Y - Clouds.MAXIMUM_CLOUD_HEIGHT;
clouds = new Clouds(width, topCloudY, BOTTOM_CLOUD_Y);
}
/**
called when the user clicks the mouse,
if the user clicks the region of the replay button, the game restarts
*/
public void mouseClicked(MouseEvent mouseEvent)
{
Point clicked = mouseEvent.getPoint();
Rectangle bounds = new Rectangle(replayButtonX, replayButtonY,
replayButtonImage.getWidth(), replayButtonImage.getHeight());
if (bounds.contains(clicked))
{
removeMouseListener(this);
movementTimer.stop();
animationTimer.stop();
mainClass.displayGameScene();
}
}
/**
this method is not used - it is part of the MouseListener interface
@param mouseEvent the mouse event
*/
public void mouseEntered(MouseEvent mouseEvent)
{
}
/**
this method is not used - it is part of the MouseListener interface
@param mouseEvent the mouse event
*/
public void mouseExited(MouseEvent mouseEvent)
{
}
/**
this method is not used - it is part of the MouseListener interface
@param mouseEvent the mouse event
*/
public void mouseReleased(MouseEvent mouseEvent)
{
}
/**
this method is not used - it is part of the MouseListener interface
@param mouseEvent the mouse event
*/
public void mousePressed(MouseEvent mouseEvent)
{
}
public GameOverScene(int theWidth, int theHeight, int theScore,
Main theMainClass)
{
width = theWidth;
height = theHeight;
score = theScore;
mainClass = theMainClass;
generateBackground();
generateGameOverTitle();
generateScore();
generateHighScore();
generateReplayButton();
generateClouds();
addMouseListener(this);
animate();
}
}