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