|
001 packagepackage is used to name the directory or folder a class is in Schwarz;
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 * This is the classic game breakbreak terminates the loop immediately out of wich the objective is to get rid of all the briks.
012 * The first time a brick is hit, it changes color from blue to red. When a red is hit, the brick disappears
013 * and the score is increased. If the user does not hit the ball with the
014 * paddle before it hits the botem of the screen, a life is lost. You begin with three lives.
015 * Looked at Anam's code to get my items to spin. Also used some of the code from space invaders example code to start off.
016 *
017 * @authorthis is the Javadoc tag for documenting who created the source code Jacob Schwarz
018 */
019
020 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 Assignment7 extendsextends means to customize or extend the functionality of a class Game
021 {open braces start code blocks and must be matched with a close brace
022 privateprivate is used to restrict access to the current class only ArrayList<Sprite> bricks;
023 privateprivate is used to restrict access to the current class only RectangleSprite brick;
024 privateprivate is used to restrict access to the current class only OvalSprite ball, leftwall, rightwall;
025 privateprivate is used to restrict access to the current class only OutlineSprite boundary;
026 privateprivate is used to restrict access to the current class only LineSprite paddle;
027 privateprivate is used to restrict access to the current class only StringSprite score;
028 privateprivate is used to restrict access to the current class only StringSprite lives;
029 privateprivate is used to restrict access to the current class only ProjectileTransformer ballTransformer, brickTransformer;
030 privateprivate is used to restrict access to the current class only StringSprite gameover, win;
031 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer rows =this assignment operator makes the left side equal to the right side 4;
032 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer columns =this assignment operator makes the left side equal to the right side 10;
033 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer startlives;
034
035 /**creates the bricks to be broken*/
036 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value totalBricks()
037 {open braces start code blocks and must be matched with a close brace
038 doubledouble is the type for numbers that can contain decimal fractions brickwidth =this assignment operator makes the left side equal to the right side 0.1;
039 bricks =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor ArrayList<Sprite>();
040 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 r=this assignment operator makes the left side equal to the right side0; r<rows; r++this is the increment operator, which increases the variable by 1)
041 {open braces start code blocks and must be matched with a close brace
042 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 c=this assignment operator makes the left side equal to the right side0; c<columns; c++this is the increment operator, which increases the variable by 1)
043 {open braces start code blocks and must be matched with a close brace
044 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);
045 brick.setSize(.6/columns);
046 brick.setColor(getColor("blue"));
047 doubledouble is the type for numbers that can contain decimal fractions x =this assignment operator makes the left side equal to the right side .18+adds two numbers together or concatenates Strings togetherc*0.7/columns;
048 doubledouble is the type for numbers that can contain decimal fractions y =this assignment operator makes the left side equal to the right side .15+adds two numbers together or concatenates Strings togetherr*0.3/rows;
049 brick.setLocation(x, y);
050
051 Spinner spin=this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor Spinner(0);
052 spin.setRotationDegrees(30);
053 brick.addTransformer(spin);
054 addSprite(brick);
055 bricks.add(brick);
056 }close braces end code blocks and must match an earlier open brace
057 }close braces end code blocks and must match an earlier open brace
058 }close braces end code blocks and must match an earlier open brace
059
060 /**creates the paddle that the ball bounses off of*/
061 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value makePaddle()
062 {open braces start code blocks and must be matched with a close brace
063 paddle=this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor LineSprite(.4, .9, .6, .9);
064 paddle.setLocation(.5, .9);
065 paddle.setLineThickness(.06);
066 paddle.setColor(getColor("white"));
067
068 Spinner spinning=this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor Spinner(0);
069 spinning.setRotationDegrees(50);
070 paddle.addTransformer(spinning);
071 addSprite(paddle);
072
073 }close braces end code blocks and must match an earlier open brace
074
075 /**creates the ball that is used to destroy the brcks*/
076 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value makeBall(doubledouble is the type for numbers that can contain decimal fractions x, doubledouble is the type for numbers that can contain decimal fractions y)
077 {open braces start code blocks and must be matched with a close brace
078 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);
079 ball.setLocation(.5, .7);
080 ball.setColor(getColor("yellow"));
081 ball.setSize(0.07);
082 addSprite(ball);
083 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);
084 ball.setTracker(ballTransformer);
085 ball.setBlurLength(3);
086
087 }close braces end code blocks and must match an earlier open brace
088
089 /**alows the paddle to move with the mouse*/
090 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value movePaddle()
091 {open braces start code blocks and must be matched with a close brace
092 paddle.setX(getMouseX());
093 }close braces end code blocks and must match an earlier open brace
094
095 /**adds the score to the game board*/
096 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value score()
097 {open braces start code blocks and must be matched with a close brace
098 setScore(0);
099 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());
100 score.setLocation(0.95, 0.05);
101 score.setSize(0.25);
102 score.setColor(getColor("red"));
103 score.rightJustify();
104 addSprite(score);
105 }close braces end code blocks and must match an earlier open brace
106
107 /**adds the number of lives the to the board*/
108 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value lives()
109 {open braces start code blocks and must be matched with a close brace
110 setLives(3);
111 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());
112 lives.setLocation(0.05, 0.05);
113 lives.setSize(0.25);
114 lives.setColor(getColor("white"));
115 lives.leftJustify();
116 addSprite(lives);
117 }close braces end code blocks and must match an earlier open brace
118
119 /**creates the boundry that the ball will be able to bounse off of*/
120 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value makeBoundary()
121 {open braces start code blocks and must be matched with a close brace
122 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));
123 boundary.setLineThickness(0.05);
124 boundary.setLocation(0.5, 0.6);
125 boundary.setSize(1.0);
126 boundary.setColor(getColor("orange"));
127 addSprite(boundary);
128 }close braces end code blocks and must match an earlier open brace
129
130 /**adds some bumpers to the game to make it harder to play*/
131 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value addBumpers()
132 {open braces start code blocks and must be matched with a close brace
133 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, 1);
134 leftwall.setColor(getColor("white"));
135 leftwall.setSize(.25);
136 leftwall.setLocation(.01,.5);
137 addSprite(leftwall);
138
139 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, 1);
140 rightwall.setColor(getColor("white"));
141 rightwall.setSize(.25);
142 rightwall.setLocation(0.99,.5);
143 addSprite(rightwall);
144 }close braces end code blocks and must match an earlier open brace
145
146 /**ifif executes the next statement only if the condition in parenthesis evaluates to true the player looses all lives thisthis means the current object (the implicit parameter) string will be displayed*/
147 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value gameOver()
148 {open braces start code blocks and must be matched with a close brace
149 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!!!");
150 gameover.setSize(.9);
151 gameover.setColor(getColor("green"));
152 gameover.setLocation(.5, .3);
153 removeAllSprites();
154 addSprite(gameover);
155 setLives(0);
156 pause();
157 }close braces end code blocks and must match an earlier open brace
158
159 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value winGame()
160 {open braces start code blocks and must be matched with a close brace
161 win =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor StringSprite("YOU WIN!!!!");
162 win.setSize(.9);
163 win.setColor(getColor("Yellow"));
164 win.setLocation(.5, .3);
165 removeAllSprites();
166 addSprite(win);
167 }close braces end code blocks and must match an earlier open brace
168
169 /** checks to see ifif executes the next statement only if the condition in parenthesis evaluates to true the ball falls below the paddle and ifif executes the next statement only if the condition in parenthesis evaluates to true so is reset and a newnew is used to create objects by calling the constructor ball is placed*/
170 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value checkBallPosition()
171 {open braces start code blocks and must be matched with a close brace
172 ifif executes the next statement only if the condition in parenthesis evaluates to true(ball.getLocation().y>.93)
173 {open braces start code blocks and must be matched with a close brace
174 ball.setVisible(falsefalse is a value for the boolean type and means not true);
175 makeBall(.5,.5);
176 startlives=this assignment operator makes the left side equal to the right side getLives();
177 setLives(startlives-1);
178 lives.setText("Lives: "+adds two numbers together or concatenates Strings together getLives());
179 }close braces end code blocks and must match an earlier open brace
180 ifif executes the next statement only if the condition in parenthesis evaluates to true(getLives()<1)
181 {open braces start code blocks and must be matched with a close brace
182 gameOver();
183 }close braces end code blocks and must match an earlier open brace
184
185 ifif executes the next statement only if the condition in parenthesis evaluates to true(getScore()>=this evaluates to true if the left side is not less than the right side1000)
186 {open braces start code blocks and must be matched with a close brace
187 winGame();
188 }close braces end code blocks and must match an earlier open brace
189 }close braces end code blocks and must match an earlier open brace
190
191 /**registers when the ball hits a brick and cnages the color to red ifif executes the next statement only if the condition in parenthesis evaluates to true blue and makes the brick disapere ifif executes the next statement only if the condition in parenthesis evaluates to true the brick is hit when red*/
192 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value ballHitBrick()
193 {open braces start code blocks and must be matched with a close brace
194 forfor is a looping structure for repeatedly executing a block of code(Sprite single: bricks)
195 {open braces start code blocks and must be matched with a close brace
196 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))
197 {open braces start code blocks and must be matched with a close brace
198 ball.bounceOffOf(single);
199 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")
200 {open braces start code blocks and must be matched with a close brace
201 single.setVisible(falsefalse is a value for the boolean type and means not true);
202 intint is the type for whole numbers and it is short for integer startscore =this assignment operator makes the left side equal to the right side getScore();
203 setScore(startscore+adds two numbers together or concatenates Strings together25);
204 score.setText("Score:"+adds two numbers together or concatenates Strings togethergetScore());
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(getColorName(single.getColor())==this is the comparison operator which evaluates to true if both sides are the same"Blue")
207 single.setColor(getColor("Red"));
208 }close braces end code blocks and must match an earlier open brace
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 /**handles the collision of th ball with the other objects causing them to bounce*/
213 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value handleCollisions()
214 {open braces start code blocks and must be matched with a close brace
215 ball.bounceOffOf(boundary);
216 ball.bounceOffOf(paddle);
217 ball.bounceOffOf(leftwall);
218 ball.bounceOffOf(rightwall);
219
220 ballHitBrick();
221 }close braces end code blocks and must match an earlier open brace
222
223 /**sets up the game*/
224 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value setup()
225 {open braces start code blocks and must be matched with a close brace
226 totalBricks();
227 makePaddle();
228 makeBall(.5, .5);
229 score();
230 lives();
231 makeBoundary();
232 addBumpers();
233
234 String helpText=this assignment operator makes the left side equal to the right side
235 "Hello this page teaches you the rulls of the game.<br>"+adds two numbers together or concatenates Strings together
236 "The objective is to break all of the bricks without loosing all your lives.<br>"+adds two numbers together or concatenates Strings together
237 "To break the bricks you must make them go from blue to red by hitting them with the ball then hit them again when thier red to break them.<br>"+adds two numbers together or concatenates Strings together
238 "Press the button that says resume and select r to restart the game.";
239 setHelpText(helpText);
240 }close braces end code blocks and must match an earlier open brace
241
242 /**handle input and game events*/
243 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value advance()
244 {open braces start code blocks and must be matched with a close brace
245 ifif executes the next statement only if the condition in parenthesis evaluates to true(getLives()>0)
246 {open braces start code blocks and must be matched with a close brace
247 movePaddle();
248 handleCollisions();
249 checkBallPosition();
250 }close braces end code blocks and must match an earlier open brace
251
252 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')
253 {open braces start code blocks and must be matched with a close brace
254 removeAllSprites();
255 setup();
256 }close braces end code blocks and must match an earlier open brace
257
258 }close braces end code blocks and must match an earlier open brace
259 }close braces end code blocks and must match an earlier open brace
|