|
001 packagepackage is used to name the directory or folder a class is in HemendraPavitra;
002
003 importimport means to make the classes and/or packages available in this program wiki.Wiki;
004 importimport means to make the classes and/or packages available in this program fang.*;
005 importimport means to make the classes and/or packages available in this program java.awt.*;
006 importimport means to make the classes and/or packages available in this program java.awt.geom.*;
007
008 /**
009 * All about my game here.
010 * @authorthis is the Javadoc tag for documenting who created the source code Ppullay,Hpullay
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 Breakout extendsextends means to customize or extend the functionality of a class GameLoop
013 {open braces start code blocks and must be matched with a close brace
014 /**a rectangle*/
015 privateprivate is used to restrict access to the current class only Sprite rectangle, topCeiling, leftWall, rightWall, gameFloor;
016 privateprivate is used to restrict access to the current class only doubledouble is the type for numbers that can contain decimal fractions startYCoord, startXCoord;
017 privateprivate is used to restrict access to the current class only Sprite paddle, ball;
018 privateprivate is used to restrict access to the current class only ProjectileTracker tracker;
019 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer level;
020 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 bricks;
021 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;
022 privateprivate is used to restrict access to the current class only StringSprite scoreSprite, livesSprite, gameOver, youWin,txtBonusLives;
023
024 /**sets up the game*/
025 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value startGame()
026 {open braces start code blocks and must be matched with a close brace
027 makeSprites();
028 addSprites();
029 makeBricks();
030 maketopCeiling();
031 makeLeftWall();
032 makeRightWall();
033 makePaddle();
034 makeBall();
035 makeTracker();
036 makeGameFloor();
037 level=this assignment operator makes the left side equal to the right side0;
038 score=this assignment operator makes the left side equal to the right side0;
039 lives=this assignment operator makes the left side equal to the right side5;
040 setHelpText ("<h1>Breakout</h1)" +adds two numbers together or concatenates Strings together
041 "<br><br>Hit bricks with the ball to remove from screen.<br><br> Enjoy!");
042
043 }close braces end code blocks and must match an earlier open brace
044
045 /**makes the sprites*/
046 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value makeSprites()
047 {open braces start code blocks and must be matched with a close brace
048 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);
049 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");
050 scoreSprite.setScale(0.2);
051 scoreSprite.rightJustify();
052 scoreSprite.setColor(Color.GREEN);
053 scoreSprite.setLocation(.9, .95);
054 /*Adds score to the game.*/
055
056 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);
057 livesSprite=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor StringSprite("Lives: 5");
058 livesSprite.setScale(0.19);
059 livesSprite.setColor(Color.GREEN);
060 livesSprite.setLocation(.2, .95);
061 /*Adds lives to the game.*/
062
063 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: Loser!!");
064 gameOver.setScale(0.3);
065 gameOver.setColor(Color.RED);
066 gameOver.setLocation(.5, .5);
067 /*Lets player know that game is over.*/
068
069 youWin=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor StringSprite("Winner!!");
070 youWin.setScale(0.3);
071 youWin.setColor(Color.RED);
072 youWin.setLocation(.5, .5);
073 /*Lets player know that they won the game.*/
074
075 txtBonusLives=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor StringSprite("Bonus Lives & Points!!");
076 txtBonusLives.setScale(0.3);
077 txtBonusLives.setColor(Color.RED);
078 txtBonusLives.setLocation(.5, .95);
079 }close braces end code blocks and must match an earlier open brace
080
081 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value makeBricks()
082 {open braces start code blocks and must be matched with a close brace
083 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;
084 bricks=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) arrays70]brackets are typically used to declare, initialize and index (indicate which element of) arrays;
085 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 side5;
086 doubledouble is the type for numbers that can contain decimal fractions startLocationY=this assignment operator makes the left side equal to the right side.25;
087 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;
088 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;
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 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)
090 {open braces start code blocks and must be matched with a close brace
091 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;
092
093 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 side14;
094 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;
095 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;
096 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;
097 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)
098 {open braces start code blocks and must be matched with a close brace
099 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;
100 bricks[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);
101 bricks[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);
102 bricks[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);
103 bricks[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);
104 canvas.addSprite(bricks[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);
105 index++this is the increment operator, which increases the variable by 1;
106 /*Makes the bricks to add to the game.*/
107 }close braces end code blocks and must match an earlier open brace
108 }close braces end code blocks and must match an earlier open brace
109 }close braces end code blocks and must match an earlier open brace
110
111 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value maketopCeiling()
112 {open braces start code blocks and must be matched with a close brace
113 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);
114 topCeiling.setLocation(.5, .01);
115 topCeiling.setColor(Color.RED);
116 canvas.addSprite(topCeiling);
117 /*Makes ceiling to add to the game.*/
118 }close braces end code blocks and must match an earlier open brace
119
120 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value makeLeftWall()
121 {open braces start code blocks and must be matched with a close brace
122 leftWall =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor RectangleSprite(1, 10);
123 leftWall.setLocation(startXCoord / 4.5, .5);
124 leftWall.setColor(Color.RED);
125 canvas.addSprite(leftWall);
126 /*Makes left wall to add to the game.*/
127 }close braces end code blocks and must match an earlier open brace
128
129 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value makeRightWall()
130 {open braces start code blocks and must be matched with a close brace
131 rightWall =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor RectangleSprite(1, 10);
132 rightWall.setLocation(1 - startXCoord / 4.5, .5);
133 rightWall.setColor(Color.RED);
134 canvas.addSprite(rightWall);
135 /*Makes right wall to add to the game.*/
136 }close braces end code blocks and must match an earlier open brace
137
138 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value makeGameFloor()
139 {open braces start code blocks and must be matched with a close brace
140 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);
141 gameFloor.setLocation(.5, .99);
142 gameFloor.setColor(Color.BLACK);
143 canvas.addSprite(gameFloor);
144 /*Makes floor to add to the game.*/
145 }close braces end code blocks and must match an earlier open brace
146
147 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value makePaddle()
148 {open braces start code blocks and must be matched with a close brace
149 paddle =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor OvalSprite(5, .5);
150 paddle.setLocation(.5, .8);
151 paddle.setScale(.15);
152 paddle.setColor(Color.BLACK);
153 canvas.addSprite(paddle);
154 /*Makes paddle to add to game.*/
155 }close braces end code blocks and must match an earlier open brace
156
157 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value makeBall()
158 {open braces start code blocks and must be matched with a close brace
159 ball =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor OvalSprite(1, 1);
160 ball.setLocation(.5, .7799);
161 ball.setScale(.02);
162 ball.setColor(Color.BLACK);
163 canvas.addSprite(ball);
164 /*Makes ball to add to game.*/
165 }close braces end code blocks and must match an earlier open brace
166
167 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value randomColorBound()
168 {open braces start code blocks and must be matched with a close brace
169 ball.setColor(newnew is used to create objects by calling the constructor Color(random.nextInt()));
170 paddle.setColor(newnew is used to create objects by calling the constructor Color(random.nextInt()));
171 /*Makes the ball and paddle random colors.*/
172 }close braces end code blocks and must match an earlier open brace
173
174 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value makeTracker()
175 {open braces start code blocks and must be matched with a close brace
176 tracker=this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor ProjectileTracker(0.35,-.35);
177 ball.setTracker(tracker);
178 /*Makes tracker forfor is a looping structure for repeatedly executing a block of code the ball.*/
179 }close braces end code blocks and must match an earlier open brace
180
181 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value wallBounce()
182 {open braces start code blocks and must be matched with a close brace
183 ifif executes the next statement only if the condition in parenthesis evaluates to true(paddle.intersects(ball))
184 {open braces start code blocks and must be matched with a close brace
185 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());
186 tracker.bounce(normal);
187 /*Makes ball bounce off of paddle.*/
188 }close braces end code blocks and must match an earlier open brace
189
190 ifif executes the next statement only if the condition in parenthesis evaluates to true(rightWall.intersects(ball))
191 {open braces start code blocks and must be matched with a close brace
192 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());
193 tracker.bounce(normal);
194 /*Makes ball bounce off right wall.*/
195 }close braces end code blocks and must match an earlier open brace
196
197 ifif executes the next statement only if the condition in parenthesis evaluates to true(leftWall.intersects(ball))
198 {open braces start code blocks and must be matched with a close brace
199 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());
200 tracker.bounce(normal);
201 /*Makes ball bounce off left wall.*/
202 }close braces end code blocks and must match an earlier open brace
203
204 ifif executes the next statement only if the condition in parenthesis evaluates to true(topCeiling.intersects(ball))
205 {open braces start code blocks and must be matched with a close brace
206 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());
207 tracker.bounce(normal);
208 /*Makes ball bounce off of top ceiling.*/
209 }close braces end code blocks and must match an earlier open brace
210 }close braces end code blocks and must match an earlier open brace
211
212 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value brickIntersects()
213 {open braces start code blocks and must be matched with a close brace
214 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<bricks.length; i++this is the increment operator, which increases the variable by 1)
215 {open braces start code blocks and must be matched with a close brace
216
217 ifif executes the next statement only if the condition in parenthesis evaluates to true(bricks[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 &&) bricks[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))
218 {open braces start code blocks and must be matched with a close brace
219 score++this is the increment operator, which increases the variable by 1;
220 ifif executes the next statement only if the condition in parenthesis evaluates to true(level==this is the comparison operator which evaluates to true if both sides are the same0)
221 {open braces start code blocks and must be matched with a close brace
222 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(bricks[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());
223 tracker.bounce(normal);
224 bricks[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);
225 }close braces end code blocks and must match an earlier open brace
226 }close braces end code blocks and must match an earlier open brace
227 }close braces end code blocks and must match an earlier open brace
228 /*Increases score ifif executes the next statement only if the condition in parenthesis evaluates to true ball intersects brick.*/
229 updateScore();
230 }close braces end code blocks and must match an earlier open brace
231
232 /**adds the sprites to the screen*/
233 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value updateScore()
234 {open braces start code blocks and must be matched with a close brace
235 scoreSprite.setText("Score: "+adds two numbers together or concatenates Strings togetherscore);
236 }close braces end code blocks and must match an earlier open brace
237 /*Adds the score.*/
238 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value updateLives()
239 {open braces start code blocks and must be matched with a close brace
240 livesSprite.setText("Lives: "+adds two numbers together or concatenates Strings togetherlives);
241 }close braces end code blocks and must match an earlier open brace
242 /*Adds the lives.*/
243 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value addSprites()
244 {open braces start code blocks and must be matched with a close brace
245 canvas.addSprite(scoreSprite);
246 canvas.addSprite(livesSprite);
247 }close braces end code blocks and must match an earlier open brace
248 /*Adds the score and lives to the game.*/
249
250 /**handle input and game events*/
251
252 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value resetGame()
253 {open braces start code blocks and must be matched with a close brace
254 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)
255 {open braces start code blocks and must be matched with a close brace
256 canvas.addSprite(gameOver);
257 }close braces end code blocks and must match an earlier open brace
258 }close braces end code blocks and must match an earlier open brace
259
260 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value winGame()
261 {open braces start code blocks and must be matched with a close brace
262 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 same71)
263 {open braces start code blocks and must be matched with a close brace
264 canvas.addSprite(youWin);
265 canvas.removeSprite(ball);
266 canvas.removeSprite(paddle);
267 }close braces end code blocks and must match an earlier open brace
268 }close braces end code blocks and must match an earlier open brace
269 /*gives play a bonus life ifif executes the next statement only if the condition in parenthesis evaluates to true they achieve a score of 20*/
270 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value bonusLives()
271 {open braces start code blocks and must be matched with a close brace
272 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 same35)
273 {open braces start code blocks and must be matched with a close brace
274 lives=this assignment operator makes the left side equal to the right sidelives+adds two numbers together or concatenates Strings together1;
275 score=this assignment operator makes the left side equal to the right sidescore+adds two numbers together or concatenates Strings together1;
276 canvas.addSprite(txtBonusLives);
277 }close braces end code blocks and must match an earlier open brace
278 updateLives();
279 }close braces end code blocks and must match an earlier open brace
280 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)
281 {open braces start code blocks and must be matched with a close brace
282 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;
283 paddle.setLocation(x,paddle.getLocation().y);
284 wallBounce();
285 brickIntersects();
286 randomColorBound(); //Got help from Jayson.
287 winGame();
288 bonusLives();
289 resetGame();
290 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))
291 {open braces start code blocks and must be matched with a close brace
292 lives--this is the decrement operator, which decreases the variable by 1;
293 }close braces end code blocks and must match an earlier open brace
294 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)
295 {open braces start code blocks and must be matched with a close brace
296 lives--this is the decrement operator, which decreases the variable by 1;
297 ball.setLocation(.5,.5);
298 }close braces end code blocks and must match an earlier open brace
299 updateLives();
300 }close braces end code blocks and must match an earlier open brace
301 /*Subtract lives everytime the ball drops below the floor.*/
302 }close braces end code blocks and must match an earlier open brace
|