|
001 packagepackage is used to name the directory or folder a class is in FA;
002 //start auto-imports
003 importimport means to make the classes and/or packages available in this program java.util.*;
004 //end auto-imports
005
006 importimport means to make the classes and/or packages available in this program fang.*;
007 importimport means to make the classes and/or packages available in this program java.awt.*;
008 importimport means to make the classes and/or packages available in this program java.awt.geom.*;
009
010 /**
011 * Assignment 7: Breakdot.
012 * A ball is set in motion to hit bricks.The first time a brick is hit,
013 * it changes color from red to green. When a green is hit, the brick disappears
014 * and the score is increased. The buttons give you a chance to play
015 * with the ball moving rapidly or slowly. If the user does not
016 * hit the ball with the paddle, a 'life' is lost. You have three lives.
017 *
018 * @authorthis is the Javadoc tag for documenting who created the source code Derrick Dixon
019 * @authorthis is the Javadoc tag for documenting who created the source code Frank Anderson
020 */
021 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 Consolidated_Assignment_7 extendsextends means to customize or extend the functionality of a class Game
022 {open braces start code blocks and must be matched with a close brace
023 /** startLives is the initial value of the
024 * number of times the player can miss the ball with the paddle. */
025 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer startLives;
026 /** startScore initializes the score and keeps track of the score. */
027 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer startScore;
028 /** allBricks is an ArrayList that stores the targets to be hit by the ball. */
029 privateprivate is used to restrict access to the current class only ArrayList<Sprite> allBricks;
030 /** ball is the object used to strike targets(bricks) and eventually resulting
031 * in score. */
032 privateprivate is used to restrict access to the current class only OvalSprite ball;
033 /** boundary represents the top and sides of the game field against
034 * which the ball bounces. */
035 privateprivate is used to restrict access to the current class only OutlineSprite boundary ;
036 /** brick is an element of ArrayList, allBricks. */
037 privateprivate is used to restrict access to the current class only RectangleSprite brick;
038 /** score is the number of green bricks hit. */
039 /** lives the the number of times remaining forfor is a looping structure for repeatedly executing a block of code the ball to go below the paddle */
040 privateprivate is used to restrict access to the current class only StringSprite score, lives;
041 /** paddle is the horizontl line used to keep the ball in play. If the
042 * ball goes below the paddle, lives decreases by one. */
043 privateprivate is used to restrict access to the current class only LineSprite paddle;
044 /** leftWall is a projection added as an enhncement on the left of the game field. */
045 privateprivate is used to restrict access to the current class only PolygonSprite leftWall;
046 /** rightWall is a projection added as an enhncement on the right of the game field. */
047 privateprivate is used to restrict access to the current class only PolygonSprite rightWall;
048 /** ballTransformer give the ball and initial velocity.
049 brickTransformer gives the brick and initial velocity. */
050 privateprivate is used to restrict access to the current class only ProjectileTransformer ballTransformer, brickTransformer;
051 /** numberOfRows is the number of horizontal rows of bricks displayed at the top of the screen. */
052 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer numberOfRows =this assignment operator makes the left side equal to the right side 3;
053 /** numberOfColumns is the number of vertical columns into which the bricks are arranged. */
054 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer numberOfColumns =this assignment operator makes the left side equal to the right side 8;
055 /** bricksRemaining represents the number of bricks remaining as targets. */
056 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer bricksRemaining;
057 /** gameOver is a message indicating that the game is over. */
058 privateprivate is used to restrict access to the current class only StringSprite gameOver;
059 /** buttonFast and buttonSlow allows the user to choose the speed of the ball. */
060 privateprivate is used to restrict access to the current class only ButtonSprite buttonFast;
061 privateprivate is used to restrict access to the current class only ButtonSprite buttonSlow;
062 /** ballXSpeed and ballYSpeed initializes the speed of the ball
063 * in the x-direction and y-direction, respecively. */
064 privateprivate is used to restrict access to the current class only doubledouble is the type for numbers that can contain decimal fractions ballXSpeed;
065 privateprivate is used to restrict access to the current class only doubledouble is the type for numbers that can contain decimal fractions ballYSpeed;
066
067 /** Place two buttons near the bottom of the screen to allow the user
068 * to select the speed of the ball in play. */
069 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value displayButtons()
070 {open braces start code blocks and must be matched with a close brace
071 buttonFast =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor ButtonSprite("Speed=Fast");
072 buttonFast.setLocation(.4,.95);
073 buttonFast.setSize(.2);
074 addSprite(buttonFast);
075
076 buttonSlow =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor ButtonSprite("Speed=Slow");
077 buttonSlow.setSize(.2);
078 buttonSlow.setLocation(.6,.95);
079 addSprite(buttonSlow);
080 }close braces end code blocks and must match an earlier open brace
081
082 /** Add an obstacle to the left and right walls. */
083 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value addCurvedWalls()
084 {open braces start code blocks and must be matched with a close brace
085 leftWall =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor PolygonSprite(0,0,.05,.15,.1,.3,.2,.4,.1,.5,.05,.6,0,1);
086 leftWall.setColor(getColor("blue"));
087 leftWall.setSize(1.2);
088 leftWall.setLocation(.01,.5);
089 addSprite(leftWall);
090
091 rightWall =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor PolygonSprite(1,0,.95,.15,.9,.3,.8,.4,.9,.5,.95,.6,1,1);
092 rightWall.setColor(getColor("green"));
093 rightWall.setSize(1.2);
094 rightWall.setLocation(0.99,.5);
095 addSprite(rightWall);
096 }close braces end code blocks and must match an earlier open brace
097
098
099 /** Determine the count, stored in bricksRemaining, of the number of visible bricks. */
100 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer checkRemainingBricks()
101 {open braces start code blocks and must be matched with a close brace
102 bricksRemaining =this assignment operator makes the left side equal to the right side 0;
103 forfor is a looping structure for repeatedly executing a block of code(Sprite single : allBricks)
104 {open braces start code blocks and must be matched with a close brace
105 ifif executes the next statement only if the condition in parenthesis evaluates to true(single.isVisible() ==this is the comparison operator which evaluates to true if both sides are the same truetrue is the boolean value that is the opposite of false)
106 bricksRemaining++this is the increment operator, which increases the variable by 1;
107 }close braces end code blocks and must match an earlier open brace
108 returnreturn means to provide the result of the method and/or cease execution of the method immediately bricksRemaining;
109 }close braces end code blocks and must match an earlier open brace
110
111
112
113 /** Displays the statement "GAME OVER". The ball is also removed from view.
114 * When the game is over, the game pauses. */
115 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value displayGameOver()
116 {open braces start code blocks and must be matched with a close brace
117 buttonFast.setVisible(falsefalse is a value for the boolean type and means not true);
118 buttonSlow.setVisible(falsefalse is a value for the boolean type and means not true);
119 gameOver =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor StringSprite("GAME OVER");
120 gameOver.setSize(.9);
121 gameOver.setColor(getColor("orange"));
122 gameOver.setLocation(.5, .3);
123 addSprite(gameOver);
124 ball.setVisible(falsefalse is a value for the boolean type and means not true);
125 paddle.setVisible(falsefalse is a value for the boolean type and means not true);
126 setLives(0);
127 pause();
128 }close braces end code blocks and must match an earlier open brace
129
130
131 /** If the ball position falls below the paddle, it disappears and a life is lost. */
132 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value checkBallYPosition
133 ()
134 {open braces start code blocks and must be matched with a close brace
135 ifif executes the next statement only if the condition in parenthesis evaluates to true(ball.getLocation().y>.93)
136 {open braces start code blocks and must be matched with a close brace
137 ball.setVisible(falsefalse is a value for the boolean type and means not true);
138 makeAndAddBall(.1, -.4);
139 startLives =this assignment operator makes the left side equal to the right side getLives();
140 setLives(startLives-1);
141 lives.setText("Lives:"+adds two numbers together or concatenates Strings togethergetLives());
142 }close braces end code blocks and must match an earlier open brace
143 ifif executes the next statement only if the condition in parenthesis evaluates to true (getLives()<1)
144 displayGameOver();
145 }close braces end code blocks and must match an earlier open brace
146
147 /** Check to see ifif executes the next statement only if the condition in parenthesis evaluates to true the ball collides with a visible brick. If the brick is red,
148 * change the brick's color to green. If the brick hit is green,
149 * make the brick disappear and increase the score by 1. */
150 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value checkBallCollidingWithBrick()
151 {open braces start code blocks and must be matched with a close brace
152 forfor is a looping structure for repeatedly executing a block of code(Sprite single: allBricks)
153 {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(single.isVisible()==this is the comparison operator which evaluates to true if both sides are the same truetrue is the boolean value that is the opposite of false &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 &&) single.intersects(ball))
155 {open braces start code blocks and must be matched with a close brace
156 ball.bounceOffOf(single);
157
158 ifif executes the next statement only if the condition in parenthesis evaluates to true(getColorName(single.getColor())==this is the comparison operator which evaluates to true if both sides are the same"Green")
159 {open braces start code blocks and must be matched with a close brace
160 single.setVisible(falsefalse is a value for the boolean type and means not true);
161 startScore =this assignment operator makes the left side equal to the right side getScore();
162 setScore(startScore+adds two numbers together or concatenates Strings together1);
163 score.setText("Score:"+adds two numbers together or concatenates Strings togethergetScore());
164 }close braces end code blocks and must match an earlier open brace
165 ifif executes the next statement only if the condition in parenthesis evaluates to true(getColorName(single.getColor())==this is the comparison operator which evaluates to true if both sides are the same"Red")
166 single.setColor(getColor("green"));
167 }close braces end code blocks and must match an earlier open brace
168 }close braces end code blocks and must match an earlier open brace
169
170 }close braces end code blocks and must match an earlier open brace
171
172 /** Provide a surface from which the ball can bounce. The bottom boundary
173 * is not visible, but the ball never reaches it because of checks on the
174 * y-position of the ball. */
175 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value makeAndAddBoundary()
176 {open braces start code blocks and must be matched with a close brace
177 boundary=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor OutlineSprite(newnew is used to create objects by calling the constructor RectangleSprite(2,2));
178 boundary.setLineThickness(0.05);
179 boundary.setLocation(0.5, 0.6);
180 boundary.setSize(1.0);
181 boundary.setColor(getColor("Gray"));
182 addSprite(boundary);
183 }close braces end code blocks and must match an earlier open brace
184
185 /** Establish and display the number of lives remaining forfor is a looping structure for repeatedly executing a block of code the player
186 * and the score based on the number of bricks hit. */
187 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value makeAndAddScoreAndLives()
188 {open braces start code blocks and must be matched with a close brace
189 setLives(3);
190 lives=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor StringSprite("Lives:"+adds two numbers together or concatenates Strings togethergetLives());
191 lives.setLocation(0.05, 0.05);
192 lives.setSize(0.25);
193 lives.setColor(getColor("white"));
194 lives.leftJustify();
195 addSprite(lives);
196
197 setScore(0);
198 score=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor StringSprite ("Score:"+adds two numbers together or concatenates Strings togethergetScore());
199 score.setLocation(0.95, 0.05);
200 score.setSize(0.25);
201 score.setColor(getColor("white"));
202 score.rightJustify();
203 addSprite(score);
204 }close braces end code blocks and must match an earlier open brace
205
206 /** A ball will be used to hit the bricks. A ball is created at the point (.5, .85)
207 * and initially moves with speed provided as parameters.
208 * The parameters provide the speed in the x-direction as the y-direction.
209 * @paramthis is the Javadoc tag for documenting the purpose of parameters x is the the initial velocity of the ball in the x-direction.
210 * @paramthis is the Javadoc tag for documenting the purpose of parameters y is the initial velocity of the ball in the y-direction. */
211 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value makeAndAddBall(doubledouble is the type for numbers that can contain decimal fractions x, doubledouble is the type for numbers that can contain decimal fractions y)
212 {open braces start code blocks and must be matched with a close brace
213 ball=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor OvalSprite(1, 1);
214 ball.setLocation(0.5, 0.85);
215 ball.setSize(0.05);
216 ball.setColor(getColor("yellow"));
217 addSprite(ball);
218 ballTransformer=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor ProjectileTransformer(x, y);
219 ball.setTracker(ballTransformer);
220 ball.setBlurLength(3);
221 }close braces end code blocks and must match an earlier open brace
222
223 /** Create a line Sprite to be used as a paddle. */
224 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value makeAndAddPaddle()
225 {open braces start code blocks and must be matched with a close brace
226 paddle=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor LineSprite (0.4, 0.95, 0.6, 0.95);
227 paddle.setLocation(0.5, 0.9);
228 paddle.setColor(getColor("yellow"));
229 paddle.setLineThickness(0.05);
230 addSprite(paddle);
231 }close braces end code blocks and must match an earlier open brace
232
233 /** Move the paddle horizontally with the mouse position
234 * so that the ball can bounce off of the paddle. */
235 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value movePaddle()
236 {open braces start code blocks and must be matched with a close brace
237 paddle.setX(getMouseX());
238 }close braces end code blocks and must match an earlier open brace
239
240 /** Cause the ball to bounce off the boundary, paddle, left wall,
241 * right wall, or brick. **/
242 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value handleCollisions()
243 {open braces start code blocks and must be matched with a close brace
244 ball.bounceOffOf(boundary);
245 ball.bounceOffOf(paddle);
246
247 ball.bounceOffOf(leftWall);
248 ball.bounceOffOf(rightWall);
249
250 checkBallCollidingWithBrick();
251 }close braces end code blocks and must match an earlier open brace
252
253 /** Create red bricks, display them on the screen, and add to an ArrayList, allBricks. */
254 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value createBricks()
255 {open braces start code blocks and must be matched with a close brace
256 allBricks =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor ArrayList<Sprite>();
257 forfor is a looping structure for repeatedly executing a block of code (intint is the type for whole numbers and it is short for integer i =this assignment operator makes the left side equal to the right side 0; i<numberOfRows; i++this is the increment operator, which increases the variable by 1)
258 {open braces start code blocks and must be matched with a close brace
259 forfor is a looping structure for repeatedly executing a block of code( intint is the type for whole numbers and it is short for integer j =this assignment operator makes the left side equal to the right side 0; j < numberOfColumns; j++this is the increment operator, which increases the variable by 1)
260 {open braces start code blocks and must be matched with a close brace
261 brick =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor RectangleSprite(2,1);
262 brick.setColor(getColor("Red"));
263 brick.setSize(.7/numberOfColumns);
264 brick.setLocation(.18+adds two numbers together or concatenates Strings togetherj*.8/numberOfColumns, .15+adds two numbers together or concatenates Strings together i*.2/numberOfRows);
265 brick.setVisible(truetrue is the boolean value that is the opposite of false);
266 addSprite(brick);
267 allBricks.add(brick);
268 }close braces end code blocks and must match an earlier open brace
269 }close braces end code blocks and must match an earlier open brace
270 }close braces end code blocks and must match an earlier open brace
271
272 /**sets up the game*/
273 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value setup()
274 {open braces start code blocks and must be matched with a close brace
275 createBricks();
276 makeAndAddPaddle();
277 makeAndAddBoundary();
278 makeAndAddScoreAndLives();
279 bricksRemaining =this assignment operator makes the left side equal to the right side 0;
280 addCurvedWalls();
281 displayButtons();
282 makeAndAddBall(.1,-.4);
283
284 }close braces end code blocks and must match an earlier open brace
285
286
287 /**handle input and game events*/
288 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value advance()
289 {open braces start code blocks and must be matched with a close brace
290
291 /**Clicking on a button selects the initial ball speed and allows the user
292 * to change the speed during the game. */
293 ifif executes the next statement only if the condition in parenthesis evaluates to true( getClick2D() !=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 &&) getClick2D().intersects(buttonSlow))
294 {open braces start code blocks and must be matched with a close brace
295
296 ball.setVisible(falsefalse is a value for the boolean type and means not true);
297 ballXSpeed=this assignment operator makes the left side equal to the right side.1;
298 ballYSpeed=this assignment operator makes the left side equal to the right side-.2;
299 makeAndAddBall(ballXSpeed, ballYSpeed);
300
301 }close braces end code blocks and must match an earlier open brace
302 ifif executes the next statement only if the condition in parenthesis evaluates to true(getClick2D() !=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 &&) getClick2D().intersects(buttonFast))
303 {open braces start code blocks and must be matched with a close brace
304 ball.setVisible(falsefalse is a value for the boolean type and means not true);
305 ballXSpeed=this assignment operator makes the left side equal to the right side.4;
306 ballYSpeed=this assignment operator makes the left side equal to the right side-.4;
307 makeAndAddBall(ballXSpeed, ballYSpeed);
308
309 }close braces end code blocks and must match an earlier open brace
310
311
312 /** Play the game as longlong is the type for whole numbers (they have a larger range than int) as lives > 0. */
313 ifif executes the next statement only if the condition in parenthesis evaluates to true(getLives()>0)
314 {open braces start code blocks and must be matched with a close brace
315
316 movePaddle();
317 handleCollisions();
318 checkBallYPosition();
319 ifif executes the next statement only if the condition in parenthesis evaluates to true (checkRemainingBricks() <=this evaluates to true if the left side is not more than the right side0)
320 displayGameOver();
321 }close braces end code blocks and must match an earlier open brace
322 }close braces end code blocks and must match an earlier open brace
323 }close braces end code blocks and must match an earlier open brace
|