-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBird.java
More file actions
339 lines (295 loc) · 8.59 KB
/
Copy pathBird.java
File metadata and controls
339 lines (295 loc) · 8.59 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
import java.awt.*;
import java.io.File;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
/**
This represents the bird in the game
@author Jack Humphries
@version 4/1/2014
*/
public class Bird
{
/** the bird's width */
public static final int WIDTH = 71;
/** the bird's height */
public static final int HEIGHT = 51;
/** the constant that the vertical move distance is set to when
the bird jumps */
public static final double JUMP_VERTICAL_CONSTANT = -5;
/** the ratio by which the vertical move distance is multiplied
by when the bird is jumping (a value under one causes the jump
to decrease) */
public static final double JUMP_RATIO = 0.95;
/** the boundary that the vertical move distance must meet in order for
the bird to transition from jumping to falling */
public static final double FALL_BOUNDARY = -0.4;
/** the constant added to the vertical move distance
when the bird is falling */
public static final double FALL_CONSTANT = 0.04;
/** the number of times the bird must be redrawn before
it changes its wing position */
private int BIRD_FLAP_WAIT = 6;
/** the distance by which the bird moves when it is "bobbing"
each time the frame is redrawn (such as before the user starts playing) */
public static final double BOB_DISTANCE = 0.5;
/** the maximum distance by which the bird can bob in one direction
before it must bob in the other direction */
public static final double MAX_BOB_DISTANCE = 16;
/** the distance the bird has bobbed in one direction so far */
private double currentBobDistance;
/** specifies if the bird is bobbing up or down */
private boolean bobbingUp;
/** the bird's x position */
private double x;
/** the bird's x position when it was first created
(used when resetting the bird) */
private final double originalX;
/** the bird's y position */
private double y;
/** the bird's y position when it was first created
(used when resetting the bird) */
private final double originalY;
/** the current bird image */
private BufferedImage birdImage;
/** the bird image in the flying up position */
private BufferedImage flyingUpBirdImage;
/** the bird image in the flying middle position */
private BufferedImage flyingMidBirdImage;
/** the bird image in the flying down position */
private BufferedImage flyingDownBirdImage;
/** specifies if the game is playing
- true if game is playing, else false */
private boolean playing;
/** the vertical distance by which the bird should move (positive if
the bird is jumping, otherwise negative if the bird is falling */
private double verticalMoveDistance;
/** keeps track of which flying position the bird should be in */
private int animationPosition;
/** the "top" y coordinate that the bird can fly up to
(remember that the coordinate system is inverted in Java) */
private int topY;
/**
generates the graphics for the bird
*/
private void generateBird()
{
File flyingUpBirdPath = new File("images/bird_flap_up.png");
File flyingMidBirdPath = new File("images/bird_flap_mid.png");
File flyingDownBirdPath = new File("images/bird_flap_down.png");
try
{
flyingUpBirdImage = ImageIO.read(flyingUpBirdPath);
flyingMidBirdImage = ImageIO.read(flyingMidBirdPath);
flyingDownBirdImage = ImageIO.read(flyingDownBirdPath);
}
catch (Exception exception)
{
}
}
/**
redraws the bird at (x, y)
@param g2 the graphics object
*/
public void redrawBird(Graphics2D g2)
{
if (animationPosition > BIRD_FLAP_WAIT * 3)
{
animationPosition = 1;
}
if (animationPosition >= 1 && animationPosition <= BIRD_FLAP_WAIT)
{
birdImage = flyingUpBirdImage;
}
else if (animationPosition > BIRD_FLAP_WAIT
&& animationPosition <= BIRD_FLAP_WAIT * 2)
{
birdImage = flyingMidBirdImage;
}
else if (animationPosition > BIRD_FLAP_WAIT * 2
&& animationPosition <= BIRD_FLAP_WAIT * 3)
{
birdImage = flyingDownBirdImage;
}
else if (animationPosition > BIRD_FLAP_WAIT * 3
&& animationPosition <= BIRD_FLAP_WAIT * 4)
{
birdImage = flyingMidBirdImage;
}
g2.drawImage(birdImage, (int)x, (int)y, null);
animationPosition++;
}
/**
moves the bird
*/
public void moveBird()
{
if (playing)
{
if (isJumping())
{
verticalMoveDistance = verticalMoveDistance * JUMP_RATIO;
//make sure the vertical move distance is negative so that
//the bird falls
if (verticalMoveDistance >= 0)
{
verticalMoveDistance = verticalMoveDistance * -1;
}
}
else
{
verticalMoveDistance = verticalMoveDistance + FALL_CONSTANT;
if (verticalMoveDistance < 0)
{
verticalMoveDistance = verticalMoveDistance * -1;
}
}
y = y + verticalMoveDistance;
}
else
{
if (bobbingUp)
{
if (currentBobDistance < MAX_BOB_DISTANCE)
{
y += BOB_DISTANCE;
currentBobDistance += BOB_DISTANCE;
}
else
{
y -= BOB_DISTANCE;
currentBobDistance -= BOB_DISTANCE;
bobbingUp = false;
}
}
else
{
if (Math.abs(currentBobDistance) < MAX_BOB_DISTANCE)
{
y -= BOB_DISTANCE;
currentBobDistance -= BOB_DISTANCE;
}
else
{
y = y + BOB_DISTANCE;
currentBobDistance = currentBobDistance + BOB_DISTANCE;
bobbingUp = true;
}
}
}
}
/**
causes the bird to fly up if it is not at or above its
"top" y coordinate
@return true if the bird flies up, else false if the bird is at the
"top" y coordinate and does not fly up
*/
public boolean flyUp()
{
if (y > topY + HEIGHT)
{
verticalMoveDistance = JUMP_VERTICAL_CONSTANT;
return true;
}
else
{
return false;
}
}
/**
used to find out if the bird is jumping
@return true if the bird is jumping (verticalJumpDistance is greater
than FALL_BOUDNARY), else return false (verticalJumpDistance is
less than or equal to FALL_BOUNDARY)
*/
private boolean isJumping()
{
if (verticalMoveDistance < FALL_BOUNDARY)
{
return true;
}
return false;
}
/**
sets the bird to "playing" by causing the bird
to respond to gravity as the game has started
*/
public void startPlaying()
{
playing = true;
}
/**
resets the bird by causing it to stop responding to user input,
moving it back to its original position, and making it bob
*/
public void reset()
{
playing = false;
x = originalX;
y = originalY;
}
/**
the x coordinate of the bird
@return the x coordinate
*/
public double getXPosition()
{
return x;
}
/**
the y coordinate of the bird
@return the y coordinate
*/
public double getYPosition()
{
return y;
}
/**
the width of the bird
@return the value of WIDTH
*/
public int getWidth()
{
return WIDTH;
}
/**
the height of the bird
@return the value of HEIGHT
*/
public int getHeight()
{
return HEIGHT;
}
/**
returns an integer RGB value of the pixel (x, y) of the bird,
or 0 if the pixel coordinate is out of bounds
@param rgbX the x coordinate of the pixel
@param rgbY the y coordinate of the pixel
@return the integer pixel
*/
public int getRGB(int rgbX, int rgbY)
{
if ((rgbX < WIDTH && rgbY < HEIGHT)
&& (rgbX >= 0 && rgbY >= 0))
{
return birdImage.getRGB(rgbX, rgbY);
}
else
{
return 0;
}
}
public Bird(double theX, double theY, int theTopY)
{
x = theX;
originalX = theX;
y = theY;
originalY = theY;
playing = false;
bobbingUp = true;
verticalMoveDistance = 0;
animationPosition = 1;
topY = theTopY;
birdImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
generateBird();
}
}