|
001 packagepackage is used to name the directory or folder a class is in scg.ch06;
002
003 importimport means to make the classes and/or packages available in this program fang2.core.Game;
004 importimport means to make the classes and/or packages available in this program fang2.core.Sprite;
005 importimport means to make the classes and/or packages available in this program fang2.sprites.OvalSprite;
006
007 /**
008 * The PongBall is an augmented {open braces start code blocks and must be matched with a close brace@link fang2.sprites.OvalSprite
009 * OvalSprite}close braces end code blocks and must match an earlier open brace which has a velocity, an advance method which applies
010 * that velocity, and the ability to bounce off of the walls of the game
011 * and other sprites.
012 */
013 publicpublic is used to indicate unrestricted access (any other class can have access) classclass is a group of fields and methods used for making objects PongBall
014 extendsextends means to customize or extend the functionality of a class OvalSprite {open braces start code blocks and must be matched with a close brace
015 /**
016 * current velocity in screens/second; decomposed into x-coordinate
017 * and y-coordinate velocities
018 */
019 privateprivate is used to restrict access to the current class only doubledouble is the type for numbers that can contain decimal fractions deltaX;
020 privateprivate is used to restrict access to the current class only doubledouble is the type for numbers that can contain decimal fractions deltaY;
021
022 /**
023 * Create a newnew is used to create objects by calling the constructor PongBall. Specify the size and the initial velocity of
024 * the ball.
025 *
026 * @paramthis is the Javadoc tag for documenting the purpose of parameters width width of the ball in screens
027 * @paramthis is the Javadoc tag for documenting the purpose of parameters height height of the ball in screens
028 * @paramthis is the Javadoc tag for documenting the purpose of parameters deltaX initial horizontal velocity in screens per second
029 * @paramthis is the Javadoc tag for documenting the purpose of parameters deltaY initial vertical velocity in screens per second
030 */
031 publicpublic is used to indicate unrestricted access (any other class can have access) PongBall(doubledouble is the type for numbers that can contain decimal fractions width, doubledouble is the type for numbers that can contain decimal fractions height,
032 doubledouble is the type for numbers that can contain decimal fractions deltaX, doubledouble is the type for numbers that can contain decimal fractions deltaY) {open braces start code blocks and must be matched with a close brace
033 super(width, height);
034 // A scope hole: two detltaX are in scope (line 9, line 25)
035 // Deepest nested block in scope wins: line 25
036 // this.deltaX refers to the field (line 9)
037 thisthis means the current object (the implicit parameter).deltaX =this assignment operator makes the left side equal to the right side deltaX;// set FIELD to the parameter value
038 thisthis means the current object (the implicit parameter).deltaY =this assignment operator makes the left side equal to the right side deltaY;// set FIELD to the parameter value
039 }close braces end code blocks and must match an earlier open brace
040
041 /**
042 * Create a newnew is used to create objects by calling the constructor PongBall. Specify the size of the ball.
043 *
044 * @paramthis is the Javadoc tag for documenting the purpose of parameters width width of the ball in screens
045 * @paramthis is the Javadoc tag for documenting the purpose of parameters height height of the ball in screens
046 */
047 publicpublic is used to indicate unrestricted access (any other class can have access) PongBall(doubledouble is the type for numbers that can contain decimal fractions width, doubledouble is the type for numbers that can contain decimal fractions height) {open braces start code blocks and must be matched with a close brace
048 thisthis means the current object (the implicit parameter)(width, height, 0.0, 0.0);
049 }close braces end code blocks and must match an earlier open brace
050
051 /**
052 * Update thisthis means the current object (the implicit parameter) ball's position on the screen. This moves the
053 * responsibility forfor is a looping structure for repeatedly executing a block of code updating the ball's position out of the {open braces start code blocks and must be matched with a close brace@link
054 * fang2.core.Game Game}close braces end code blocks and must match an earlier open brace classclass is a group of fields and methods used for making objects's {open braces start code blocks and must be matched with a close brace@code advance}close braces end code blocks and must match an earlier open brace method. This is a
055 * good division of responsibilities: the ball knows its speed and
056 * whatever elseelse is what happens when the if condition is false it needs to know.
057 *
058 * @paramthis is the Javadoc tag for documenting the purpose of parameters deltaT time elapsed since the last call to advance in
059 * seconds
060 */
061 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value advance(doubledouble is the type for numbers that can contain decimal fractions deltaT, Sprite paddle) {open braces start code blocks and must be matched with a close brace
062 translate(deltaX * deltaT, deltaY * deltaT);
063 bounceOffEdges();
064 ifif executes the next statement only if the condition in parenthesis evaluates to true (intersects(paddle)) {open braces start code blocks and must be matched with a close brace
065 bounceOffSprite(paddle);
066 incrementScore();
067 }close braces end code blocks and must match an earlier open brace
068 }close braces end code blocks and must match an earlier open brace
069
070 /**
071 * Has the ball hit a scoring/out of bounds line?
072 *
073 * @returnnull truetrue is the boolean value that is the opposite of false ifif executes the next statement only if the condition in parenthesis evaluates to true out of bounds; falsefalse is a value for the boolean type and means not true otherwise
074 */
075 publicpublic is used to indicate unrestricted access (any other class can have access) booleanboolean is a type that is either true or false isOutOfBounds() {open braces start code blocks and must be matched with a close brace
076 returnreturn means to provide the result of the method and/or cease execution of the method immediately hitRightEdge();
077 }close braces end code blocks and must match an earlier open brace
078
079 /**
080 * Set the velocity of thisthis means the current object (the implicit parameter) ball to the given x and y values.
081 *
082 * @paramthis is the Javadoc tag for documenting the purpose of parameters deltaX horizontal velocity of the ball (screens/sec)
083 * @paramthis is the Javadoc tag for documenting the purpose of parameters deltaY vertical velocity of the ball (screens/sec)
084 */
085 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value setVelocity(doubledouble is the type for numbers that can contain decimal fractions deltaX, doubledouble is the type for numbers that can contain decimal fractions deltaY) {open braces start code blocks and must be matched with a close brace
086 thisthis means the current object (the implicit parameter).deltaX =this assignment operator makes the left side equal to the right side deltaX;
087 thisthis means the current object (the implicit parameter).deltaY =this assignment operator makes the left side equal to the right side deltaY;
088 }close braces end code blocks and must match an earlier open brace
089
090 /**
091 * Is thisthis means the current object (the implicit parameter) sprite above the other?
092 *
093 * @paramthis is the Javadoc tag for documenting the purpose of parameters other sprite to test against
094 *
095 * @returnnull truetrue is the boolean value that is the opposite of false ifif executes the next statement only if the condition in parenthesis evaluates to true thisthis means the current object (the implicit parameter)'s center is above other; falsefalse is a value for the boolean type and means not true otherwise
096 */
097 privateprivate is used to restrict access to the current class only booleanboolean is a type that is either true or false aboveIt(Sprite other) {open braces start code blocks and must be matched with a close brace
098 returnreturn means to provide the result of the method and/or cease execution of the method immediately (getY() < other.getMinY());
099 }close braces end code blocks and must match an earlier open brace
100
101 /**
102 * Is thisthis means the current object (the implicit parameter) sprite below other?
103 *
104 * @paramthis is the Javadoc tag for documenting the purpose of parameters other sprite to test against
105 *
106 * @returnnull truetrue is the boolean value that is the opposite of false ifif executes the next statement only if the condition in parenthesis evaluates to true thisthis means the current object (the implicit parameter)'s center is below other; falsefalse is a value for the boolean type and means not true otherwise
107 */
108 privateprivate is used to restrict access to the current class only booleanboolean is a type that is either true or false belowIt(Sprite other) {open braces start code blocks and must be matched with a close brace
109 returnreturn means to provide the result of the method and/or cease execution of the method immediately (other.getMaxY() < getY());
110 }close braces end code blocks and must match an earlier open brace
111
112 /**
113 * Bounce the ball off all walls off which it bounces.
114 */
115 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value bounceOffEdges() {open braces start code blocks and must be matched with a close brace
116 ifif executes the next statement only if the condition in parenthesis evaluates to true (hitTopEdge()) {open braces start code blocks and must be matched with a close brace
117 bounceOffHorizontalEdge();
118 clearTop(0.0);
119 }close braces end code blocks and must match an earlier open brace elseelse is what happens when the if condition is false ifif executes the next statement only if the condition in parenthesis evaluates to true (hitBottomEdge()) {open braces start code blocks and must be matched with a close brace
120 bounceOffHorizontalEdge();
121 clearBottom(1.0);
122 }close braces end code blocks and must match an earlier open brace
123 ifif executes the next statement only if the condition in parenthesis evaluates to true (hitLeftEdge()) {open braces start code blocks and must be matched with a close brace
124 bounceOffVerticalEdge();
125 clearLeft(0.0);
126 }close braces end code blocks and must match an earlier open brace
127 /* elseelse is what happens when the if condition is false ifif executes the next statement only if the condition in parenthesis evaluates to true (hitRightEdge()) {open braces start code blocks and must be matched with a close brace
128 * clearRight(1.0);}close braces end code blocks and must match an earlier open brace */
129 }close braces end code blocks and must match an earlier open brace
130
131 /**
132 * Respond to hitting a horizontal edge; reverse the up/down direction
133 * (by changing the sign of deltaY).
134 */
135 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value bounceOffHorizontalEdge() {open braces start code blocks and must be matched with a close brace
136 deltaY =this assignment operator makes the left side equal to the right side -deltaY;
137 }close braces end code blocks and must match an earlier open brace
138
139 /**
140 * A ball collides with any other sprite, treat the other as a
141 * rectangle, bounce off the horizontal/vertical surfaces (or both).
142 * Only reverse direction ifif executes the next statement only if the condition in parenthesis evaluates to true you were moving "into" the paddle in the
143 * given direction.
144 *
145 * <p>If the other is on our right AND it intersects our right edge,
146 * then we were moving toward it when we hit. Need to reverse. If,
147 * instead, it is on our right and somehow our left edge intersects
148 * (or seems to), we actually hit the top or bottom edge and there is
149 * no reversal in the x-dimension.
150 *
151 * @paramthis is the Javadoc tag for documenting the purpose of parameters other the other sprite which we hit
152 */
153 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value bounceOffSprite(Sprite other) {open braces start code blocks and must be matched with a close brace
154 ifif executes the next statement only if the condition in parenthesis evaluates to true (leftOfIt(other) &this performs a bit-wise and (not the same as boolean and which is &&)&this performs a bit-wise and (not the same as boolean and which is &&) intersectsRight(other)) {open braces start code blocks and must be matched with a close brace
155 bounceOffVerticalEdge();
156 clearRight(other.getMinX());
157 }close braces end code blocks and must match an earlier open brace
158 ifif executes the next statement only if the condition in parenthesis evaluates to true (rightOfIt(other) &this performs a bit-wise and (not the same as boolean and which is &&)&this performs a bit-wise and (not the same as boolean and which is &&) intersectsLeft(other)) {open braces start code blocks and must be matched with a close brace
159 bounceOffVerticalEdge();
160 clearLeft(other.getMaxX());
161 }close braces end code blocks and must match an earlier open brace
162
163 ifif executes the next statement only if the condition in parenthesis evaluates to true (aboveIt(other) &this performs a bit-wise and (not the same as boolean and which is &&)&this performs a bit-wise and (not the same as boolean and which is &&) intersectsBottom(other)) {open braces start code blocks and must be matched with a close brace
164 bounceOffHorizontalEdge();
165 clearBottom(other.getMinY());
166 }close braces end code blocks and must match an earlier open brace
167 ifif executes the next statement only if the condition in parenthesis evaluates to true (belowIt(other) &this performs a bit-wise and (not the same as boolean and which is &&)&this performs a bit-wise and (not the same as boolean and which is &&) intersectsTop(other)) {open braces start code blocks and must be matched with a close brace
168 bounceOffHorizontalEdge();
169 clearTop(other.getMaxY());
170 }close braces end code blocks and must match an earlier open brace
171 }close braces end code blocks and must match an earlier open brace
172
173 /**
174 * Respond to hitting a vertical edge; reverse the left/right
175 * direction (by changing the sign of deltaX).
176 */
177 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value bounceOffVerticalEdge() {open braces start code blocks and must be matched with a close brace
178 deltaX =this assignment operator makes the left side equal to the right side -deltaX;
179 }close braces end code blocks and must match an earlier open brace
180
181 /**
182 * Make sure our bottom edge is clear of y
183 *
184 * @paramthis is the Javadoc tag for documenting the purpose of parameters y the line we must be above
185 */
186 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value clearBottom(doubledouble is the type for numbers that can contain decimal fractions y) {open braces start code blocks and must be matched with a close brace
187 ifif executes the next statement only if the condition in parenthesis evaluates to true (y < getMaxY()) {open braces start code blocks and must be matched with a close brace
188 translateY(y - getMaxY());
189 }close braces end code blocks and must match an earlier open brace
190 }close braces end code blocks and must match an earlier open brace
191
192 /**
193 * Make sure our left edge is clear of x
194 *
195 * @paramthis is the Javadoc tag for documenting the purpose of parameters x the line we must be to the right of
196 */
197 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value clearLeft(doubledouble is the type for numbers that can contain decimal fractions x) {open braces start code blocks and must be matched with a close brace
198 ifif executes the next statement only if the condition in parenthesis evaluates to true (getMinX() < x) {open braces start code blocks and must be matched with a close brace
199 translateX(x - getMinX());
200 }close braces end code blocks and must match an earlier open brace
201 }close braces end code blocks and must match an earlier open brace
202
203 /**
204 * Make sure our right edge is clear of x
205 *
206 * @paramthis is the Javadoc tag for documenting the purpose of parameters x the line we must be to the left of
207 */
208 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value clearRight(doubledouble is the type for numbers that can contain decimal fractions x) {open braces start code blocks and must be matched with a close brace
209 ifif executes the next statement only if the condition in parenthesis evaluates to true (x < getMaxX()) {open braces start code blocks and must be matched with a close brace
210 translateY(x - getMaxX());
211 }close braces end code blocks and must match an earlier open brace
212 }close braces end code blocks and must match an earlier open brace
213
214 /**
215 * Make sure our top edge is clear of the given y
216 *
217 * @paramthis is the Javadoc tag for documenting the purpose of parameters y the line we must be below
218 */
219 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value clearTop(doubledouble is the type for numbers that can contain decimal fractions y) {open braces start code blocks and must be matched with a close brace
220 ifif executes the next statement only if the condition in parenthesis evaluates to true (getMinY() < y) {open braces start code blocks and must be matched with a close brace
221 translateY(y - getMinY());
222 }close braces end code blocks and must match an earlier open brace
223 }close braces end code blocks and must match an earlier open brace
224
225 /**
226 * Did the ball hit the bottom edge?
227 *
228 * @returnnull truetrue is the boolean value that is the opposite of false ifif executes the next statement only if the condition in parenthesis evaluates to true the ball hit the bottom edge
229 */
230 privateprivate is used to restrict access to the current class only booleanboolean is a type that is either true or false hitBottomEdge() {open braces start code blocks and must be matched with a close brace
231 returnreturn means to provide the result of the method and/or cease execution of the method immediately (getMaxY() >=this evaluates to true if the left side is not less than the right side 1.0);
232 }close braces end code blocks and must match an earlier open brace
233
234 /**
235 * Did the ball hit the left edge?
236 *
237 * @returnnull truetrue is the boolean value that is the opposite of false ifif executes the next statement only if the condition in parenthesis evaluates to true it hit the left edge
238 */
239 privateprivate is used to restrict access to the current class only booleanboolean is a type that is either true or false hitLeftEdge() {open braces start code blocks and must be matched with a close brace
240 returnreturn means to provide the result of the method and/or cease execution of the method immediately (getMinX() <=this evaluates to true if the left side is not more than the right side 0.0);
241 }close braces end code blocks and must match an earlier open brace
242
243 /**
244 * Did the ball hit the right edge?
245 *
246 * @returnnull truetrue is the boolean value that is the opposite of false ifif executes the next statement only if the condition in parenthesis evaluates to true it has hit the right edge
247 */
248 privateprivate is used to restrict access to the current class only booleanboolean is a type that is either true or false hitRightEdge() {open braces start code blocks and must be matched with a close brace
249 returnreturn means to provide the result of the method and/or cease execution of the method immediately (getMaxX() >=this evaluates to true if the left side is not less than the right side 1.0);
250 }close braces end code blocks and must match an earlier open brace
251
252 /**
253 * Did the ball hit the top edge?
254 *
255 * @returnnull truetrue is the boolean value that is the opposite of false ifif executes the next statement only if the condition in parenthesis evaluates to true the ball hit the top edge
256 */
257 privateprivate is used to restrict access to the current class only booleanboolean is a type that is either true or false hitTopEdge() {open braces start code blocks and must be matched with a close brace
258 returnreturn means to provide the result of the method and/or cease execution of the method immediately (getMinY() <=this evaluates to true if the left side is not more than the right side 0.0);
259 }close braces end code blocks and must match an earlier open brace
260
261 /**
262 * Add one to the game's score.
263 */
264 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value incrementScore() {open braces start code blocks and must be matched with a close brace
265 Game.getCurrentGame().
266 setScore(Game.getCurrentGame().getScore() +adds two numbers together or concatenates Strings together 1);
267 }close braces end code blocks and must match an earlier open brace
268
269 /**
270 * Does other intersect our bottom edge?
271 *
272 * @paramthis is the Javadoc tag for documenting the purpose of parameters other sprite to test against
273 *
274 * @returnnull truetrue is the boolean value that is the opposite of false ifif executes the next statement only if the condition in parenthesis evaluates to true given edge intersects; falsefalse is a value for the boolean type and means not true otherwise
275 */
276 privateprivate is used to restrict access to the current class only booleanboolean is a type that is either true or false intersectsBottom(Sprite other) {open braces start code blocks and must be matched with a close brace
277 returnreturn means to provide the result of the method and/or cease execution of the method immediately (other.getMinY() < getMaxY());
278 }close braces end code blocks and must match an earlier open brace
279
280 /**
281 * Does other intersect our left edge?
282 *
283 * @paramthis is the Javadoc tag for documenting the purpose of parameters other sprite to test against
284 *
285 * @returnnull truetrue is the boolean value that is the opposite of false ifif executes the next statement only if the condition in parenthesis evaluates to true given edge intersects; falsefalse is a value for the boolean type and means not true otherwise
286 */
287 privateprivate is used to restrict access to the current class only booleanboolean is a type that is either true or false intersectsLeft(Sprite other) {open braces start code blocks and must be matched with a close brace
288 returnreturn means to provide the result of the method and/or cease execution of the method immediately (getMinX() < other.getMaxX());
289 }close braces end code blocks and must match an earlier open brace
290
291 /**
292 * Does other intersect our right edge?
293 *
294 * @paramthis is the Javadoc tag for documenting the purpose of parameters other sprite to test against
295 *
296 * @returnnull truetrue is the boolean value that is the opposite of false ifif executes the next statement only if the condition in parenthesis evaluates to true given edge intersects; falsefalse is a value for the boolean type and means not true otherwise
297 */
298 privateprivate is used to restrict access to the current class only booleanboolean is a type that is either true or false intersectsRight(Sprite other) {open braces start code blocks and must be matched with a close brace
299 returnreturn means to provide the result of the method and/or cease execution of the method immediately (other.getMinX() < getMaxX());
300 }close braces end code blocks and must match an earlier open brace
301
302 /**
303 * Does other intersect our top edge?
304 *
305 * @paramthis is the Javadoc tag for documenting the purpose of parameters other sprite to test against
306 *
307 * @returnnull truetrue is the boolean value that is the opposite of false ifif executes the next statement only if the condition in parenthesis evaluates to true given edge intersects; falsefalse is a value for the boolean type and means not true otherwise
308 */
309 privateprivate is used to restrict access to the current class only booleanboolean is a type that is either true or false intersectsTop(Sprite other) {open braces start code blocks and must be matched with a close brace
310 returnreturn means to provide the result of the method and/or cease execution of the method immediately (getMinY() < other.getMaxY());
311 }close braces end code blocks and must match an earlier open brace
312
313 /**
314 * Is thisthis means the current object (the implicit parameter) sprite to the left of other?
315 *
316 * @paramthis is the Javadoc tag for documenting the purpose of parameters other the sprite to test against
317 *
318 * @returnnull truetrue is the boolean value that is the opposite of false ifif executes the next statement only if the condition in parenthesis evaluates to true thisthis means the current object (the implicit parameter)'s center is to the left of other; falsefalse is a value for the boolean type and means not true
319 * otherwise
320 */
321 privateprivate is used to restrict access to the current class only booleanboolean is a type that is either true or false leftOfIt(Sprite other) {open braces start code blocks and must be matched with a close brace
322 returnreturn means to provide the result of the method and/or cease execution of the method immediately (getX() < other.getMinX());
323 }close braces end code blocks and must match an earlier open brace
324
325 /**
326 * Is thisthis means the current object (the implicit parameter) sprite to the right of other?
327 *
328 * @paramthis is the Javadoc tag for documenting the purpose of parameters other the sprite to test against
329 *
330 * @returnnull truetrue is the boolean value that is the opposite of false ifif executes the next statement only if the condition in parenthesis evaluates to true thisthis means the current object (the implicit parameter)'s center is to the right of other; falsefalse is a value for the boolean type and means not true
331 * otherwise
332 */
333 privateprivate is used to restrict access to the current class only booleanboolean is a type that is either true or false rightOfIt(Sprite other) {open braces start code blocks and must be matched with a close brace
334 returnreturn means to provide the result of the method and/or cease execution of the method immediately (other.getMaxX() < getX());
335 }close braces end code blocks and must match an earlier open brace
336 }close braces end code blocks and must match an earlier open brace
337
338 //Uploaded on Mon Mar 29 21:39:34 EDT 2010
|