|
001 packagepackage is used to name the directory or folder a class is in Wilson;
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 * All about my game.
012 * @authorthis is the Javadoc tag for documenting who created the source code Wilson Green
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 Breakout extendsextends means to customize or extend the functionality of a class Game
015 {open braces start code blocks and must be matched with a close brace
016 /**Global variables
017 */
018 ArrayList<Sprite> topEnemies;
019 RectangleSprite heroPaddle =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor RectangleSprite(12,1);
020 OvalSprite ball;
021 ProjectileTransformer projectile;
022 StringSprite score, scoreInt, lives;
023 OutlineSprite boundary =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor OutlineSprite(newnew is used to create objects by calling the constructor RectangleSprite(2,2));
024 OutlineSprite bottomBoundary =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor OutlineSprite(newnew is used to create objects by calling the constructor LineSprite(0,1,1,1));
025 PolygonSprite leftTriangle =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor PolygonSprite(3);
026 PolygonSprite rightTriangle =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor PolygonSprite(3);
027 intint is the type for whole numbers and it is short for integer blocks;
028
029 /**runs the various methods that set up the game
030 */
031 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value setup()
032 {open braces start code blocks and must be matched with a close brace
033 setScore(0);
034 setLives(3);
035 enemySetup();
036 createPlayer();
037 makeAndAddBall();
038 handleCollision();
039 makeBoundary();
040 score();
041 lives();
042 }close braces end code blocks and must match an earlier open brace
043
044 /**Creates the paddle sprite that the player controls
045 */
046 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value createPlayer()
047 {open braces start code blocks and must be matched with a close brace
048 heroPaddle.setSize(.2);
049 heroPaddle.setLocation(.5, .9);
050 heroPaddle.setColor(getColor("WHITE"));
051 addSprite(heroPaddle);
052 }close braces end code blocks and must match an earlier open brace
053
054 /**Lets you move the heroPaddle to where the mouse is horizontally along the x-axis
055 */
056 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value movePaddle()
057 {open braces start code blocks and must be matched with a close brace
058 heroPaddle.setX(getMouseX());
059 }close braces end code blocks and must match an earlier open brace
060
061 /**Creates a stringSprite forfor is a looping structure for repeatedly executing a block of code lives and adds it
062 */
063 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value lives()
064 {open braces start code blocks and must be matched with a close brace
065 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 together getLives());
066 lives.setSize(0.2);
067 lives.setColor(getColor("white"));
068 lives.setLocation(0.3, .95);
069 lives.setVisible(truetrue is the boolean value that is the opposite of false);
070 addSprite(lives);
071 }close braces end code blocks and must match an earlier open brace
072
073 /**Updates the lives stringSprite
074 */
075 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value updateLives()
076 {open braces start code blocks and must be matched with a close brace
077 lives.setText("Lives: " +adds two numbers together or concatenates Strings together getLives());
078 }close braces end code blocks and must match an earlier open brace
079
080 /**Creates and initalizes a stringSprite forfor is a looping structure for repeatedly executing a block of code your score
081 */
082 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value score()
083 {open braces start code blocks and must be matched with a close brace
084 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 together getScore());
085 score.setSize(0.2);
086 score.setColor(getColor("WHITE"));
087 score.setLocation(.7, .95);
088 addSprite(score);
089 }close braces end code blocks and must match an earlier open brace
090
091 /**Updates your score
092 */
093 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value updateScore()
094 {open braces start code blocks and must be matched with a close brace
095 score.setText("Score: " +adds two numbers together or concatenates Strings together getScore());
096 }close braces end code blocks and must match an earlier open brace
097
098 /**This creates and adds a OvalSprite forfor is a looping structure for repeatedly executing a block of code the ball in the center of the game
099 */
100 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value makeAndAddBall()
101 {open braces start code blocks and must be matched with a close brace
102 ball=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor OvalSprite(0.1, 0.1);
103 ball.setLocation(0.5, 0.5);
104 ball.setColor(getColor("WHITE"));
105 ball.setSize(.05);
106 addSprite(ball);
107 projectile=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor ProjectileTransformer(0.2, 0.35);
108 ball.setTracker(projectile);
109 }close braces end code blocks and must match an earlier open brace
110
111 /**This method creates the outline boundary forfor is a looping structure for repeatedly executing a block of code the game, so the ball wont leave.
112 Also creates another boundary at the bottom forfor is a looping structure for repeatedly executing a block of code determining when you lose a life
113 And the two triangles in the bottom left and right corner are created here.
114 */
115 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value makeBoundary()
116 {open braces start code blocks and must be matched with a close brace
117 boundary.setLineThickness(0.01);
118 boundary.setLocation(0.5, 0.5);
119 boundary.setSize(1.0);
120 boundary.setColor(getColor("BLACK"));
121 addSprite(boundary);
122 bottomBoundary.setLineThickness(0.01);
123 bottomBoundary.setLocation(0.5, 0.99);
124 bottomBoundary.setSize(1.0);
125 bottomBoundary.setColor(getColor("BLACK"));
126 addSprite(bottomBoundary);
127 leftTriangle.setLocation(0, 1);
128 leftTriangle.setScale(.5);
129 leftTriangle.setColor(getColor("red"));
130 leftTriangle.setVisible(truetrue is the boolean value that is the opposite of false);
131 addSprite(leftTriangle);
132 rightTriangle.setLocation(1, 1);
133 rightTriangle.setScale(.5);
134 rightTriangle.setColor(getColor("red"));
135 rightTriangle.setVisible(truetrue is the boolean value that is the opposite of false);
136 addSprite(rightTriangle);
137 }close braces end code blocks and must match an earlier open brace
138
139 /**Sets up the enemy and uses nested forfor is a looping structure for repeatedly executing a block of code statements to create the enemies from an arraylist.
140 */
141 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value enemySetup()
142 {open braces start code blocks and must be matched with a close brace
143 topEnemies=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor ArrayList<Sprite>();
144 intint is the type for whole numbers and it is short for integer enemiesAcross=this assignment operator makes the left side equal to the right side10;
145 intint is the type for whole numbers and it is short for integer enemiesDown=this assignment operator makes the left side equal to the right side3;
146 doubledouble is the type for numbers that can contain decimal fractions enemyWidth=this assignment operator makes the left side equal to the right side1;
147 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<enemiesDown; j++this is the increment operator, which increases the variable by 1)
148 {open braces start code blocks and must be matched with a close brace
149 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 side1; i<=this evaluates to true if the left side is not more than the right sideenemiesAcross; i++this is the increment operator, which increases the variable by 1)
150 {open braces start code blocks and must be matched with a close brace
151 RectangleSprite enemy=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor RectangleSprite(1, 1);
152 enemy.setSize(0.9/enemiesAcross);
153 enemy.setColor(getColor("RED"));
154 doubledouble is the type for numbers that can contain decimal fractions x=this assignment operator makes the left side equal to the right side0.5/enemiesAcross+adds two numbers together or concatenates Strings together1.0/enemiesAcross*(i-1);
155 x=this assignment operator makes the left side equal to the right sidex*enemyWidth;
156 enemy.scale(enemyWidth);
157 doubledouble is the type for numbers that can contain decimal fractions y=this assignment operator makes the left side equal to the right side0.5/enemiesAcross+adds two numbers together or concatenates Strings together(j+adds two numbers together or concatenates Strings together0.0)/enemiesAcross;
158 y=this assignment operator makes the left side equal to the right sidey*enemyWidth;
159 enemy.setLocation(x, y);
160 addSprite(enemy);
161 topEnemies.add(enemy);
162 }close braces end code blocks and must match an earlier open brace
163 }close braces end code blocks and must match an earlier open brace
164 blocks =this assignment operator makes the left side equal to the right side topEnemies.size();
165 }close braces end code blocks and must match an earlier open brace
166
167 /**This sets up the string that shows ifif executes the next statement only if the condition in parenthesis evaluates to true you lose all of your lives.
168 */
169 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value gameOver()
170 {open braces start code blocks and must be matched with a close brace
171 StringSprite end =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!");
172 end.setSize(.3);
173 end.setLocation(.5, .5);
174 end.setColor(getColor("WHITE"));
175 ball.hide();
176 end.setVisible(truetrue is the boolean value that is the opposite of false);
177 addSprite(end);
178 stop();
179 }close braces end code blocks and must match an earlier open brace
180
181 /**This sets up the string that shows ifif executes the next statement only if the condition in parenthesis evaluates to true you destroy all the bricks.
182 */
183 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value win()
184 {open braces start code blocks and must be matched with a close brace
185 StringSprite winner =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!");
186 winner.setSize(.3);
187 winner.setLocation(.5, .5);
188 winner.setColor(getColor("WHITE"));
189 ball.hide();
190 winner.setVisible(truetrue is the boolean value that is the opposite of false);
191 addSprite(winner);
192 stop();
193 }close braces end code blocks and must match an earlier open brace
194
195 /**This method handles what happens when the ball touches the boundarys.
196 */
197 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value handleCollision()
198 {open braces start code blocks and must be matched with a close brace
199 forfor is a looping structure for repeatedly executing a block of code(Sprite single: topEnemies)
200 {open braces start code blocks and must be matched with a close brace
201 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 sametruetrue 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))
202 {open braces start code blocks and must be matched with a close brace
203 ball.bounceOffOf(single);
204 ifif executes the next statement only if the condition in parenthesis evaluates to true(getColorName(single.getColor()) ==this is the comparison operator which evaluates to true if both sides are the same "Green")
205 {open braces start code blocks and must be matched with a close brace
206 single.hide();
207 blocks--this is the decrement operator, which decreases the variable by 1;
208 }close braces end code blocks and must match an earlier open brace
209 elseelse is what happens when the if condition is false
210 single.setColor(getColor("green"));
211 intint is the type for whole numbers and it is short for integer original=this assignment operator makes the left side equal to the right sidegetScore();
212 setScore(original+adds two numbers together or concatenates Strings together10);
213 }close braces end code blocks and must match an earlier open brace
214 }close braces end code blocks and must match an earlier open brace
215 ifif executes the next statement only if the condition in parenthesis evaluates to true(ball.intersects(heroPaddle))
216 ball.bounceOffOf(heroPaddle);
217 ifif executes the next statement only if the condition in parenthesis evaluates to true(ball.intersects(leftTriangle))
218 ball.bounceOffOf(leftTriangle);
219 ifif executes the next statement only if the condition in parenthesis evaluates to true(ball.intersects(rightTriangle))
220 ball.bounceOffOf(rightTriangle);
221 ifif executes the next statement only if the condition in parenthesis evaluates to true(ball.intersects(boundary))
222 ball.bounceOffOf(boundary);
223 ifif executes the next statement only if the condition in parenthesis evaluates to true(ball.intersects(bottomBoundary))
224 {open braces start code blocks and must be matched with a close brace
225 ifif executes the next statement only if the condition in parenthesis evaluates to true(getLives() > 0)
226 {open braces start code blocks and must be matched with a close brace
227 setLives(getLives() - 1);
228 updateLives();
229 ball.setLocation(.5, .5);
230 }close braces end code blocks and must match an earlier open brace
231 elseelse is what happens when the if condition is false
232 gameOver();
233 }close braces end code blocks and must match an earlier open brace
234 }close braces end code blocks and must match an earlier open brace
235
236 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value checkBlocks()
237 {open braces start code blocks and must be matched with a close brace
238 ifif executes the next statement only if the condition in parenthesis evaluates to true (blocks ==this is the comparison operator which evaluates to true if both sides are the same 0)
239 win();
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 checkBlocks();
246 movePaddle();
247 handleCollision();
248 updateScore();
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
|