|
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.attributes.Location2D;
004 importimport means to make the classes and/or packages available in this program fang2.core.Game;
005 importimport means to make the classes and/or packages available in this program fang2.core.Sprite;
006 importimport means to make the classes and/or packages available in this program fang2.sprites.OvalSprite;
007
008 /**
009 * The PongBall is an augmented {open braces start code blocks and must be matched with a close brace@link fang2.sprites.OvalSprite
010 * OvalSprite}close braces end code blocks and must match an earlier open brace which has a velocity, an advance method which applies
011 * that velocity, and the ability to bounce off of the walls of the game
012 * and other sprites.
013 */
014 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 GeneralPongBall
015 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
016 /**
017 * current velocity in screens/second; decomposed into x-coordinate
018 * and y-coordinate velocities
019 */
020 privateprivate is used to restrict access to the current class only doubledouble is the type for numbers that can contain decimal fractions deltaX;
021 privateprivate is used to restrict access to the current class only doubledouble is the type for numbers that can contain decimal fractions deltaY;
022
023 publicpublic is used to indicate unrestricted access (any other class can have access) finalfinal means not changeable (often used for constants) intint is the type for whole numbers and it is short for integer LEFT_EDGE =this assignment operator makes the left side equal to the right side 0x0001;
024 publicpublic is used to indicate unrestricted access (any other class can have access) finalfinal means not changeable (often used for constants) intint is the type for whole numbers and it is short for integer RIGHT_EDGE =this assignment operator makes the left side equal to the right side 0x0002;
025 publicpublic is used to indicate unrestricted access (any other class can have access) finalfinal means not changeable (often used for constants) intint is the type for whole numbers and it is short for integer TOP_EDGE =this assignment operator makes the left side equal to the right side 0x0004;
026 publicpublic is used to indicate unrestricted access (any other class can have access) finalfinal means not changeable (often used for constants) intint is the type for whole numbers and it is short for integer BOTTOM_EDGE =this assignment operator makes the left side equal to the right side 0x0008;
027
028 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer bounceEdges;
029
030 /**
031 * Create a newnew is used to create objects by calling the constructor PongBall. Specify the size and the initial velocity of
032 * the ball.
033 *
034 * @paramthis is the Javadoc tag for documenting the purpose of parameters width width of the ball in screens
035 * @paramthis is the Javadoc tag for documenting the purpose of parameters height height of the ball in screens
036 * @paramthis is the Javadoc tag for documenting the purpose of parameters deltaX initial horizontal velocity in screens per second
037 * @paramthis is the Javadoc tag for documenting the purpose of parameters deltaY initial vertical velocity in screens per second
038 */
039 publicpublic is used to indicate unrestricted access (any other class can have access) GeneralPongBall(doubledouble is the type for numbers that can contain decimal fractions width, doubledouble is the type for numbers that can contain decimal fractions height, doubledouble is the type for numbers that can contain decimal fractions deltaX,
040 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
041 super(width, height);
042 /*
043 * A scope hole: to which deltaX (line 9 or line 25) does the
044 * variable deltaX refer? The closest (furthest in in curly braces),
045 * so line 25 How to refer to the field? Use thisthis means the current object (the implicit parameter) and a dot.
046 */
047 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
048 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
049 thisthis means the current object (the implicit parameter).bounceEdges =this assignment operator makes the left side equal to the right side LEFT_EDGE |this performs a bit-wise or (not the same as boolean or which is ||) RIGHT_EDGE |this performs a bit-wise or (not the same as boolean or which is ||) TOP_EDGE |this performs a bit-wise or (not the same as boolean or which is ||) BOTTOM_EDGE;
050 }close braces end code blocks and must match an earlier open brace
051
052 /**
053 * Create a newnew is used to create objects by calling the constructor PongBall. Specify the size and the initial velocity
054 * will defaultdefault is what is executed when no cases are matched to 0.0, 0.0
055 *
056 * @paramthis is the Javadoc tag for documenting the purpose of parameters width width of the ball in screens
057 * @paramthis is the Javadoc tag for documenting the purpose of parameters height height of the ball in screens
058 */
059 publicpublic is used to indicate unrestricted access (any other class can have access) GeneralPongBall(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
060 /*
061 * The first line of a constructor can pass the job to a different
062 * constructor of the current object by calling a method "this".
063 */
064 thisthis means the current object (the implicit parameter)(width, height, 0.0, 0.0);
065 }close braces end code blocks and must match an earlier open brace
066
067 publicpublic is used to indicate unrestricted access (any other class can have access) Location2D getVelocity() {open braces start code blocks and must be matched with a close brace
068 returnreturn means to provide the result of the method and/or cease execution of the method immediately newnew is used to create objects by calling the constructor Location2D(deltaX, deltaY);
069 }close braces end code blocks and must match an earlier open brace
070
071 /**
072 * Set the velocity of thisthis means the current object (the implicit parameter) ball to the given x and y values.
073 *
074 * @paramthis is the Javadoc tag for documenting the purpose of parameters deltaX horizontal velocity of the ball (screens/sec)
075 * @paramthis is the Javadoc tag for documenting the purpose of parameters deltaY vertical velocity of the ball (screens/sec)
076 */
077 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
078 thisthis means the current object (the implicit parameter).deltaX =this assignment operator makes the left side equal to the right side deltaX;
079 thisthis means the current object (the implicit parameter).deltaY =this assignment operator makes the left side equal to the right side deltaY;
080 }close braces end code blocks and must match an earlier open brace
081
082 /**
083 * Update thisthis means the current object (the implicit parameter) ball's position on the screen. This moves the
084 * 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
085 * 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 good
086 * division of responsibilities: the ball knows its speed and whatever
087 * elseelse is what happens when the if condition is false it needs to know.
088 *
089 * @paramthis is the Javadoc tag for documenting the purpose of parameters deltaT time elapsed since the last call to advance in
090 * seconds
091 */
092 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
093 translate(deltaX * deltaT, deltaY * deltaT);
094 bounceOffEdges();
095 ifif executes the next statement only if the condition in parenthesis evaluates to true ((paddle !=this is the not equals operator which evaluates to true if both sides are different nullnull is the value used to refer to a non-existant object) &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 &&) intersects(paddle)) {open braces start code blocks and must be matched with a close brace
096 bounceOffSprite(paddle);
097 Game.getCurrentGame().setScore(Game.getCurrentGame().getScore() +adds two numbers together or concatenates Strings together
098 1);
099 }close braces end code blocks and must match an earlier open brace
100 }close braces end code blocks and must match an earlier open brace
101
102 /**
103 * @returnnull the bounceEdges
104 */
105 publicpublic is used to indicate unrestricted access (any other class can have access) intint is the type for whole numbers and it is short for integer getBounceEdges() {open braces start code blocks and must be matched with a close brace
106 returnreturn means to provide the result of the method and/or cease execution of the method immediately bounceEdges;
107 }close braces end code blocks and must match an earlier open brace
108
109 /**
110 * @paramthis is the Javadoc tag for documenting the purpose of parameters bounceEdges the bounceEdges to set
111 */
112 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value setBounceEdges(intint is the type for whole numbers and it is short for integer bounceEdges) {open braces start code blocks and must be matched with a close brace
113 thisthis means the current object (the implicit parameter).bounceEdges =this assignment operator makes the left side equal to the right side bounceEdges;
114 }close braces end code blocks and must match an earlier open brace
115
116 /**
117 * Did the ball hit the right edge?
118 *
119 * @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
120 */
121 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
122 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);
123 }close braces end code blocks and must match an earlier open brace
124
125 /**
126 * Did the ball hit the left edge?
127 *
128 * @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
129 */
130 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
131 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);
132 }close braces end code blocks and must match an earlier open brace
133
134 /**
135 * Did the ball hit the bottom edge?
136 *
137 * @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
138 */
139 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
140 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);
141 }close braces end code blocks and must match an earlier open brace
142
143 /**
144 * Did the ball hit the top edge?
145 *
146 * @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
147 */
148 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
149 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);
150 }close braces end code blocks and must match an earlier open brace
151
152 /**
153 * Respond to hitting a horizontal edge; reverse the up/down direction
154 * (by changing the sign of deltaY).
155 */
156 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value bounceOffHorizontalEdge() {open braces start code blocks and must be matched with a close brace
157 deltaY =this assignment operator makes the left side equal to the right side -deltaY;
158 }close braces end code blocks and must match an earlier open brace
159
160 /**
161 * Respond to hitting a vertical edge; reverse the left/right
162 * direction (by changing the sign of deltaX).
163 */
164 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value bounceOffVerticalEdge() {open braces start code blocks and must be matched with a close brace
165 deltaX =this assignment operator makes the left side equal to the right side -deltaX;
166 }close braces end code blocks and must match an earlier open brace
167
168 /**
169 * Has the ball hit a scoring/out of bounds line?
170 *
171 * @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
172 */
173 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
174 returnreturn means to provide the result of the method and/or cease execution of the method immediately (((bounceEdges &this performs a bit-wise and (not the same as boolean and which is &&) TOP_EDGE) ==this is the comparison operator which evaluates to true if both sides are the same 0) &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 &&) hitTopEdge()) ||this is boolean or, meaning if either or both are true then the result is true
175 (((bounceEdges &this performs a bit-wise and (not the same as boolean and which is &&) BOTTOM_EDGE) ==this is the comparison operator which evaluates to true if both sides are the same 0) &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 &&) hitBottomEdge()) ||this is boolean or, meaning if either or both are true then the result is true
176 (((bounceEdges &this performs a bit-wise and (not the same as boolean and which is &&) LEFT_EDGE) ==this is the comparison operator which evaluates to true if both sides are the same 0) &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 &&) hitLeftEdge()) ||this is boolean or, meaning if either or both are true then the result is true
177 (((bounceEdges &this performs a bit-wise and (not the same as boolean and which is &&) RIGHT_EDGE) ==this is the comparison operator which evaluates to true if both sides are the same 0) &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 &&) hitRightEdge());
178 }close braces end code blocks and must match an earlier open brace
179
180 /**
181 * Bounce the ball off all walls off which it bounces.
182 */
183 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value bounceOffEdges() {open braces start code blocks and must be matched with a close brace
184 ifif executes the next statement only if the condition in parenthesis evaluates to true ((((bounceEdges &this performs a bit-wise and (not the same as boolean and which is &&) TOP_EDGE) !=this is the not equals operator which evaluates to true if both sides are different 0) &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 &&) hitTopEdge()) ||this is boolean or, meaning if either or both are true then the result is true
185 (((bounceEdges &this performs a bit-wise and (not the same as boolean and which is &&) BOTTOM_EDGE) !=this is the not equals operator which evaluates to true if both sides are different 0) &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 &&) hitBottomEdge())) {open braces start code blocks and must be matched with a close brace
186 bounceOffHorizontalEdge();
187 }close braces end code blocks and must match an earlier open brace
188 ifif executes the next statement only if the condition in parenthesis evaluates to true ((((bounceEdges &this performs a bit-wise and (not the same as boolean and which is &&) LEFT_EDGE) !=this is the not equals operator which evaluates to true if both sides are different 0) &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 &&) hitLeftEdge()) ||this is boolean or, meaning if either or both are true then the result is true
189 (((bounceEdges &this performs a bit-wise and (not the same as boolean and which is &&) RIGHT_EDGE) !=this is the not equals operator which evaluates to true if both sides are different 0) &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 &&) hitRightEdge())) {open braces start code blocks and must be matched with a close brace
190 bounceOffVerticalEdge();
191 }close braces end code blocks and must match an earlier open brace
192 }close braces end code blocks and must match an earlier open brace
193
194 /**
195 * A ball collides with any other sprite, treat the other as a
196 * rectangle, bounce off the horizontal/vertical surfaces (or both).
197 *
198 * @paramthis is the Javadoc tag for documenting the purpose of parameters other the other sprite which we hit
199 */
200 publicpublic is used to indicate unrestricted access (any other class can have access) 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
201 // we hit a horizontal edge so reflect the y-velocity
202 ifif executes the next statement only if the condition in parenthesis evaluates to true ((getY() < other.getMinY()) ||this is boolean or, meaning if either or both are true then the result is true (other.getMaxY() < getY())) {open braces start code blocks and must be matched with a close brace
203 deltaY =this assignment operator makes the left side equal to the right side -deltaY;
204 }close braces end code blocks and must match an earlier open brace
205
206 // we hit a vertical edge so reflect the x-velocity
207 ifif executes the next statement only if the condition in parenthesis evaluates to true ((getX() < other.getMinX()) ||this is boolean or, meaning if either or both are true then the result is true (other.getMaxX() < getX())) {open braces start code blocks and must be matched with a close brace
208 deltaX =this assignment operator makes the left side equal to the right side -deltaX;
209 }close braces end code blocks and must match an earlier open brace
210 }close braces end code blocks and must match an earlier open brace
211 }close braces end code blocks and must match an earlier open brace
212
213 //Uploaded on Mon Mar 29 21:39:42 EDT 2010
|