|
001 packagepackage is used to name the directory or folder a class is in Derrick;
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 * Frank and I worked on thisthis means the current object (the implicit parameter) together but at the end we could not agree on a couple of *items so we went our seperate ways and he turned his completed version in and I turned in *my completed version.*
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 * @authorthis is the Javadoc tag for documenting who created the source code Derrick and Frank
018 */
019 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 AssignmentSevenBreakOut extendsextends means to customize or extend the functionality of a class Game
020 {open braces start code blocks and must be matched with a close brace
021 /** starttLives is the initial value of the
022 * number of times the player can miss the ball with the paddle. */
023 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, startLives, numClicks;
024
025 /** allBricks is an ArrayList that stores the targets to be hit by the ball. */
026 privateprivate is used to restrict access to the current class only ArrayList<Sprite> allBricks;
027 /** ball is the object used to strike targets(bricks) and eventually resulting
028 * in score. */
029 privateprivate is used to restrict access to the current class only OvalSprite ball;
030 /** boundary represents the top and sides of the game field against
031 * which the ball bounces. */
032 privateprivate is used to restrict access to the current class only OutlineSprite boundary ;
033 /** brick is an element of ArrayList, allBricks. */
034 privateprivate is used to restrict access to the current class only RectangleSprite brick;
035 /** score is the number of green bricks hit. */
036 /** 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 play again message and start message*/
037 privateprivate is used to restrict access to the current class only StringSprite score, lives, helpBox, playAgain, start;
038
039 /** paddle is the horizontl line used to keep the ball in play. If the
040 * ball goes below the paddle, lives decreases by one. */
041 privateprivate is used to restrict access to the current class only LineSprite paddle;
042 /** leftWall is a projection added as an enhncement on the left of the game field. */
043 privateprivate is used to restrict access to the current class only PolygonSprite leftWall;
044 /** rightWall is a projection added as an enhncement on the right of the game field. */
045 privateprivate is used to restrict access to the current class only PolygonSprite rightWall;
046 /** ballTransformer give the ball and initial velocity.
047 brickTransformer gives the brick and initial velocity. */
048 privateprivate is used to restrict access to the current class only ProjectileTransformer ballTransformer, brickTransformer;
049 /** numberOfRows is the number of horizontal rows of bricks displayed at the top of the screen. */
050 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;
051 /** numberOfColumns is the number of vertical columns into which the bricks are arranged. */
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 numberOfColumns =this assignment operator makes the left side equal to the right side 8;
053 /** bricksRemaining represents the number of bricks remaining as targets. */
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 bricksRemaining;
055 /** gameOver is a message indicating that the game is over. */
056 privateprivate is used to restrict access to the current class only StringSprite gameOver;
057 /** buttonFast and buttonSlow allows the user to choose the speed of the ball. */
058 privateprivate is used to restrict access to the current class only ButtonSprite buttonFast;
059 privateprivate is used to restrict access to the current class only ButtonSprite buttonSlow;
060 /** ballXSpeed and ballYSpeed initializes the speed of the ball
061 * in the x-direction and y-direction, respecively. */
062 privateprivate is used to restrict access to the current class only doubledouble is the type for numbers that can contain decimal fractions ballXSpeed;
063 privateprivate is used to restrict access to the current class only doubledouble is the type for numbers that can contain decimal fractions ballYSpeed;
064
065
066 /**makes and creates help box.*/
067
068 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value makeAndAddGetHelp()
069 {open braces start code blocks and must be matched with a close brace
070 String helpBox=this assignment operator makes the left side equal to the right side
071 "Press start to begin.<br>"+adds two numbers together or concatenates Strings together
072 "Press 'r' and choose a button.<br>"+adds two numbers together or concatenates Strings together
073 "These buttons will increase or decrease the ball speed";
074
075 setHelpText(helpBox);
076 }close braces end code blocks and must match an earlier open brace
077 /**Creates start set of instructions.*/
078 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value clickStartToPlay()
079 {open braces start code blocks and must be matched with a close brace
080 start =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor StringSprite("Click Start to Play");
081 start.setSize(.9);
082 start.setColor(getColor("orange"));
083 start.setLocation(.5, .5);
084 addSprite(start);
085 }close braces end code blocks and must match an earlier open brace
086 /**Creates buttons.*/
087 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value displayButtons()
088 {open braces start code blocks and must be matched with a close brace
089 buttonFast =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor ButtonSprite("Faster?");
090 buttonFast.setLocation(.2,.95);
091 buttonFast.setSize(.2);
092 addSprite(buttonFast);
093
094 buttonSlow =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor ButtonSprite("Slower?");
095 buttonSlow.setSize(.2);
096 buttonSlow.setLocation(.8,.95);
097
098 addSprite(buttonSlow);
099 }close braces end code blocks and must match an earlier open brace
100 /**Display halve of an oval forfor is a looping structure for repeatedly executing a block of code the left and right walls.*/
101 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value addCurvedWalls()
102 {open braces start code blocks and must be matched with a close brace
103 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);
104 leftWall.setColor(getColor("grey"));
105 leftWall.setSize(1.2);
106 leftWall.setLocation(.01,.5);
107 addSprite(leftWall);
108
109 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);
110 rightWall.setColor(getColor("grey"));
111 rightWall.setSize(1.2);
112 rightWall.setLocation(0.99,.5);
113 addSprite(rightWall);
114 }close braces end code blocks and must match an earlier open brace
115
116
117 /** Determine the count, stored in bricksRemaining, of the number of visible bricks. */
118 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()
119 {open braces start code blocks and must be matched with a close brace
120 bricksRemaining =this assignment operator makes the left side equal to the right side 0;
121 forfor is a looping structure for repeatedly executing a block of code(Sprite single : allBricks)
122 {open braces start code blocks and must be matched with a close brace
123 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)
124 bricksRemaining++this is the increment operator, which increases the variable by 1;
125 }close braces end code blocks and must match an earlier open brace
126 returnreturn means to provide the result of the method and/or cease execution of the method immediately bricksRemaining;
127 }close braces end code blocks and must match an earlier open brace
128
129
130
131 /** Displays the statement "GAME OVER". The ball is also removed from view. */
132 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value displayGameOver()
133 {open braces start code blocks and must be matched with a close brace
134 lives.setVisible(falsefalse is a value for the boolean type and means not true);
135 score.setVisible(falsefalse is a value for the boolean type and means not true);
136 brick.setVisible(falsefalse is a value for the boolean type and means not true);
137 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");
138 gameOver.setSize(.9);
139 gameOver.setColor(getColor("orange"));
140 gameOver.setLocation(.5, .3);
141 addSprite(gameOver);
142 pause();
143 ball.setVisible(falsefalse is a value for the boolean type and means not true);
144 paddle.setVisible(falsefalse is a value for the boolean type and means not true);
145 setLives(0);
146
147 }close braces end code blocks and must match an earlier open brace
148
149
150 /** If the ball position falls below the paddle, it disappears and a life is lost. */
151 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value checkBallYPosition()
152 {open braces start code blocks and must be matched with a close brace
153 ifif executes the next statement only if the condition in parenthesis evaluates to true(ball.getLocation().y>.93)
154 {open braces start code blocks and must be matched with a close brace
155 ball.setVisible(falsefalse is a value for the boolean type and means not true);
156 makeAndAddBall(.1, -.4);
157 startLives =this assignment operator makes the left side equal to the right side getLives();
158 setLives(startLives-1);
159 lives.setText("Lives:"+adds two numbers together or concatenates Strings togethergetLives());
160 }close braces end code blocks and must match an earlier open brace
161 ifif executes the next statement only if the condition in parenthesis evaluates to true (getLives()<1)
162 displayGameOver();
163 }close braces end code blocks and must match an earlier open brace
164
165 /** 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 it does,
166 * make the brick disappear and increase the score by 10. */
167 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value checkBallCollidingWithBrick()
168 {open braces start code blocks and must be matched with a close brace
169 forfor is a looping structure for repeatedly executing a block of code(Sprite single: allBricks)
170 {open braces start code blocks and must be matched with a close brace
171 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))
172 {open braces start code blocks and must be matched with a close brace
173 ball.bounceOffOf(single);
174
175 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")
176 {open braces start code blocks and must be matched with a close brace
177 single.setVisible(falsefalse is a value for the boolean type and means not true);
178 startScore =this assignment operator makes the left side equal to the right side getScore();
179 setScore(startScore+adds two numbers together or concatenates Strings together1);
180 score.setText("Score:"+adds two numbers together or concatenates Strings togethergetScore());
181 }close braces end code blocks and must match an earlier open brace
182 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")
183 single.setColor(getColor("green"));
184 }close braces end code blocks and must match an earlier open brace
185 }close braces end code blocks and must match an earlier open brace
186
187 }close braces end code blocks and must match an earlier open brace
188
189 /** Provide a surface from which the ball can bounce. The bottom boundary
190 * is not visible, but the ball never reaches it because of checks on the
191 * y-position of the ball. */
192 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value makeAndAddBoundary()
193 {open braces start code blocks and must be matched with a close brace
194 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));
195 boundary.setLineThickness(0.05);
196 boundary.setLocation(0.5, 0.6);
197 boundary.setSize(1.0);
198 boundary.setColor(getColor("Gray"));
199 addSprite(boundary);
200
201 }close braces end code blocks and must match an earlier open brace
202
203 /** Establish and display the number of lives remaining forfor is a looping structure for repeatedly executing a block of code the player
204 * and the score based on the number of bricks hit. */
205 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value makeAndAddScoreAndLives()
206 {open braces start code blocks and must be matched with a close brace
207 setLives(3);
208 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());
209 lives.setLocation(0.05, 0.05);
210 lives.setSize(0.25);
211 lives.setColor(getColor("white"));
212 lives.leftJustify();
213 addSprite(lives);
214
215 setScore(0);
216 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());
217 score.setLocation(0.95, 0.05);
218 score.setSize(0.25);
219 score.setColor(getColor("white"));
220 score.rightJustify();
221 addSprite(score);
222 }close braces end code blocks and must match an earlier open brace
223
224
225 /** A ball will be used to hit the bricks. A ball is created at the point (.5, .85)
226 * and initially moves with speed provided as parameters.
227 * The parameters provide the speed in the x-direction as the y-direction.
228 * @paramthis is the Javadoc tag for documenting the purpose of parameters x is the the initial velocity of the ball in the x-direction.
229 * @paramthis is the Javadoc tag for documenting the purpose of parameters y is the initial velocity of the ball in the y-direction. */
230 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)
231 {open braces start code blocks and must be matched with a close brace
232 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);
233 ball.setLocation(0.5, 0.85);
234 ball.setSize(0.05);
235 ball.setColor(getColor("yellow"));
236 addSprite(ball);
237 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);
238 ball.setTracker(ballTransformer);
239 ball.setBlurLength(3);
240 }close braces end code blocks and must match an earlier open brace
241
242 /** Create a line Sprite to be used as a paddle. */
243 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value makeAndAddPaddle()
244 {open braces start code blocks and must be matched with a close brace
245 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);
246 paddle.setLocation(0.5, 0.9);
247 paddle.setColor(getColor("yellow"));
248 paddle.setLineThickness(0.05);
249 addSprite(paddle);
250 }close braces end code blocks and must match an earlier open brace
251
252 /** Move the paddle so that the ball can bounce off of the paddle. */
253 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value movePaddle()
254 {open braces start code blocks and must be matched with a close brace
255 paddle.setX(getMouseX());
256 }close braces end code blocks and must match an earlier open brace
257
258 /* Cause the ball to bounce off the boundary, paddle, or brick. **/
259 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value handleCollisions()
260 {open braces start code blocks and must be matched with a close brace
261
262
263 ball.bounceOffOf(boundary);
264
265 ball.bounceOffOf(paddle);
266
267 ball.bounceOffOf(leftWall);
268 ball.bounceOffOf(rightWall);
269
270 checkBallCollidingWithBrick();
271 }close braces end code blocks and must match an earlier open brace
272 /** Create bricks, display them on the screen, and add to an ArrayList, bricks. */
273 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value createBricks()
274 {open braces start code blocks and must be matched with a close brace
275 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>();
276 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)
277 {open braces start code blocks and must be matched with a close brace
278 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)
279 {open braces start code blocks and must be matched with a close brace
280 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);
281 brick.setColor(getColor("Red"));
282 brick.setSize(.7/numberOfColumns);
283 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);
284 brick.setVisible(truetrue is the boolean value that is the opposite of false);
285 addSprite(brick);
286 allBricks.add(brick);
287 }close braces end code blocks and must match an earlier open brace
288 }close braces end code blocks and must match an earlier open brace
289 }close braces end code blocks and must match an earlier open brace
290 /**Changes the speed of the ball based on player's choice.*/
291 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value changeSpeed()
292 {open braces start code blocks and must be matched with a close brace
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 buttonSlow.setVisible(falsefalse is a value for the boolean type and means not true);
301 buttonFast.setVisible(falsefalse is a value for the boolean type and means not true);
302
303 }close braces end code blocks and must match an earlier open brace
304 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))
305 {open braces start code blocks and must be matched with a close brace
306 ball.setVisible(falsefalse is a value for the boolean type and means not true);
307 ballXSpeed=this assignment operator makes the left side equal to the right side.1;
308 ballYSpeed=this assignment operator makes the left side equal to the right side-.8;
309 makeAndAddBall(ballXSpeed, ballYSpeed);
310 buttonSlow.setVisible(falsefalse is a value for the boolean type and means not true);
311 buttonFast.setVisible(falsefalse is a value for the boolean type and means not true);
312
313
314 }close braces end code blocks and must match an earlier open brace
315 }close braces end code blocks and must match an earlier open brace
316 /**sets up the game*/
317 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value setup()
318 {open braces start code blocks and must be matched with a close brace
319 makeAndAddGetHelp();
320 createBricks();
321 makeAndAddPaddle();
322 makeAndAddBoundary();
323 makeAndAddScoreAndLives();
324 bricksRemaining =this assignment operator makes the left side equal to the right side 0;
325 addCurvedWalls();
326 clickStartToPlay();
327 makeAndAddBall(.1,-.6);
328 numClicks++this is the increment operator, which increases the variable by 1;
329
330
331 }close braces end code blocks and must match an earlier open brace
332
333 /**handle input and game events*/
334 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value advance()
335 {open braces start code blocks and must be matched with a close brace
336 changeSpeed();
337
338 /** Removes the start instruction */
339
340 ifif executes the next statement only if the condition in parenthesis evaluates to true(numClicks==this is the comparison operator which evaluates to true if both sides are the same1)
341 {open braces start code blocks and must be matched with a close brace
342 start.setVisible(falsefalse is a value for the boolean type and means not true);
343 numClicks=this assignment operator makes the left side equal to the right side0;
344 }close braces end code blocks and must match an earlier open brace
345
346 /** If the player presses the r button the player gets the option of a faster or slower ball. */
347
348 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'r')
349 {open braces start code blocks and must be matched with a close brace
350 displayButtons();
351 }close braces end code blocks and must match an earlier open brace
352
353
354
355
356 ifif executes the next statement only if the condition in parenthesis evaluates to true(getLives()>0)
357 {open braces start code blocks and must be matched with a close brace
358 movePaddle();
359 // allows the user to move the paddle.
360 handleCollisions();
361 // bounces the ball of the walls; kills bricks.
362 checkBallYPosition();
363 // used to determine whether to end the game
364 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)
365
366 // If there are no bricks remaining,
367 displayGameOver();
368
369 //the game is over.
370 }close braces end code blocks and must match an earlier open brace
371 }close braces end code blocks and must match an earlier open brace
372 }close braces end code blocks and must match an earlier open brace
|