MattC/Breakout

From ggc

Jump to: navigation, search

001 package MattC;
002 
003 import wiki.Wiki;
004 import fang.*;
005 import java.awt.*;
006 import java.awt.geom.*;
007 
008 /**
009  * All about my game here.
010  @author Mcrauswe
011  */
012 public class Breakout extends GameLoop
013 {
014 
015   private Sprite[] bricks;
016   private Sprite ball, paddle;
017   private Sprite lBorder, rBorder, tBorder, bottomBorder;
018   private StringSprite livesCount, scoreBoard, gameover, winner, resetGame;
019   private ProjectileTracker tracker;
020   private int score, lives, trackScore;
021 
022   public void startGame()
023   {
024 
025     lives=3;
026     score=0;
027     makeBricks();
028     makeBall();
029     makePaddle();
030     makeTracker();
031     makeSprites();
032     toggleAudible();
033     pauseToggle();
034     setHelpText("Hit the ball with the paddle while trying to eliminate all bricks. You have three lives!");
035   }
036 
037   private void makeSprites()
038   {
039     makeWalls();
040 
041     gameover=new StringSprite("YOU LOST, HAHA!!");
042     gameover.setScale(0.6);
043     gameover.setColor(Color.RED);
044     gameover.setLocation(.5.5);
045 
046     winner=new StringSprite("YOU WON, GREAT JOB!!");
047     winner.setScale(0.6);
048     winner.setColor(Color.BLUE);
049     winner.setLocation(.5.5);
050 
051   }
052 
053   private void makeWalls()
054   {
055     lBorder=new RectangleSprite(240);
056     lBorder.setLocation(0.0150.5);
057     lBorder.setScale(1);
058     lBorder.setColor(Color.RED);
059     canvas.addSprite(lBorder);
060 
061     rBorder=new RectangleSprite(240);
062     rBorder.setLocation(.9850.5);
063     rBorder.setScale(1);
064     rBorder.setColor(Color.RED);
065     canvas.addSprite(rBorder);
066 
067     tBorder=new RectangleSprite(401);
068     tBorder.setLocation(0.50);
069     tBorder.setScale(1);
070     tBorder.setColor(Color.RED);
071     canvas.addSprite(tBorder);
072 
073     bottomBorder=new RectangleSprite(401);
074     bottomBorder.setLocation(0.51);
075     bottomBorder.setScale(1);
076     bottomBorder.setColor(Color.RED);
077     canvas.addSprite(bottomBorder);
078   }
079 
080   public void randomColorBound()
081   {
082     ball.setColor(new Color(random.nextInt()));
083     bottomBorder.setColor(new Color(random.nextInt()));
084     lBorder.setColor(new Color(random.nextInt()));
085     rBorder.setColor(new Color(random.nextInt()));
086     tBorder.setColor(new Color(random.nextInt()));
087   }
088 
089   /**Makes the bricks*/
090   public void makeBricks()
091   {
092     int index=0;
093     bricks=new Sprite[50];
094     int numBricksHigh=5;
095     double startLocationY=0.1;
096     double endLocationY=.3;
097     double distanceHigh=endLocationY-startLocationY;
098     for(int j=0; j<numBricksHigh; j++)
099     {
100       double y=j*distanceHigh/(numBricksHigh-1)+startLocationY;
101 
102       int numBricksAcross=10;
103       double startLocationX=0.1;
104       double endLocationX=1-startLocationX;
105       double distanceAcross=endLocationX-startLocationX;
106 
107       for(int i=0; i<numBricksAcross; i++)
108       {
109         double x=i*distanceAcross/(numBricksAcross-1)+startLocationX;
110         bricks[index]=new RectangleSprite(2.41.25);
111         bricks[index].setScale(.085);
112         bricks[index].setLocation(x, y);
113         bricks[index].setColor(Color.BLUE);
114         canvas.addSprite(bricks[index]);
115         index++;
116       }
117     }
118   }
119   /**Makes the ball*/
120   public void makeBall()
121   {
122     Ellipse2D.Double circle=new Ellipse2D.Double(0,0,1,1);
123     ball=new Sprite(circle);
124     ball.setLocation(.2,.6);
125     ball.setScale(.03);
126     ball.setColor(Color.RED);
127     canvas.addSprite(ball);
128   }
129   /**Makes the paddle*/
130   private void makePaddle()
131   {
132     paddle=new RectangleSprite(153);
133     paddle.setColor(Color.GREEN);
134     paddle.setScale(.12);
135     paddle.setLocation(.5.9);
136     canvas.addSprite(paddle);
137   }
138 
139   /**Makes lives visible on game*/
140   private StringSprite makeLives;
141   {
142     livesCount = new StringSprite("Lives: " +lives);
143     livesCount=new StringSprite("Lives: 3");
144     livesCount.setLocation(0.14.05);
145     livesCount.setColor(Color.WHITE);
146     livesCount.setScale(0.16);
147     canvas.addSprite(livesCount);
148   }
149   /**Makes score visible on game*/
150   private StringSprite makeScore;
151 
152   {
153     scoreBoard = new StringSprite("Score: "+score);
154     scoreBoard = new StringSprite("Score: 0");
155     scoreBoard.setLocation(0.86.05);
156     scoreBoard.setColor(Color.WHITE);
157     scoreBoard.setScale(0.175);
158     canvas.addSprite(scoreBoard);
159   }
160 
161   private boolean dead = false;
162 
163   /**Makes ball move*/
164   private void makeTracker()
165   {
166     tracker=new ProjectileTracker(0.25, -.25);
167     ball.setTracker(tracker);
168   }
169 
170   private void updateScore()
171   {
172     scoreBoard.setText("Score: "+score);
173   }
174   /*Adds the score.*/
175   private void updateLives()
176   {
177     livesCount.setText("Lives: "+lives);
178   }
179 
180   private void resetGame()
181   {
182     if(lives==0)
183     {
184       canvas.addSprite(gameover);
185     }
186     if (score==50)
187     {
188       canvas.addSprite(winner);
189     }
190 
191   }
192 
193   /**Changes speed of ball during gameplay*/
194   public void advanceFrame(double timePassed)
195   {
196     if(trackScore>2)
197     {
198       if (tracker != null)
199       {
200         Point2D.Double velocity=tracker.getVelocity();
201         velocity.y+=(.35*timePassed);
202         if(velocity.y>1)
203         {
204           velocity.y=1;
205         }
206         tracker.setVelocity(velocity);
207       }
208     }
209 
210     resetGame();
211     {
212       double x=getPlayer().getMouse().getLocation().x;
213       paddle.setLocation(x,paddle.getLocation().y);
214 
215       if (lives == && bottomBorder.intersects(ball))
216       {
217         lives--;
218       }
219       if(bottomBorder.intersects(ball&& lives > 1)
220       {
221         lives--;
222         ball.setLocation(.5,.5);
223       }
224       updateLives();
225     }
226 
227 
228     bounceOffWalls();
229     hitsBricks();
230     randomColorBound();
231 
232 
233   }
234 
235   /**Allows the ball to bounce of the walls and the paddle*/
236   private void bounceOffWalls()
237   {
238     bounceOffPaddle();
239     bounceOffRight();
240     bounceOffLeft();
241     bounceOffTop();
242   }
243 
244   public void bounceOffPaddle()
245   {
246     if(paddle.intersects(ball))
247     {
248       double normal=Sprite.getNormalVector(paddle.getShape(), ball.getShape());
249       tracker.bounce(normal);
250     }
251   }
252 
253   public void bounceOffRight()
254   {
255     if(rBorder.intersects(ball))
256     {
257       double normal=Sprite.getNormalVector(rBorder.getShape(), ball.getShape());
258       tracker.bounce(normal);
259     }
260   }
261 
262   public void bounceOffLeft()
263   {
264     if(lBorder.intersects(ball))
265     {
266       double normal=Sprite.getNormalVector(lBorder.getShape(), ball.getShape());
267       tracker.bounce(normal);
268     }
269   }
270 
271   public void bounceOffTop()
272   {
273     if(tBorder.intersects(ball))
274     {
275       double normal=Sprite.getNormalVector(tBorder.getShape(), ball.getShape());
276       tracker.bounce(normal);
277     }
278   }
279 
280 
281   /**Allows the ball to hit the bricks*/
282 
283 
284   //*Takes lives away and if you do too many will display message telling you have lost the game*/
285 
286 
287   private void hitsBricks()
288   {
289     for(int i=0; i<bricks.length; i++)
290     {
291       if(bricks[i].isVisible() && bricks[i].intersects(ball))
292       {
293         double normal=Sprite.getNormalVector(bricks[i].getShape(), ball.getShape());
294         score++;
295         scoreBoard.setText("Score: "+score);
296         tracker.bounce(normal);
297         bricks[i].setVisible(false);
298 
299       }
300     }
301 
302 
303   }
304 
305 
306 }


Download/View MattC/Breakout.java





Views
Personal tools
Add to 
del.icio.usAdd to 
diggAdd to 
FacebookAdd to 
favoritesAdd to 
GoogleAdd to 
MySpaceAdd to 
PrintAdd to 
SlashdotAdd to 
StumbleUponAdd to 
Twitter