GBell/Breakout2

From ggc

Jump to: navigation, search

001 package GBell;
002 
003 import wiki.Wiki;
004 import fang.*;
005 import java.awt.*;
006 import java.awt.geom.*;
007 
008 /**
009  * Based on the well-known game "Breakout."
010  @author Gbell
011  */
012 public class Breakout2 extends GameLoop
013 {
014   private Sprite[] bricks;
015   private Sprite leftBorder, rightBorder, topBorder, floor;
016   private Sprite paddle, paddleBase;
017   private Sprite ball;
018   private ProjectileTracker tracker, tracker2;
019   private int lives, score, trackScore, bricksLeft, difficulty;
020   private StringSprite gameover, restart, livesText, scoreText, win, start;
021   private Color color = new Color(150150150);
022   private boolean dead = false;
023   private boolean gravity = false;
024   private Point2D.Double initVelocity;
025   private StringSprite easy, hard, directions;
026 
027   public void startGame()
028   {
029     directions=new StringSprite("Click to choose your difficulty.");
030     directions.setColor(Color.GREEN);
031     directions.setLocation(.5.2);
032     directions.setScale(.9);
033     canvas.addSprite(directions);
034 
035     easy=new StringSprite("Easy");
036     easy.setLocation(.25.5);
037     easy.setScale(.3);
038     easy.setColor(Color.BLUE);
039     canvas.addSprite(easy);
040 
041     hard=new StringSprite("Hard");
042     hard.setColor(Color.RED);
043     hard.setLocation(.75.5);
044     hard.setScale(.3);
045     canvas.addSprite(hard);
046 
047     toggleAudible();
048     pauseToggle();
049     difficulty=-1;
050   }
051   public void lead()
052   {
053     gravity=false;
054   }
055   public void advanceFrame(double timePassed)
056   {
057     Point2D.Double click=getPlayer().getMouse().getClickLocation();
058     if(click!=null && easy.intersects(click))
059     {
060       difficulty=0;
061       lives=5;
062       canvas.removeAllSprites();
063       beginGame();
064     }
065 
066     if(click!=null && hard.intersects(click))
067     {
068       difficulty=1;
069       lives=3;
070       canvas.removeAllSprites();
071       beginGame();
072     }
073     if(difficulty==1)
074     {
075       if(tracker!=null)
076       {
077         if(trackScore>4)
078         {
079           paddle.setColor(Color.BLUE);
080           ball.setColor(Color.BLUE);
081           gravity=true;
082         }
083 
084 
085         if(trackScore>12 && gravity==true)
086         {
087           paddle.setScale(.11);
088           paddleBase.setScale(.11);
089           paddleBase.setLocation(.5.96);
090           paddle.setColor(Color.WHITE);
091           ball.setColor(Color.WHITE);
092           gravity=false;
093         }
094         if(trackScore>20 && gravity==false)
095         {
096           gravity=true;
097         }
098 
099         if(gravity==true)
100         {
101           Point2D.Double velocity=tracker.getVelocity();
102           velocity.y+=(.65*timePassed);
103           if(velocity.y>1)
104           {
105             velocity.y=1;
106           }
107           tracker.setVelocity(velocity);
108         }
109       }
110     }
111     if(difficulty!=-1)
112     {
113       double x=getPlayer().getMouse().getLocation().x;
114       paddle.setLocation(x,paddle.getLocation().y);
115       paddleBase.setLocation(x,paddleBase.getLocation().y);
116 
117       bounceOffWalls();
118       hitsBricks();
119       clickCommand();
120       playerDies();
121     }
122   }
123 
124   public void beginGame()
125   {
126     score=0;
127     bricksLeft=50;
128     trackScore=0;
129     bricks=new Sprite[50];
130     makeSprites();
131     makeBricks();
132   }
133 
134   private void makeSprites()
135   {
136     lead();
137     makeBall();
138     makePaddle();
139     makeTracker();
140     makeLivesAndScore();
141     makeBorders();
142   }
143   public void makeBricks()
144   {
145     int index=0;
146     int numBricksHigh=5;
147     double startLocationY=0.1;
148     double endLocationY=.3;
149     double distanceHigh=endLocationY-startLocationY;
150     for(int j=0; j<numBricksHigh; j++)
151     {
152       double y=j*distanceHigh/(numBricksHigh-1)+startLocationY;
153 
154       int numBricksAcross=10;
155       double startLocationX=0.1;
156       double endLocationX=1-startLocationX;
157       double distanceAcross=endLocationX-startLocationX;
158 
159       for(int i=0; i<numBricksAcross; i++)
160       {
161         double x=i*distanceAcross/(numBricksAcross-1)+startLocationX;
162         bricks[index]=new RectangleSprite(2.41.25);
163         bricks[index].setScale(.085);
164         bricks[index].setLocation(x, y);
165         bricks[index].setColor(Color.RED);
166         canvas.addSprite(bricks[index]);
167         index++;
168       }
169     }
170   }
171 
172   public void makeBorders()
173   {
174     makeFloor();
175     makeLeftBorder();
176     makeRightBorder();
177     makeTopBorder();
178   }
179 
180   public void makeLeftBorder()
181   {
182     leftBorder=new RectangleSprite(240);
183     leftBorder.setLocation(0.0150.5);
184     leftBorder.setScale(1);
185     leftBorder.setColor(color);
186     canvas.addSprite(leftBorder);
187   }
188 
189   public void makeRightBorder()
190   {
191     rightBorder=new RectangleSprite(240);
192     rightBorder.setLocation(.9850.5);
193     rightBorder.setScale(1);
194     rightBorder.setColor(color);
195     canvas.addSprite(rightBorder);
196   }
197 
198   public void makeTopBorder()
199   {
200     topBorder=new RectangleSprite(401);
201     topBorder.setLocation(0.50);
202     topBorder.setScale(1);
203     topBorder.setColor(color);
204     canvas.addSprite(topBorder);
205   }
206 
207   public void makeFloor()
208   {
209     floor=new RectangleSprite(401);
210     floor.setLocation(0.5.96);
211     floor.setScale(1);
212     floor.setColor(Color.BLACK);
213     canvas.addSprite(floor);
214   }
215 
216   private void makeBall()
217   {
218     ball=new OvalSprite(11);
219     ball.setColor(Color.GREEN);
220     ball.setScale(.04);
221     ball.setLocation(.5.8);
222     canvas.addSprite(ball);
223   }
224 
225   private void makePaddle()
226   {
227     paddle=new RectangleSprite(152);
228     paddle.setColor(Color.GREEN);
229     paddle.setScale(.2);
230     paddle.setLocation(.5.9);
231 
232 
233     paddleBase=new RectangleSprite(1515);
234     paddleBase.setColor(Color.BLACK);
235     paddleBase.setScale(.2);
236     paddleBase.setLocation(.5.99);
237     canvas.addSprite(paddleBase);
238     canvas.addSprite(paddle);
239   }
240 
241   private void makeTracker()
242   {
243     tracker=new ProjectileTracker(0.25, -.25);
244     ball.setTracker(tracker);
245     initVelocity=tracker.getVelocity();
246   }
247 
248   private void makeLivesAndScore()
249   {
250     livesText = new StringSprite("Lives: " + lives);
251     livesText.setLocation(0.86.05);
252     livesText.setColor(Color.WHITE);
253     livesText.setScale(0.16);
254     canvas.addSprite(livesText);
255 
256     scoreText = new StringSprite("Score: "+score);
257     scoreText.setLocation(0.14.05);
258     scoreText.setColor(Color.WHITE);
259     scoreText.setScale(0.175);
260     canvas.addSprite(scoreText);
261   }
262 
263   public void clickCommand()
264   {
265     if(getPlayer().getMouse().getClickLocation()!=null && dead == true)
266     {
267       dead = false;
268       resetGame();
269     }
270   }
271 
272   public void playerDies()
273   {
274     if(floor.intersects(ball&& ball.isVisible())
275     {
276       if(lives>0)
277       {
278         dead=true;
279         lives--;
280         ball.setLocation(0,0);
281         livesText.setText("Lives: " + lives);
282         canvas.removeSprite(ball, paddle);
283         ball.setTracker(null);
284         restart=new StringSprite("Click to restart game.");
285         restart.setLocation(0.50.5);
286         restart.setScale(.5);
287         restart.setColor(Color.WHITE);
288         canvas.addSprite(restart);
289         gravity=false;
290         tracker.setVelocity(initVelocity);
291       }
292 
293       else
294       {
295         gameover=new StringSprite("Game Over");
296         gameover.setLocation(0.50.5);
297         gameover.setScale(.4);
298         gameover.setColor(Color.WHITE);
299         canvas.addSprite(gameover);
300         canvas.removeSprite(ball, paddle);
301         ball.setTracker(null);
302         startGame();
303       }
304     }
305   }
306 
307   public void resetGame()
308   {
309     canvas.removeSprite (paddle, ball, scoreText, livesText, restart);
310     trackScore=0;
311     makeSprites();
312 
313   }
314 
315   private void bounceOffWalls()
316   {
317     bounceOffPaddle();
318     bounceOffRight();
319     bounceOffLeft();
320     bounceOffTop();
321   }
322 
323   public void bounceOffPaddle()
324   {
325     if(paddle.intersects(ball))
326     {
327       double normal=Sprite.getNormalVector(paddle.getShape(), ball.getShape());
328       tracker.bounce(normal);
329     }
330     if(paddleBase.intersects(ball))
331     {
332       double normal=Sprite.getNormalVector(paddleBase.getShape(), ball.getShape());
333       tracker.bounce(normal);
334     }
335   }
336 
337   public void bounceOffRight()
338   {
339     if(rightBorder.intersects(ball))
340     {
341       double normal=Sprite.getNormalVector(rightBorder.getShape(), ball.getShape());
342       tracker.bounce(normal);
343     }
344   }
345 
346   public void bounceOffLeft()
347   {
348     if(leftBorder.intersects(ball))
349     {
350       double normal=Sprite.getNormalVector(leftBorder.getShape(), ball.getShape());
351       tracker.bounce(normal);
352     }
353   }
354 
355   public void bounceOffTop()
356   {
357     if(topBorder.intersects(ball))
358     {
359       double normal=Sprite.getNormalVector(topBorder.getShape(), ball.getShape());
360       tracker.bounce(normal);
361     }
362   }
363 
364   private void hitsBricks()
365   {
366     for(int i=0; i<bricks.length; i++)
367     {
368       if(bricks[i].isVisible() && bricks[i].intersects(ball))
369       {
370         double normal=Sprite.getNormalVector(bricks[i].getShape(), ball.getShape());
371         score++;
372         bricksLeft--;
373         scoreText.setText("Score: "+score);
374         trackScore++;
375         tracker.bounce(normal);
376         bricks[i].setVisible(false);
377         if(bricksLeft==0)
378         {
379           canvas.removeAllSprites();
380           win=new StringSprite("Congratulations\nYou Win!");
381           win.setLocation(0.50.5);
382           win.setScale(.4);
383           win.setColor(Color.WHITE);
384           canvas.addSprite(win);
385 
386         }
387       }
388     }
389   }
390 }


Download/View GBell/Breakout2.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