-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPipe.java
More file actions
249 lines (216 loc) · 5.86 KB
/
Copy pathPipe.java
File metadata and controls
249 lines (216 loc) · 5.86 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
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;
import java.io.File;
import javax.imageio.ImageIO;
/**
This represents a pipe in the game
@author Jack Humphries
@version 4/1/2014
*/
public class Pipe
{
/** the width of the pipe */
public static final int WIDTH = 100;
/** the height of the pipe */
public static int HEIGHT;
/** the height of the pipe segment image (see the file),
some of the pipe segment may be off-screen */
public static final int PIPE_SEGMENT_HEIGHT = 750;
/** the height of the hole */
public static final int HOLE_HEIGHT = 190;
/** the padding between the top/bottom of the screen and the pipe hole */
public static final int PADDING = 85;
/** the y coordinate of the top of the hole */
private int holeY;
/** the x coordinate of the left side of the pipe */
private double x;
/** the top of the pipe */
private BufferedImage pipeTop;
/** the bottom of the pipe */
private BufferedImage pipeBottom;
/** specifies if the bird has successfully moved past the pipe */
private boolean pipeCleared;
/**
sets the bottom y coordinate of the hole to a random value
within a range of PADDING pixels + HOLE_HEIGHT pixels from
the bottom, and PADDING pixels from the top
*/
private void setHole()
{
//the hole should start a tad below the top of the screen (PADDING)
int topY = PADDING;
//the hole should start a tad above the bottom of the screen (PADDING)
//additionally, subtract HOLE_HEIGHT from the height so that part of the
//hole is not cut off by the bottom of the screen
int bottomY = HEIGHT - PADDING - HOLE_HEIGHT;
Random gen = new Random();
holeY = gen.nextInt((bottomY - topY) + 1) + topY;
}
/**
generates the graphics for the pipe
*/
private void generatePipe()
{
//the top and bottom of the pipe have to be generated separately
//since there is a hole in the middle of the pipe
File pipeTopPath = new File("images/pipe_top.png");
File pipeBottomPath = new File("images/pipe_bottom.png");
try
{
pipeTop = ImageIO.read(pipeTopPath);
pipeBottom = ImageIO.read(pipeBottomPath);
}
catch (Exception exception)
{
}
}
/**
redraws the pipe
@param g2 the graphics object
*/
public void redrawPipe(Graphics2D g2)
{
int pipeTopY = holeY - pipeTop.getHeight();
g2.drawImage(pipeTop, (int)x, pipeTopY, null);
int pipeBottomY = holeY + HOLE_HEIGHT;
g2.drawImage(pipeBottom, (int)x, pipeBottomY, null);
}
/**
sets the new x coordinate where the pipe will be redrawn
@param newX the new x coordinate
*/
public void setX(double newX)
{
x = newX;
}
/**
sets pipeCleared to true when the bird has successfully moved
past a pipe
*/
public void setPipeCleared()
{
pipeCleared = true;
}
/**
the y coordinate of the bottom of the hole
@return the value of holeY
*/
public int getHoleY()
{
return holeY;
}
/**
the height of the hole
@return the value of HOLE_HEIGHT
*/
public int getHoleHeight()
{
return HOLE_HEIGHT;
}
/**
the y position of either the top pipe segment or the
bottom pipe segment
@param topPart specifies if the y position should be returned for the top
pipe segment (true) or the bottom pipe segment (false)
@return the height of the pipe segment
*/
public int getPipeSegmentYPosition(boolean topPart)
{
if (topPart)
{
return holeY - PIPE_SEGMENT_HEIGHT;
}
else
{
return holeY + HOLE_HEIGHT;
}
}
/**
the x coordinate of the left of the pipe
@return the value of x
*/
public double getPipeXPosition()
{
return x;
}
/**
the width of the pipe
@return the value of WIDTH
*/
public int getPipeWidth()
{
return WIDTH;
}
/**
the height of the pipe
@return the value of HEIGHT
*/
public int getPipeHeight()
{
return HEIGHT;
}
/**
returns the height of either the top segment of the pipe
or the bottom segment
@param topPart specifies if the height returned should be
for the top segment (true) or the bottom segment (false)
@return the height of the pipe segment
*/
public int getPipeSegmentHeight(boolean topPart)
{
if (topPart)
{
return pipeTop.getHeight();
}
else
{
return pipeBottom.getHeight();
}
}
/**
specifies if the bird has successfully moved past the pipe
@return true if the bird has successfully moved past the pipe,
else return false (if the bird has not yet moved past the pipe)
*/
public boolean getPipeCleared()
{
return pipeCleared;
}
/**
returns an integer RGB value of the pixel (x, y) of the pipe in
either the top segment or the bottom segment,
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
@param hitTopPartOfPipe specifies if the pixel should come from the top
pipe segment (true) or the bottom pipe segment (false)
@return the integer pixel
*/
public int getRGB(int rgbX, int rgbY, boolean hitTopPartOfPipe)
{
if ((rgbX < WIDTH && rgbY < PIPE_SEGMENT_HEIGHT)
&& (rgbX >= 0 && rgbY >= 0))
{
if (hitTopPartOfPipe)
{
return pipeTop.getRGB(rgbX, rgbY);
}
else
{
return pipeBottom.getRGB(rgbX, rgbY);
}
}
else
{
return 0;
}
}
public Pipe(double theX, int theHeight)
{
x = theX;
HEIGHT = theHeight;
setHole();
generatePipe();
}
}