|
001 packagepackage is used to name the directory or folder a class is in BellOlson;
002 importimport means to make the classes and/or packages available in this program wiki.Wiki;
003 importimport means to make the classes and/or packages available in this program fang.*;
004 importimport means to make the classes and/or packages available in this program java.awt.*;
005 importimport means to make the classes and/or packages available in this program java.awt.geom.*;
006
007 /**
008 * Based on air hockey.
009 * @authorthis is the Javadoc tag for documenting who created the source code Gbell &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 &&) Jolson
010 */
011 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 FinalAssignment extendsextends means to customize or extend the functionality of a class GameLoop
012 {open braces start code blocks and must be matched with a close brace
013 privateprivate is used to restrict access to the current class only Sprite leftBorder, rightBorder, topBorder, floor;
014 privateprivate is used to restrict access to the current class only Sprite paddle, paddle2, background;
015 privateprivate is used to restrict access to the current class only Sprite ball, goal, goal2;
016 privateprivate is used to restrict access to the current class only ProjectileTracker tracker, tracker2, tracker3, handicap;
017 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer scoreBottom, scoreTop, trackScore, difficulty;
018 privateprivate is used to restrict access to the current class only StringSprite gameover, restart, scoreBottomText, scoreTopText, win, start, directions;
019 privateprivate is used to restrict access to the current class only Color color =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor Color(150, 150, 150);
020 privateprivate is used to restrict access to the current class only booleanboolean is a type that is either true or false dead =this assignment operator makes the left side equal to the right side falsefalse is a value for the boolean type and means not true;
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 playernum;
022 privateprivate is used to restrict access to the current class only Sprite bar, circle, circle2;
023
024 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value startGame()
025 {open braces start code blocks and must be matched with a close brace
026 scoreBottom=this assignment operator makes the left side equal to the right side0;
027 scoreTop=this assignment operator makes the left side equal to the right side0;
028 trackScore=this assignment operator makes the left side equal to the right side0;
029 makeSprites();
030 toggleAudible();
031 setHelpText("Use the mouse to control the paddle. Don't miss the puck.<br> Press start to begin.");
032 }close braces end code blocks and must match an earlier open brace
033
034 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value makeSprites()
035 {open braces start code blocks and must be matched with a close brace
036 background=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor RectangleSprite(1,1);
037 background.setColor(Color.WHITE);
038 background.setScale(5);
039 background.setLocation(.5,.5);
040 canvas.addSprite(background);
041
042 bar=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor RectangleSprite(90,1);
043 bar.setColor(Color.BLACK);
044 bar.setScale(1);
045 bar.setLocation(.5,.5);
046 canvas.addSprite(bar);
047
048 circle=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor OvalSprite(1,1);
049 circle.setLocation(.5,.5);
050 circle.setColor(Color.BLACK);
051 circle.setScale(.1);
052 canvas.addSprite(circle);
053
054 circle2=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor OvalSprite(1,1);
055 circle2.setLocation(.5,.5);
056 circle2.setColor(Color.WHITE);
057 circle2.setScale(.08);
058 canvas.addSprite(circle2);
059
060 makeTexts();
061 makeBallandGoal();
062 makePaddle();
063 makeTracker();
064 makeScores();
065 makeBorders();
066 }close braces end code blocks and must match an earlier open brace
067
068 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value makeTexts()
069 {open braces start code blocks and must be matched with a close brace
070 restart=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor StringSprite("Click to restart game.");
071 restart.setLocation(0.5, 0.4);
072 restart.setScale(.5);
073 restart.setColor(Color.BLACK);
074
075 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");
076 gameover.setLocation(0.5, 0.4);
077 gameover.setScale(.4);
078 gameover.setColor(Color.BLACK);
079 }close braces end code blocks and must match an earlier open brace
080
081 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value makeBorders()
082 {open braces start code blocks and must be matched with a close brace
083 makeFloor();
084 makeLeftBorder();
085 makeRightBorder();
086 makeTopBorder();
087 }close braces end code blocks and must match an earlier open brace
088
089 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value makeLeftBorder()
090 {open braces start code blocks and must be matched with a close brace
091 leftBorder=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor RectangleSprite(2, 40);
092 leftBorder.setLocation(0.015, 0.5);
093 leftBorder.setScale(1);
094 leftBorder.setColor(color);
095 canvas.addSprite(leftBorder);
096 }close braces end code blocks and must match an earlier open brace
097
098 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value makeRightBorder()
099 {open braces start code blocks and must be matched with a close brace
100 rightBorder=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor RectangleSprite(2, 40);
101 rightBorder.setLocation(.985, 0.5);
102 rightBorder.setScale(1);
103 rightBorder.setColor(color);
104 canvas.addSprite(rightBorder);
105 }close braces end code blocks and must match an earlier open brace
106
107 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value makeTopBorder()
108 {open braces start code blocks and must be matched with a close brace
109 topBorder=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor RectangleSprite(40, 1);
110 topBorder.setLocation(0.5, 0);
111 topBorder.setScale(1);
112 topBorder.setColor(color);
113 canvas.addSprite(topBorder);
114 canvas.addSprite(goal2);
115 }close braces end code blocks and must match an earlier open brace
116
117 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value makeFloor()
118 {open braces start code blocks and must be matched with a close brace
119 floor=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor RectangleSprite(40, 1);
120 floor.setLocation(0.5, 1);
121 floor.setScale(1);
122 floor.setColor(color);
123 canvas.addSprite(floor);
124 canvas.addSprite(goal);
125 }close braces end code blocks and must match an earlier open brace
126
127 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value makeBallandGoal()
128 {open braces start code blocks and must be matched with a close brace
129 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);
130 ball.setColor(Color.BLACK);
131 ball.setScale(.075);
132 ball.setLocation(.5, .5);
133 canvas.addSprite(ball);
134
135 goal=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor RectangleSprite(10, 1);
136 goal.setColor(Color.RED);
137 goal.setScale(.3);
138 goal.setLocation(.5, .999);
139
140 goal2=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor RectangleSprite(10, 1);
141 goal2.setColor(Color.BLUE);
142 goal2.setScale(.3);
143 goal2.setLocation(.5, .001);
144
145 }close braces end code blocks and must match an earlier open brace
146
147 privateprivate is used to restrict access to the current class only 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 sidenewnew is used to create objects by calling the constructor OvalSprite(4, 2);
150 paddle.setColor(Color.RED);
151 paddle.setScale(.1);
152 paddle.setLocation(.5, .9);
153 canvas.addSprite(paddle);
154
155 paddle2=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor OvalSprite(4, 2);
156 paddle2.setColor(Color.BLUE);
157 paddle2.setScale(.1);
158 paddle2.setLocation(.5, .1);
159 canvas.addSprite(paddle2);
160 }close braces end code blocks and must match an earlier open brace
161
162 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value makeTracker()
163 {open braces start code blocks and must be matched with a close brace
164 tracker=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor ProjectileTracker(0.4, 0.4);
165 ball.setTracker(tracker);
166 }close braces end code blocks and must match an earlier open brace
167
168 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value makeScores()
169 {open braces start code blocks and must be matched with a close brace
170 scoreBottomText =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor StringSprite("Score: " +adds two numbers together or concatenates Strings together scoreBottom);
171 scoreBottomText.setLocation(0.86, .95);
172 scoreBottomText.setColor(Color.BLACK);
173 scoreBottomText.setScale(0.175);
174 canvas.addSprite(scoreBottomText);
175
176 scoreTopText =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor StringSprite("Score: "+adds two numbers together or concatenates Strings togetherscoreTop);
177 scoreTopText.setLocation(0.14, .05);
178 scoreTopText.setColor(Color.BLACK);
179 scoreTopText.setScale(0.175);
180 canvas.addSprite(scoreTopText);
181 }close braces end code blocks and must match an earlier open brace
182
183
184 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)
185 {open braces start code blocks and must be matched with a close brace
186 ifif executes the next statement only if the condition in parenthesis evaluates to true(tracker!=this is the not equals operator which evaluates to true if both sides are differentnullnull is the value used to refer to a non-existant object)
187 {open braces start code blocks and must be matched with a close brace
188 doubledouble is the type for numbers that can contain decimal fractions x=this assignment operator makes the left side equal to the right sidegetPlayer(0).getMouse().getLocation().x;
189 doubledouble is the type for numbers that can contain decimal fractions y=this assignment operator makes the left side equal to the right sidegetPlayer(0).getMouse().getLocation().y;
190 ifif executes the next statement only if the condition in parenthesis evaluates to true(y<.7)
191 {open braces start code blocks and must be matched with a close brace
192 y=this assignment operator makes the left side equal to the right side.7;
193 }close braces end code blocks and must match an earlier open brace
194 ifif executes the next statement only if the condition in parenthesis evaluates to true(y>.9)
195 {open braces start code blocks and must be matched with a close brace
196 y=this assignment operator makes the left side equal to the right side.9;
197 }close braces end code blocks and must match an earlier open brace
198 paddle.setLocation(x,y);
199
200 doubledouble is the type for numbers that can contain decimal fractions a=this assignment operator makes the left side equal to the right sidegetPlayer(1).getMouse().getLocation().x;
201 doubledouble is the type for numbers that can contain decimal fractions b=this assignment operator makes the left side equal to the right sidegetPlayer(1).getMouse().getLocation().y;
202 ifif executes the next statement only if the condition in parenthesis evaluates to true(b>.3)
203 {open braces start code blocks and must be matched with a close brace
204 b=this assignment operator makes the left side equal to the right side.3;
205 }close braces end code blocks and must match an earlier open brace
206 ifif executes the next statement only if the condition in parenthesis evaluates to true(b<.1)
207 {open braces start code blocks and must be matched with a close brace
208 b=this assignment operator makes the left side equal to the right side.1;
209 }close braces end code blocks and must match an earlier open brace
210 paddle2.setLocation(a,b);
211
212 bounceOffWalls();
213 clickCommand();
214 playerDies();
215 makeHandicaps();
216 }close braces end code blocks and must match an earlier open brace
217 }close braces end code blocks and must match an earlier open brace
218
219 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value clickCommand()
220 {open braces start code blocks and must be matched with a close brace
221 ifif executes the next statement only if the condition in parenthesis evaluates to true(getPlayer(playernum).getMouse().getClickLocation()!=this is the not equals operator which evaluates to true if both sides are differentnullnull 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 &&) dead ==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)
222 {open braces start code blocks and must be matched with a close brace
223 dead =this assignment operator makes the left side equal to the right side falsefalse is a value for the boolean type and means not true;
224 resetGame();
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
228 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value playerDies()
229 {open braces start code blocks and must be matched with a close brace
230 ifif executes the next statement only if the condition in parenthesis evaluates to true(goal.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 &&) ball.isVisible())
231 {open braces start code blocks and must be matched with a close brace
232 ifif executes the next statement only if the condition in parenthesis evaluates to true(scoreTop<10)
233 {open braces start code blocks and must be matched with a close brace
234 dead=this assignment operator makes the left side equal to the right sidetruetrue is the boolean value that is the opposite of false;
235 scoreTop++this is the increment operator, which increases the variable by 1;
236 ball.setLocation(0,0);
237 scoreTopText.setText("Score: " +adds two numbers together or concatenates Strings together scoreTop);
238 canvas.removeSprite(ball, paddle, paddle2);
239 ball.setTracker(nullnull is the value used to refer to a non-existant object);
240 canvas.addSprite(restart);
241 playernum=this assignment operator makes the left side equal to the right side0;
242 }close braces end code blocks and must match an earlier open brace
243
244 elseelse is what happens when the if condition is false
245 {open braces start code blocks and must be matched with a close brace
246 canvas.addSprite(gameover);
247 canvas.removeSprite(ball, paddle, paddle2);
248 ball.setTracker(nullnull is the value used to refer to a non-existant object);
249 }close braces end code blocks and must match an earlier open brace
250 }close braces end code blocks and must match an earlier open brace
251 elseelse is what happens when the if condition is false ifif executes the next statement only if the condition in parenthesis evaluates to true(goal2.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 &&) ball.isVisible())
252 {open braces start code blocks and must be matched with a close brace
253 ifif executes the next statement only if the condition in parenthesis evaluates to true(scoreBottom<10)
254 {open braces start code blocks and must be matched with a close brace
255 dead=this assignment operator makes the left side equal to the right sidetruetrue is the boolean value that is the opposite of false;
256 scoreBottom++this is the increment operator, which increases the variable by 1;
257 ball.setLocation(0,0);
258 scoreBottomText.setText("Score: " +adds two numbers together or concatenates Strings together scoreBottom);
259 canvas.removeSprite(ball, paddle, paddle2);
260 ball.setTracker(nullnull is the value used to refer to a non-existant object);
261 canvas.addSprite(restart);
262 playernum=this assignment operator makes the left side equal to the right side1;
263 }close braces end code blocks and must match an earlier open brace
264 elseelse is what happens when the if condition is false
265 {open braces start code blocks and must be matched with a close brace
266 canvas.addSprite(gameover);
267 canvas.removeSprite(ball, paddle, paddle2);
268 ball.setTracker(nullnull is the value used to refer to a non-existant object);
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 }close braces end code blocks and must match an earlier open brace
272 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value resetGame()
273 {open braces start code blocks and must be matched with a close brace
274 canvas.removeSprite (paddle, ball, scoreTopText, scoreBottomText, restart);
275 trackScore=this assignment operator makes the left side equal to the right side0;
276 makeSprites();
277
278 }close braces end code blocks and must match an earlier open brace
279
280 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value bounceOffWalls()
281 {open braces start code blocks and must be matched with a close brace
282 bounceOffPaddle();
283 bounceOffRight();
284 bounceOffLeft();
285 bounceOffTop();
286 bounceOffFloor();
287 }close braces end code blocks and must match an earlier open brace
288
289 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value bounceOffPaddle()
290 {open braces start code blocks and must be matched with a close brace
291 ifif executes the next statement only if the condition in parenthesis evaluates to true(paddle.intersects(ball))
292 {open braces start code blocks and must be matched with a close brace
293 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());
294 tracker.bounce(normal);
295 }close braces end code blocks and must match an earlier open brace
296
297 ifif executes the next statement only if the condition in parenthesis evaluates to true(paddle2.intersects(ball))
298 {open braces start code blocks and must be matched with a close brace
299 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(paddle2.getShape(), ball.getShape());
300 tracker.bounce(normal);
301 }close braces end code blocks and must match an earlier open brace
302 }close braces end code blocks and must match an earlier open brace
303
304 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value bounceOffRight()
305 {open braces start code blocks and must be matched with a close brace
306 ifif executes the next statement only if the condition in parenthesis evaluates to true(rightBorder.intersects(ball))
307 {open braces start code blocks and must be matched with a close brace
308 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(rightBorder.getShape(), ball.getShape());
309 tracker.bounce(normal);
310 }close braces end code blocks and must match an earlier open brace
311 }close braces end code blocks and must match an earlier open brace
312
313 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value bounceOffLeft()
314 {open braces start code blocks and must be matched with a close brace
315 ifif executes the next statement only if the condition in parenthesis evaluates to true(leftBorder.intersects(ball))
316 {open braces start code blocks and must be matched with a close brace
317 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(leftBorder.getShape(), ball.getShape());
318 tracker.bounce(normal);
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
322 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value bounceOffTop()
323 {open braces start code blocks and must be matched with a close brace
324 ifif executes the next statement only if the condition in parenthesis evaluates to true(topBorder.intersects(ball))
325 {open braces start code blocks and must be matched with a close brace
326 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(topBorder.getShape(), ball.getShape());
327 tracker.bounce(normal);
328 }close braces end code blocks and must match an earlier open brace
329 }close braces end code blocks and must match an earlier open brace
330
331 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value bounceOffFloor()
332 {open braces start code blocks and must be matched with a close brace
333 ifif executes the next statement only if the condition in parenthesis evaluates to true(floor.intersects(ball))
334 {open braces start code blocks and must be matched with a close brace
335 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(floor.getShape(), ball.getShape());
336 tracker.bounce(normal);
337 }close braces end code blocks and must match an earlier open brace
338 }close braces end code blocks and must match an earlier open brace
339 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value makeHandicaps()
340 {open braces start code blocks and must be matched with a close brace
341 ifif executes the next statement only if the condition in parenthesis evaluates to true(scoreBottom>scoreTop)
342 {open braces start code blocks and must be matched with a close brace
343 Point2D.Double handicap=this assignment operator makes the left side equal to the right sidetracker.getVelocity();
344 handicap.y+=this increases the variable on the left by the value on the right(.0005*(scoreBottom-scoreTop));
345 tracker.setVelocity(handicap);
346 }close braces end code blocks and must match an earlier open brace
347 ifif executes the next statement only if the condition in parenthesis evaluates to true(scoreBottom<scoreTop)
348 {open braces start code blocks and must be matched with a close brace
349 Point2D.Double handicap=this assignment operator makes the left side equal to the right sidetracker.getVelocity();
350 handicap.y-=this decreases the variable on the left by the value on the right(.0005*(scoreTop-scoreBottom));
351 tracker.setVelocity(handicap);
352 }close braces end code blocks and must match an earlier open brace
353 }close braces end code blocks and must match an earlier open brace
354 }close braces end code blocks and must match an earlier open brace
|