|
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.sprites.LineSprite;
006 importimport means to make the classes and/or packages available in this program fang2.sprites.StringSprite;
007
008 /**
009 * The mainThe main method is the place where applications begin executing. game forfor is a looping structure for repeatedly executing a block of code solo pong. This method keeps track of scoring and
010 * is in charge of "ticking" the clock forfor is a looping structure for repeatedly executing a block of code all of the objects in the
011 * game. The objects themselves take care of updating their position as
012 * necessary.
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 GeneralPong
015 extendsextends means to customize or extend the functionality of a class Game {open braces start code blocks and must be matched with a close brace
016 /** Seconds forfor is a looping structure for repeatedly executing a block of code the countdown timer to count */
017 publicpublic is used to indicate unrestricted access (any other class can have access) staticstatic means that an instance is not required for access (class level access) finalfinal means not changeable (often used for constants) doubledouble is the type for numbers that can contain decimal fractions COUNTDOWN_SECONDS =this assignment operator makes the left side equal to the right side 3.0;
018
019 /** The ball */
020 privateprivate is used to restrict access to the current class only GeneralPongBall ball;
021
022 /** The paddle */
023 privateprivate is used to restrict access to the current class only PongPaddle paddle;
024
025 /** Countdown timer */
026 privateprivate is used to restrict access to the current class only CountdownTimer countdown;
027
028 /** Press space to start message */
029 privateprivate is used to restrict access to the current class only StringSprite pressSpace;
030
031 /** The display of the score */
032 privateprivate is used to restrict access to the current class only StringSprite volleysSprite;
033
034 privateprivate is used to restrict access to the current class only booleanboolean is a type that is either true or false showVector =this assignment operator makes the left side equal to the right side falsefalse is a value for the boolean type and means not true;
035 privateprivate is used to restrict access to the current class only LineSprite vector =this assignment operator makes the left side equal to the right side nullnull is the value used to refer to a non-existant object;
036
037 /**
038 * Create all of the sprites and the score. Add all sprites to the
039 * game. Make sure we are waiting forfor is a looping structure for repeatedly executing a block of code player to press space.
040 */
041 @Override
042 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value setup() {open braces start code blocks and must be matched with a close brace
043 ball =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor GeneralPongBall(0.1, 0.1, 0, 0);
044 ball.setColor(getColor("white"));
045 addSprite(ball);
046
047 countdown =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor CountdownTimer();
048 countdown.setLocation(0.5, 0.5);
049 countdown.setScale(0.25);
050 countdown.setColor(getColor("white"));
051 addSprite(countdown);
052
053 volleysSprite =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor StringSprite();
054 volleysSprite.setScale(0.05);
055 volleysSprite.setLocation(0.10, 0.10);
056 volleysSprite.setText(Integer.toString(0));
057 addSprite(volleysSprite);
058
059 pressSpace =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor StringSprite();
060 pressSpace.setScale(0.05);
061 pressSpace.setText("Press <Space> to begin");
062 pressSpace.setColor(getColor("yellow"));
063 pressSpace.setLocation(0.5, 0.5);
064 addSprite(pressSpace);
065
066 beginWaitingToCountdown();
067 }close braces end code blocks and must match an earlier open brace
068
069 /**
070 * Position a ball somewhere in the middle of the left half of the
071 * screen.
072 *
073 * @paramthis is the Javadoc tag for documenting the purpose of parameters theBall the ball to position
074 */
075 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value startBall(GeneralPongBall theBall) {open braces start code blocks and must be matched with a close brace
076 theBall.setVelocity(0.3, 0.3);
077 theBall.setLocation(randomDouble(0.2, 0.4), randomDouble(0.3, 0.7));
078 }close braces end code blocks and must match an earlier open brace
079
080 /**
081 * This game has three states. To make it easy to tell what state we
082 * are in and to easily switchswitch is used with case for multiple alternatives (like if/else if...) states, these methods hide the details
083 * from the {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.
084 */
085
086 /**
087 * Are we in the moving ball state?
088 *
089 * @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 we are, falsefalse is a value for the boolean type and means not true otherwise
090 */
091 privateprivate is used to restrict access to the current class only booleanboolean is a type that is either true or false isPlaying() {open braces start code blocks and must be matched with a close brace
092 returnreturn means to provide the result of the method and/or cease execution of the method immediately ball.isVisible();
093 }close braces end code blocks and must match an earlier open brace
094
095 /**
096 * Whatever state we were in, move us to the moving ball state.
097 */
098 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value beginPlaying() {open braces start code blocks and must be matched with a close brace
099 ball.show();
100 countdown.hide();
101 pressSpace.hide();
102 startBall(ball);
103 setScore(0);// no volleys yet
104 }close braces end code blocks and must match an earlier open brace
105
106 /**
107 * Are we in the waiting to countdown state?
108 *
109 * @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 we are, falsefalse is a value for the boolean type and means not true otherwise
110 */
111 privateprivate is used to restrict access to the current class only booleanboolean is a type that is either true or false isWaitingToCountdown() {open braces start code blocks and must be matched with a close brace
112 returnreturn means to provide the result of the method and/or cease execution of the method immediately pressSpace.isVisible();
113 }close braces end code blocks and must match an earlier open brace
114
115 /**
116 * Whatever state we were in, move us to the waiting to countdown
117 * state.
118 */
119 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value beginWaitingToCountdown() {open braces start code blocks and must be matched with a close brace
120 ball.hide();
121 countdown.hide();
122 pressSpace.show();
123 }close braces end code blocks and must match an earlier open brace
124
125 /**
126 * Are we in the counting down state?
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 we are, falsefalse is a value for the boolean type and means not true otherwise
129 */
130 privateprivate is used to restrict access to the current class only booleanboolean is a type that is either true or false isCountingDown() {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 countdown.isVisible();
132 }close braces end code blocks and must match an earlier open brace
133
134 /**
135 * Whatever state we were in, move us to the counting down state
136 */
137 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value beginCountingDown() {open braces start code blocks and must be matched with a close brace
138 ball.hide();
139 countdown.show();
140 pressSpace.hide();
141 countdown.setTimer(COUNTDOWN_SECONDS);
142 countdown.startTimer();
143 }close braces end code blocks and must match an earlier open brace
144
145 /**
146 * Update the score; thisthis means the current object (the implicit parameter) overrides the Game version so we can update
147 * our display of the score on the screen.
148 *
149 * @paramthis is the Javadoc tag for documenting the purpose of parameters score the newnew is used to create objects by calling the constructor score value
150 */
151 @Override
152 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value setScore(intint is the type for whole numbers and it is short for integer score) {open braces start code blocks and must be matched with a close brace
153 super.setScore(score);
154 volleysSprite.setText(Integer.toString(score));
155 }close braces end code blocks and must match an earlier open brace
156
157 /**
158 * Advance the game one tick. This game has three states: Ball moving;
159 * Counting down to ball moving; Waiting to start countdown. The three
160 * states can be separated by seeing whether: ball isVisible();
161 * countdown isVisible(); pressSpace isVisible().
162 */
163 @Override
164 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) {open braces start code blocks and must be matched with a close brace
165 ifif executes the next statement only if the condition in parenthesis evaluates to true (isWaitingToCountdown()) {open braces start code blocks and must be matched with a close brace
166 ifif executes the next statement only if the condition in parenthesis evaluates to true (getKeyPressed() ==this is the comparison operator which evaluates to true if both sides are the same ' ') {open braces start code blocks and must be matched with a close brace
167 beginCountingDown();
168 }close braces end code blocks and must match an earlier open brace
169 }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 (isCountingDown()) {open braces start code blocks and must be matched with a close brace
170 countdown.advance(deltaT);
171 ifif executes the next statement only if the condition in parenthesis evaluates to true (!this is the not operator, which changes true to false and false to trueisCountingDown()) {open braces start code blocks and must be matched with a close brace
172 beginPlaying();
173 }close braces end code blocks and must match an earlier open brace
174 }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 (isPlaying()) {open braces start code blocks and must be matched with a close brace
175 ifif executes the next statement only if the condition in parenthesis evaluates to true (thisthis means the current object (the implicit parameter).mousePressed()) {open braces start code blocks and must be matched with a close brace
176 showVector =this assignment operator makes the left side equal to the right side !this is the not operator, which changes true to false and false to trueshowVector;
177 ifif executes the next statement only if the condition in parenthesis evaluates to true (showVector) {open braces start code blocks and must be matched with a close brace
178 Location2D otherEnd =this assignment operator makes the left side equal to the right side ball.getLocation().add(
179 ball.getVelocity());
180 vector =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor LineSprite(ball.getLocation(), otherEnd);
181 vector.setColor(getColor("green"));
182 addSprite(vector);
183 }close braces end code blocks and must match an earlier open brace elseelse is what happens when the if condition is false {open braces start code blocks and must be matched with a close brace
184 removeSprite(vector);
185 vector =this assignment operator makes the left side equal to the right side nullnull is the value used to refer to a non-existant object;
186 }close braces end code blocks and must match an earlier open brace
187 }close braces end code blocks and must match an earlier open brace
188
189 ifif executes the next statement only if the condition in parenthesis evaluates to true (!this is the not operator, which changes true to false and false to trueshowVector) {open braces start code blocks and must be matched with a close brace
190 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) {open braces start code blocks and must be matched with a close brace
191 paddle.advance(deltaT);
192 }close braces end code blocks and must match an earlier open brace
193 ball.advance(deltaT, paddle);
194
195 ifif executes the next statement only if the condition in parenthesis evaluates to true (ball.isOutOfBounds()) {open braces start code blocks and must be matched with a close brace
196 beginWaitingToCountdown();
197 }close braces end code blocks and must match an earlier open brace
198 }close braces end code blocks and must match an earlier open brace
199 }close braces end code blocks and must match an earlier open brace
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// SoloPong
202
203 //Uploaded on Mon Mar 29 21:41:49 EDT 2010
|