-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTitleScene.java
More file actions
401 lines (318 loc) · 9.97 KB
/
Copy pathTitleScene.java
File metadata and controls
401 lines (318 loc) · 9.97 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
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.*;
/**
The title scene - includes the title and the play button
@author Jack Humphries
@version 4/15/2014
*/
public class TitleScene extends JComponent implements MouseListener
{
/** the x coordinate of the bird */
public static final int BIRD_X = 200;
/** 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 vertical space between the title and author images */
public static final int VERTICAL_TITLE_SPACE = 10;
/** the amount of red in the color of the background (RGB) */
public static final double BACKGROUND_COLOR_R = 0.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 width of the scene */
private int width;
/** the height of the scene */
private int height;
/** the background of the scene */
private Rectangle background;
/** the game title image (Flappy Bird) */
private BufferedImage titleImage;
/** the x coordinate of the title image */
private int titleX;
/** the y coordinate of the title image */
private int titleY;
/** the author image (Jack Humphries) */
private BufferedImage authorImage;
/** the x coordinate of the author image */
private int authorX;
/** the y coordinate of the author image */
private int authorY;
/** the original author image (Designed by Doug Nguyen) */
private BufferedImage originalAuthorImage;
/** the x coordinate of the original author image */
private int originalAuthorX;
/** the y coordinate of the original author image */
private int originalAuthorY;
/** the play button image (Play) */
private BufferedImage playButtonImage;
/** the x coordinate of the play button */
private int playButtonX;
/** the y coordinate of the play button */
private int playButtonY;
/** the bird displayed on the title scene */
private Bird bird;
/** 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);
bird.redrawBird(g2);
g2.drawImage(titleImage, titleX, titleY, null);
g2.drawImage(authorImage, authorX, authorY, null);
g2.drawImage(originalAuthorImage, originalAuthorX, originalAuthorY,
null);
g2.drawImage(playButtonImage, playButtonX, playButtonY, null);
}
/**
moves/updates the objects on screen
*/
private void moveAndUpdateObjects()
{
bird.moveBird();
clouds.moveClouds();
}
/**
starts animating the title 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 title image
*/
private void generateTitle()
{
File titlePath = new File("images/title.png");
try
{
titleImage = ImageIO.read(titlePath);
}
catch (Exception exception)
{
}
int titleWidth = titleImage.getWidth();
int titleHeight = titleImage.getHeight();
//center the title horizontally
titleX = (width / 2) - (titleWidth / 2);
//position the title two sevenths of the way down the scene
titleY = ((height / 7) * 2) - (titleHeight / 2);
}
/**
generates the author image
*/
private void generateAuthor()
{
File authorPath = new File("images/author.png");
try
{
authorImage = ImageIO.read(authorPath);
}
catch (Exception exception)
{
}
authorX = titleX + titleImage.getWidth() - authorImage.getWidth();
authorY = titleY + titleImage.getHeight() + VERTICAL_TITLE_SPACE;
}
/**
generates the original author image
*/
private void generateOriginalAuthor()
{
File originalAuthorPath = new File("images/original_author.png");
try
{
originalAuthorImage = ImageIO.read(originalAuthorPath);
}
catch (Exception exception)
{
}
originalAuthorX = titleX + titleImage.getWidth()
- originalAuthorImage.getWidth();
originalAuthorY = authorY + authorImage.getHeight()
+ VERTICAL_TITLE_SPACE;
}
/**
creates the bird
*/
private void createBird()
{
int y = ((originalAuthorY + originalAuthorImage.getHeight())
+ playButtonY) / 2;
y -= Bird.HEIGHT / 2;
bird = new Bird(BIRD_X, y, 0);
}
/**
generates the clouds
*/
private void generateClouds()
{
int topCloudY = height - BOTTOM_CLOUD_Y - Clouds.MAXIMUM_CLOUD_HEIGHT;
clouds = new Clouds(width, topCloudY, BOTTOM_CLOUD_Y);
}
/**
generates the play button image
*/
private void generatePlayButton()
{
File playButtonPath = new File("images/play_button.png");
try
{
playButtonImage = ImageIO.read(playButtonPath);
}
catch (Exception exception)
{
}
//center the play button horizontally
playButtonX = (width / 2) - (playButtonImage.getWidth() / 2);
//position the play button four fifths of the way down the scene
playButtonY = ((height / 5) * 4) - (playButtonImage.getHeight() / 2);
}
/**
called when the user clicks the mouse,
if the user clicks the region of the play button, the game starts
@param mouseEvent the mouse event
*/
public void mouseClicked(MouseEvent mouseEvent)
{
Point clicked = mouseEvent.getPoint();
Rectangle bounds = new Rectangle(playButtonX, playButtonY,
playButtonImage.getWidth(), playButtonImage.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 TitleScene(int theWidth, int theHeight, Main theMainClass)
{
width = theWidth;
height = theHeight;
mainClass = theMainClass;
generateBackground();
generateTitle();
generateAuthor();
generateOriginalAuthor();
generatePlayButton();
createBird();
generateClouds();
addMouseListener(this);
animate();
}
}