|
001 importimport means to make the classes and/or packages available in this program fang.*;
002 importimport means to make the classes and/or packages available in this program java.awt.*;
003 importimport means to make the classes and/or packages available in this program java.awt.geom.*;
004
005 /**
006 * BreakOut.
007 * @authornull Chull
008 I received a lot of help from the following student's games. mainly the curved walls from Jayson's and how to removed blocks when the ball hits from Hemendras.
009 http://ggc.javawide.org/index.php/HemendraPavitra/Breakout
010 http://ggc.javawide.org/index.php/Jayson/breakout/Breakout
011
012 */
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 Blocktrial extendsextends means to customize or extend the functionality of a class GameLoop
015 {open braces start code blocks and must be matched with a close brace
016
017 /** the boundaries forfor is a looping structure for repeatedly executing a block of code the game */
018 privateprivate is used to restrict access to the current class only Sprite rectangle, topCeiling, leftWall, rightWall, gameFloor;
019
020 /**the coordinates forfor is a looping structure for repeatedly executing a block of code x and y that the game uses forfor is a looping structure for repeatedly executing a block of code spacing */
021 privateprivate is used to restrict access to the current class only doubledouble is the type for numbers that can contain decimal fractions startYCoord, startXCoord;
022
023 /**The score and lives*/
024 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer score, lives;
025
026 /**an block*/
027 privateprivate is used to restrict access to the current class only Sprite[brackets are typically used to declare, initialize and index (indicate which element of) arrays]brackets are typically used to declare, initialize and index (indicate which element of) arrays block;
028
029 /**a ball*/
030 privateprivate is used to restrict access to the current class only Sprite ball;
031
032 /**give extra life*/
033 privateprivate is used to restrict access to the current class only Sprite extra;
034
035 /**gives extra Points*/
036 privateprivate is used to restrict access to the current class only Sprite points;
037
038 /**a paddle*/
039 privateprivate is used to restrict access to the current class only Sprite paddle;
040
041 /**The Score, lives , game win and game over sections*/
042 privateprivate is used to restrict access to the current class only StringSprite scoreSprite, livesSprite, gameWin, gameOver;
043
044 /**The Projectile Tracker which sets how the ball moves*/
045 privateprivate is used to restrict access to the current class only ProjectileTracker tracker;
046
047
048
049 /**sets up the game*/
050 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value startGame()
051
052 {open braces start code blocks and must be matched with a close brace
053 makePowerUpTwo();
054 scheduleRelative(newnew is used to create objects by calling the constructor PowerUp(), 1);
055 scheduleRelative(newnew is used to create objects by calling the constructor PowerUpTwo(), 5);
056 makeSprites();
057 makeBricks();
058 maketopCeiling();
059 makeLeftWall();
060 makeRightWall();
061 makeGameFloor();
062 makeGameParts();
063 makePowerUp();
064 makeGameStatus();
065
066
067 score=this assignment operator makes the left side equal to the right side0;
068 lives=this assignment operator makes the left side equal to the right side5;
069 setHelpText("Use the mouse to move the paddle to destroy blocks. To win break all the blocks or get 24 points. Each block is worth 1 point. There are 2 power ups which drop down Extra Life and Extra Point. The game is winnable without breaking all the blocks as long as you get 24 points.");
070 addSprites();
071
072 }close braces end code blocks and must match an earlier open brace
073 /**Makes the bricks*/
074 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value makeBricks()
075 {open braces start code blocks and must be matched with a close brace
076 intint is the type for whole numbers and it is short for integer index=this assignment operator makes the left side equal to the right side0;
077 block=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor Sprite[brackets are typically used to declare, initialize and index (indicate which element of) arrays24]brackets are typically used to declare, initialize and index (indicate which element of) arrays;
078 intint is the type for whole numbers and it is short for integer numBricksHigh=this assignment operator makes the left side equal to the right side3;
079 doubledouble is the type for numbers that can contain decimal fractions startLocationY=this assignment operator makes the left side equal to the right side0.25;
080 doubledouble is the type for numbers that can contain decimal fractions endLocationY=this assignment operator makes the left side equal to the right side1-startLocationY;
081 doubledouble is the type for numbers that can contain decimal fractions distanceHigh=this assignment operator makes the left side equal to the right sideendLocationY-startLocationY;
082 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 side0; j<numBricksHigh; j++this is the increment operator, which increases the variable by 1)
083 {open braces start code blocks and must be matched with a close brace
084 doubledouble is the type for numbers that can contain decimal fractions y=this assignment operator makes the left side equal to the right sidej*distanceHigh/(numBricksHigh-1)+adds two numbers together or concatenates Strings togetherstartLocationY;
085 intint is the type for whole numbers and it is short for integer numBricksAcross=this assignment operator makes the left side equal to the right side8;
086 doubledouble is the type for numbers that can contain decimal fractions startLocationX=this assignment operator makes the left side equal to the right side0.1;
087 doubledouble is the type for numbers that can contain decimal fractions endLocationX=this assignment operator makes the left side equal to the right side1-startLocationX;
088 doubledouble is the type for numbers that can contain decimal fractions distanceAcross=this assignment operator makes the left side equal to the right sideendLocationX-startLocationX;
089 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 side0; i<numBricksAcross; i++this is the increment operator, which increases the variable by 1)
090
091 {open braces start code blocks and must be matched with a close brace
092 doubledouble is the type for numbers that can contain decimal fractions x=this assignment operator makes the left side equal to the right sidei*distanceAcross/(numBricksAcross-1)+adds two numbers together or concatenates Strings togetherstartLocationX;
093 block[brackets are typically used to declare, initialize and index (indicate which element of) arraysindex]brackets are typically used to declare, initialize and index (indicate which element of) arrays=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor RectangleSprite(2, 1);
094 block[brackets are typically used to declare, initialize and index (indicate which element of) arraysindex]brackets are typically used to declare, initialize and index (indicate which element of) arrays.setScale(0.055);
095 block[brackets are typically used to declare, initialize and index (indicate which element of) arraysindex]brackets are typically used to declare, initialize and index (indicate which element of) arrays.setLocation(x,y/3.5);
096 block[brackets are typically used to declare, initialize and index (indicate which element of) arraysindex]brackets are typically used to declare, initialize and index (indicate which element of) arrays.setColor(Color.RED);
097 canvas.addSprite(block[brackets are typically used to declare, initialize and index (indicate which element of) arraysindex]brackets are typically used to declare, initialize and index (indicate which element of) arrays);
098 index++this is the increment operator, which increases the variable by 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 }close braces end code blocks and must match an earlier open brace
102 /**Creates the top wall.*/
103 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value maketopCeiling()
104 {open braces start code blocks and must be matched with a close brace
105 topCeiling =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor RectangleSprite(20, .5);
106 topCeiling.setLocation(.5, .01);
107 topCeiling.setColor(Color.RED);
108 canvas.addSprite(topCeiling);
109 }close braces end code blocks and must match an earlier open brace
110
111 /**Creates the left wall.*/
112 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value makeLeftWall()
113 {open braces start code blocks and must be matched with a close brace
114 leftWall =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor OvalSprite(1, 12);
115 leftWall.setLocation(startXCoord / 4.5, .5);
116 leftWall.setScale(1.2);
117 leftWall.setColor(Color.RED);
118 canvas.addSprite(leftWall);
119 }close braces end code blocks and must match an earlier open brace
120
121 /**Creates the right wall.*/
122 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value makeRightWall()
123 {open braces start code blocks and must be matched with a close brace
124 rightWall =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor OvalSprite(1, 12);
125 rightWall.setLocation(1 - startXCoord / 4.5, .5);
126 leftWall.setScale(1.2);
127 rightWall.setColor(Color.RED);
128 canvas.addSprite(rightWall);
129 }close braces end code blocks and must match an earlier open brace
130
131 /**Creates the bottom wall.*/
132 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value makeGameFloor()
133 {open braces start code blocks and must be matched with a close brace
134 gameFloor =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor RectangleSprite(20,.5);
135 gameFloor.setLocation(0, 1);
136 gameFloor.setColor(Color.BLUE);
137 gameFloor.setScale(10);
138 /** canvas.addSprite(gameFloor); **/
139
140 }close braces end code blocks and must match an earlier open brace
141
142 /**Creates the ball, paddle, and tracker.*/
143 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value makeGameParts()
144 {open braces start code blocks and must be matched with a close brace
145
146 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);
147 ball.setLocation(.5, .6);
148 ball.setScale(.025);
149
150 tracker=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor ProjectileTracker(0, .5);
151 ball.setTracker(tracker);
152
153
154
155 paddle =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor OvalSprite(6, 1);
156 paddle.setScale(0.2);
157 paddle.setLocation(0.5, 0.8);
158 paddle.setColor(Color.RED);
159
160 }close braces end code blocks and must match an earlier open brace
161
162 /**What happens when the ball hits the wall.*/
163 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value makeBallWall()
164 {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(topCeiling.intersects(ball))
166 {open braces start code blocks and must be matched with a close brace
167 doubledouble is the type for numbers that can contain decimal fractions normal=this assignment operator makes the left side equal to the right sideSprite.getNormalVector(topCeiling.getShape(), ball.getShape());
168 tracker.bounce(normal);
169 }close braces end code blocks and must match an earlier open brace
170
171 ifif executes the next statement only if the condition in parenthesis evaluates to true(leftWall.intersects(ball))
172 {open braces start code blocks and must be matched with a close brace
173 doubledouble is the type for numbers that can contain decimal fractions normal=this assignment operator makes the left side equal to the right sideSprite.getNormalVector(leftWall.getShape(), ball.getShape());
174 tracker.bounce(normal);
175 }close braces end code blocks and must match an earlier open brace
176
177 ifif executes the next statement only if the condition in parenthesis evaluates to true(rightWall.intersects(ball))
178 {open braces start code blocks and must be matched with a close brace
179 doubledouble is the type for numbers that can contain decimal fractions normal=this assignment operator makes the left side equal to the right sideSprite.getNormalVector(rightWall.getShape(), ball.getShape());
180 tracker.bounce(normal);
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(extra.intersects(paddle))
183 {open braces start code blocks and must be matched with a close brace
184
185 ifif executes the next statement only if the condition in parenthesis evaluates to true(extra.isVisible() &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 &&) extra.intersects(paddle))
186 lives++this is the increment operator, which increases the variable by 1;
187 extra.setVisible(falsefalse is a value for the boolean type and means not true);
188
189 }close braces end code blocks and must match an earlier open brace
190
191 ifif executes the next statement only if the condition in parenthesis evaluates to true(points.intersects(paddle))
192 {open braces start code blocks and must be matched with a close brace
193 ifif executes the next statement only if the condition in parenthesis evaluates to true(points.isVisible() &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 &&) points.intersects(paddle))
194 score++this is the increment operator, which increases the variable by 1;
195 points.setVisible(falsefalse is a value for the boolean type and means not true);
196 }close braces end code blocks and must match an earlier open brace
197
198
199 }close braces end code blocks and must match an earlier open brace
200
201 /**makes the sprites*/
202 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value makeSprites()
203 {open braces start code blocks and must be matched with a close brace
204 /*Adds score to the game which keeps track oh how well you are doing.*/
205 scoreSprite=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 togetherscore);
206 scoreSprite=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor StringSprite("Score: 0");
207 scoreSprite.setScale(0.2);
208 scoreSprite.rightJustify();
209 scoreSprite.setColor(Color.BLUE);
210 scoreSprite.setLocation(.9, .95);
211
212 /*Adds lives to the game which determines how many trys you have left.*/
213 livesSprite=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 togetherlives);
214 livesSprite=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor StringSprite("Lives: 3");
215 livesSprite.setScale(0.19);
216 livesSprite.setColor(Color.BLUE);
217 livesSprite.setLocation(.2, .95);
218
219 }close braces end code blocks and must match an earlier open brace
220 /**displays game won or lost*/
221 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value makeGameStatus()
222 {open braces start code blocks and must be matched with a close brace
223
224 /*Lets player know that game is over and they have no more lives.*/
225 gameOver=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor StringSprite("Game Over!");
226 gameOver.setScale(0.3);
227 gameOver.setColor(Color.RED);
228 gameOver.setLocation(.5, .5);
229
230 /*Lets player know that game is won.*/
231 gameWin=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor StringSprite("You Win!");
232 gameWin.setScale(0.3);
233 gameWin.setColor(Color.RED);
234 gameWin.setLocation(.5, .5);
235 }close braces end code blocks and must match an earlier open brace
236
237
238 /** Gives power ups*/
239 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value makePowerUp()
240 {open braces start code blocks and must be matched with a close brace
241 extra=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor ImageSprite(Wiki.getMedia("Life.JPG"));
242 extra.setScale(0.20);
243 extra.setLocation(0.5, 0.1);
244
245
246 }close braces end code blocks and must match an earlier open brace
247
248 /** Gives power ups*/
249 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value makePowerUpTwo()
250 {open braces start code blocks and must be matched with a close brace
251
252
253 points=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor ImageSprite(Wiki.getMedia("Points.JPG"));
254 points.setScale(0.20);
255 points.setLocation(0.5, 0.1);
256 }close braces end code blocks and must match an earlier open brace
257
258
259
260 /**calls PowerUp*/
261 classclass is a group of fields and methods used for making objects PowerUp implementsimplements means providing method bodies for the methods declared in the corresponding interface Alarm
262 {open braces start code blocks and must be matched with a close brace
263 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value alarm()
264 {open braces start code blocks and must be matched with a close brace
265
266 ProjectileTracker extraT;
267 extraT=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor ProjectileTracker(0, .5);
268 extra.setTracker(extraT);
269 canvas.addSprite(extra);
270 extra.setLocation(0.5, 0.1);
271 scheduleRelative(thisthis means the current object (the implicit parameter),10);
272 extra.setVisible(truetrue is the boolean value that is the opposite of false);
273
274 }close braces end code blocks and must match an earlier open brace
275 }close braces end code blocks and must match an earlier open brace
276
277 /**calls PowerUpTwo*/
278 classclass is a group of fields and methods used for making objects PowerUpTwo implementsimplements means providing method bodies for the methods declared in the corresponding interface Alarm
279 {open braces start code blocks and must be matched with a close brace
280 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value alarm()
281 {open braces start code blocks and must be matched with a close brace
282
283
284 ProjectileTracker pointsT;
285 pointsT=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor ProjectileTracker(0, .5);
286 points.setTracker(pointsT);
287 canvas.addSprite(points);
288 points.setLocation(0.5, 0.1);
289 scheduleRelative(thisthis means the current object (the implicit parameter),15);
290 points.setVisible(truetrue is the boolean value that is the opposite of false);
291
292
293 }close braces end code blocks and must match an earlier open brace
294 }close braces end code blocks and must match an earlier open brace
295
296 /**adds the sprites to the screen*/
297 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value addSprites()
298 {open braces start code blocks and must be matched with a close brace
299 canvas.addSprite(scoreSprite);
300 canvas.addSprite(livesSprite);
301 canvas.addSprite(block);
302 canvas.addSprite(paddle);
303 canvas.addSprite(ball);
304 }close braces end code blocks and must match an earlier open brace
305 /**This part of the code is forfor is a looping structure for repeatedly executing a block of code when the ball hits the block.*/
306 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value brickIntersects()
307 {open braces start code blocks and must be matched with a close brace
308 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 side0; i<block.length; i++this is the increment operator, which increases the variable by 1)
309 {open braces start code blocks and must be matched with a close brace
310
311 ifif executes the next statement only if the condition in parenthesis evaluates to true(block[brackets are typically used to declare, initialize and index (indicate which element of) arraysi]brackets are typically used to declare, initialize and index (indicate which element of) arrays.isVisible() &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 &&) block[brackets are typically used to declare, initialize and index (indicate which element of) arraysi]brackets are typically used to declare, initialize and index (indicate which element of) arrays.intersects(ball))
312 {open braces start code blocks and must be matched with a close brace
313 score++this is the increment operator, which increases the variable by 1;
314
315 {open braces start code blocks and must be matched with a close brace
316 doubledouble is the type for numbers that can contain decimal fractions normal=this assignment operator makes the left side equal to the right sideSprite.getNormalVector(block[brackets are typically used to declare, initialize and index (indicate which element of) arraysi]brackets are typically used to declare, initialize and index (indicate which element of) arrays.getShape(), ball.getShape());
317 tracker.bounce(normal);
318 block[brackets are typically used to declare, initialize and index (indicate which element of) arraysi]brackets are typically used to declare, initialize and index (indicate which element of) arrays.setVisible(falsefalse is a value for the boolean type and means not true);
319 }close braces end code blocks and must match an earlier open brace
320 }close braces end code blocks and must match an earlier open brace
321 }close braces end code blocks and must match an earlier open brace
322 /*Increases score ifif executes the next statement only if the condition in parenthesis evaluates to true ball intersects block.*/
323 updateScore();
324 }close braces end code blocks and must match an earlier open brace
325
326 /**adds the sprites to the screen*/
327 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value updateScore()
328 {open braces start code blocks and must be matched with a close brace
329 scoreSprite.setText("Score: "+adds two numbers together or concatenates Strings togetherscore);
330 }close braces end code blocks and must match an earlier open brace
331 /*Adds the score.*/
332 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value updateLives()
333 {open braces start code blocks and must be matched with a close brace
334 livesSprite.setText("Lives: "+adds two numbers together or concatenates Strings togetherlives);
335 }close braces end code blocks and must match an earlier open brace
336
337 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value resetGame()
338 {open braces start code blocks and must be matched with a close brace
339 ifif executes the next statement only if the condition in parenthesis evaluates to true(lives==this is the comparison operator which evaluates to true if both sides are the same0)
340 {open braces start code blocks and must be matched with a close brace
341 canvas.addSprite(gameOver);
342 }close braces end code blocks and must match an earlier open brace
343
344 ifif executes the next statement only if the condition in parenthesis evaluates to true(score==this is the comparison operator which evaluates to true if both sides are the same24)
345 {open braces start code blocks and must be matched with a close brace
346 canvas.addSprite(gameWin);
347 }close braces end code blocks and must match an earlier open brace
348 }close braces end code blocks and must match an earlier open brace
349
350 /**handle input and game events*/
351 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value advanceFrame(doubledouble is the type for numbers that can contain decimal fractions timePassed)
352 {open braces start code blocks and must be matched with a close brace
353 resetGame();
354 makeBallWall();
355
356 ifif executes the next statement only if the condition in parenthesis evaluates to true (lives ==this is the comparison operator which evaluates to true if both sides are the same 1 &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 &&) gameFloor.intersects(ball))
357 {open braces start code blocks and must be matched with a close brace
358 lives--this is the decrement operator, which decreases the variable by 1;
359 }close braces end code blocks and must match an earlier open brace
360 ifif executes the next statement only if the condition in parenthesis evaluates to true(gameFloor.intersects(ball) &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 &&) lives > 1)
361 {open braces start code blocks and must be matched with a close brace
362 lives--this is the decrement operator, which decreases the variable by 1;
363 ball.setLocation(.5,.5);
364 }close braces end code blocks and must match an earlier open brace
365 updateLives();
366
367 brickIntersects();
368 doubledouble is the type for numbers that can contain decimal fractions x=this assignment operator makes the left side equal to the right sidegetPlayer().getMouse().getLocation().x;
369
370 paddle.setLocation(x , paddle.getLocation().y);
371
372 ifif executes the next statement only if the condition in parenthesis evaluates to true(paddle.intersects(ball))
373 {open braces start code blocks and must be matched with a close brace
374 doubledouble is the type for numbers that can contain decimal fractions normal=this assignment operator makes the left side equal to the right sideSprite.getNormalVector(paddle.getShape(), ball.getShape());
375 tracker.bounce(normal);
376 }close braces end code blocks and must match an earlier open brace
377 }close braces end code blocks and must match an earlier open brace
378 }close braces end code blocks and must match an earlier open brace
379
|