Blake/Breakout2

From ggc

Jump to: navigation, search

001 package Blake;
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 bErskine
011  * Enhancements
012  * 1. Level Added
013  * 2. Curved Walla
014  * 3. Pause between levels
015  * 4. Cheats
016  * 5. Two Balls
017  * Also got help from Hemendra and Jayson
018  */
019 public class Breakout2 extends GameLoop
020 {
021   /**a rectangle*/
022   private Sprite ceiling, leftWall, rightWall, floor;
023   private double startYCoord, startXCoord;
024   private Sprite paddle, ball, ball2;
025   private ProjectileTracker tracker;
026   private ProjectileTracker tracker2;
027   private Sprite[] bricks;
028   private int score, lives, numBricksAcross, level=1, timeLeft;
029   private StringSprite timerSprite, scoreSprite, livesSprite, gameOver;
030 
031   /**sets up the game*/
032   public void startGame()
033   {
034     timeLeft=10;
035     removeCursor();
036     makeSprites();
037     addSprites();
038     makeceiling();
039     makeLeftWall();
040     makeRightWall();
041     makePaddle();
042     makeBall();
043     makeBall2();
044     makeTracker();
045     makeTracker2();
046     makefloor();
047     makeBricks1();
048     score=0;
049     lives=5;
050     setHelpText ("<h1>Breakout</h1)" +
051                  "<br><br>Hit bricks with the ball to remove from screen.<br><br> Enjoy!");
052 
053   }
054   
055   class TimeUpdater implements Alarm
056   {
057      public void alarm()
058      {
059         timeLeft--;
060         updateTimer();
061         if(timeLeft>0)
062         {
063            scheduleRelative(this1);
064         }
065         else
066         {
067           makeBricks2();
068           
069         }
070      }
071   }
072 
073   /**makes the sprites*/
074   private void makeSprites()
075   {
076     scoreSprite=new StringSprite("Score: "+score);
077     scoreSprite=new StringSprite("Score: 700");
078     scoreSprite.setScale(0.2);
079     scoreSprite.rightJustify();
080     scoreSprite.setColor(Color.BLUE);
081     scoreSprite.setLocation(.89.85);
082     /*Adds score to the game.*/
083 
084     livesSprite=new StringSprite("Lives: "+lives);
085     livesSprite=new StringSprite("Lives: 5");
086     livesSprite.setScale(0.19);
087     livesSprite.setColor(Color.GREEN);
088     livesSprite.setLocation(.19.85);
089     /*Adds lives to the game.*/
090     
091     
092     
093 
094     gameOver=new StringSprite("YOU LOSE");
095     gameOver.setScale(0.3);
096     gameOver.setColor(Color.BLUE);
097     gameOver.setLocation(.5.5);
098     /*Lets player know that game is over.*/
099     
100        timerSprite=new StringSprite("Timer: "+timeLeft);
101        timerSprite.leftJustify();
102        timerSprite.topJustify();
103        timerSprite.setHeight(0.1);
104        timerSprite.setLocation(00);  
105      
106 
107   }
108 
109   private void makeBricks1()
110   {
111     int index=0;
112     bricks=new Sprite[25];
113     int numBricksHigh=5;
114     double startLocationY=.25;
115     double endLocationY=1-startLocationY;
116     double distanceHigh=endLocationY-startLocationY;
117     for(int j=0; j<numBricksHigh; j++)
118     {
119       double y=j*distanceHigh/(numBricksHigh-1)+startLocationY;
120 
121       int numBricksAcross=5;
122       double startLocationX=0.1;
123       double endLocationX=1-startLocationX;
124       double distanceAcross=endLocationX-startLocationX;
125       for(int i=0; i<numBricksAcross; i++)
126       {
127         double x=i*distanceAcross/(numBricksAcross-1)+startLocationX;
128         bricks[index]=new RectangleSprite(21);
129         bricks[index].setScale(0.055);
130         bricks[index].setLocation(x,y/3.5);
131         bricks[index].setColor(Color.BLUE);
132         canvas.addSprite(bricks[index]);
133         index++;
134         /*Makes the bricks to add to the game.*/
135       }
136     }
137   }
138   
139   private void makeBricks2()
140   {
141     int index=0;
142     bricks=new Sprite[80];
143     int numBricksHigh=8;
144     double startLocationY=.25;
145     double endLocationY=1-startLocationY;
146     double distanceHigh=endLocationY-startLocationY;
147     for(int j=0; j<numBricksHigh; j++)
148     {
149       double y=j*distanceHigh/(numBricksHigh-1)+startLocationY;
150 
151       int numBricksAcross=10;
152       double startLocationX=0.1;
153       double endLocationX=1-startLocationX;
154       double distanceAcross=endLocationX-startLocationX;
155       for(int i=0; i<numBricksAcross; i++)
156       {
157         double x=i*distanceAcross/(numBricksAcross-1)+startLocationX;
158         bricks[index]=new RectangleSprite(21);
159         bricks[index].setScale(0.025);
160         bricks[index].setLocation(x,y/3.5);
161         bricks[index].setColor(Color.BLUE);
162         canvas.addSprite(bricks[index]);
163         index++;
164         /*Makes the bricks to add to the game.*/
165       }
166     }
167   }
168   
169   
170   
171   public void makeceiling()
172   {
173     ceiling = new RectangleSprite(20.5);
174     ceiling.setLocation(.5.01);
175     ceiling.setColor(Color.GREEN);
176     canvas.addSprite(ceiling);
177     /*Makes ceiling to add to the game.*/
178   }
179 
180   public void makeLeftWall()
181   {
182     leftWall = new OvalSprite(110);
183     leftWall.setLocation(startXCoord / 4.5.5);
184     leftWall.setColor(Color.GREEN);
185     canvas.addSprite(leftWall);
186     /*Makes left wall to add to the game.*/
187   }
188 
189   public void makeRightWall()
190   {
191     rightWall = new OvalSprite(110);
192     rightWall.setLocation(- startXCoord / 4.5.5);
193     rightWall.setColor(Color.GREEN);
194     canvas.addSprite(rightWall);
195     /*Makes right wall to add to the game.*/
196   }
197 
198   public void makefloor()
199   {
200     floor = new RectangleSprite(20,.5);
201     floor.setLocation(.5.99);
202     floor.setColor(Color.RED);
203     canvas.addSprite(floor);
204     /*Makes floor of the game.*/
205   }
206 
207   public void makePaddle()
208   {
209     paddle = new OvalSprite(5.5);
210     paddle.setLocation(.5.8);
211     paddle.setScale(.25);
212     paddle.setColor(Color.BLACK);
213     canvas.addSprite(paddle);
214     /*Makes paddle to add to game.*/
215   }
216 
217   public void makeBall()
218   {
219     ball = new OvalSprite(11);
220     ball.setLocation(.5.5599);
221     ball.setScale(.02);
222     ball.setColor(Color.BLACK);
223     canvas.addSprite(ball);
224     /*Makes ball to add to game.*/
225   }
226   
227   public void makeBall2()
228   {
229     ball2 = new OvalSprite(11);
230     ball2.setLocation(.5.7799);
231     ball2.setScale(.02);
232     ball2.setColor(Color.BLACK);
233     
234     /*Makes ball to add to game.*/
235   }
236   
237 
238   public void randomColorBound()
239   {
240     ball.setColor(new Color(random.nextInt()));
241     ball2.setColor(new Color(random.nextInt()));
242     paddle.setColor(new Color(random.nextInt()));
243     rightWall.setColor(new Color(random.nextInt()));
244     leftWall.setColor(new Color(random.nextInt()));
245     ceiling.setColor(new Color(random.nextInt()));
246     /*Makes the ball and paddle random colors.*/
247   }
248 
249   private void makeTracker()
250   {
251     tracker= new ProjectileTracker(0.25,-.25);
252     ball.setTracker(tracker);
253     
254     /*Makes tracker for the ball.*/
255   }
256   
257   private void makeTracker2()
258   {
259     tracker2= new ProjectileTracker(0.25,-.25);
260     ball2.setTracker(tracker2);
261     
262     /*Makes tracker for the ball.*/
263   }
264 
265   private void wallBounce()
266   {
267     if(paddle.intersects(ball))
268     {
269       double normal=Sprite.getNormalVector(paddle.getShape(), ball.getShape());
270       tracker.bounce(normal);
271       /*Makes ball bounce off of paddle.*/
272     }
273 
274     if((rightWall.intersects(ball)))
275     {
276       double normal=Sprite.getNormalVector(rightWall.getShape(), ball.getShape());
277       tracker.bounce(normal);
278       /*Makes ball bounce off right wall.*/
279     }
280 
281     if((leftWall.intersects(ball)))
282     {
283       double normal=Sprite.getNormalVector(leftWall.getShape(), ball.getShape());
284       tracker.bounce(normal);
285       /*Makes ball bounce off left wall.*/
286     }
287 
288     if((ceiling.intersects(ball)))
289     {
290       double normal=Sprite.getNormalVector(ceiling.getShape(), ball.getShape());
291       tracker.bounce(normal);
292       /*ball bounce off ceiling.*/
293     }
294     
295   }
296   
297 
298   private void wallBounce2()
299   {
300   
301     if(paddle.intersects(ball2)) 
302     {
303       double normal=Sprite.getNormalVector(paddle.getShape(), ball2.getShape());
304       tracker2.bounce(normal);
305       /*Makes ball bounce off of paddle.*/
306     }
307 
308     if((rightWall.intersects(ball2))) 
309     {
310       double normal=Sprite.getNormalVector(rightWall.getShape(), ball2.getShape());
311       tracker2.bounce(normal);
312       /*Makes ball bounce off right wall.*/
313     }
314 
315     if((leftWall.intersects(ball2)))
316     {
317       double normal=Sprite.getNormalVector(leftWall.getShape(), ball2.getShape());
318       tracker2.bounce(normal);
319       /*Makes ball bounce off left wall.*/
320     }
321 
322     if((ceiling.intersects(ball2)))
323     {
324       double normal=Sprite.getNormalVector(ceiling.getShape(), ball2.getShape());
325       tracker2.bounce(normal);
326       /*ball bounce off ceiling.*/
327     }
328   }
329   
330   private void brickIntersects()
331   {
332     
333     for(int i=0; i<bricks.length; i++)
334     {
335 
336       if((bricks[i].isVisible() && bricks[i].intersects(ball&& canvas.containsSprite(bricks[i])))
337       {
338         score++;
339         score += 9;
340         if(level==1)
341         {
342           double normal=Sprite.getNormalVector(bricks[i].getShape(), ball.getShape());
343           tracker.bounce(normal);
344           bricks[i].setVisible(false);
345         }
346         else
347         {
348           double normal=Sprite.getNormalVector(bricks[i].getShape(), ball.getShape());
349           tracker.bounce(normal);
350           bricks[i].setVisible(false);
351           
352         }
353 
354       }
355     }
356     
357     
358   }  
359   
360   private void brickIntersects2()
361   {
362     
363     for(int i=0; i<bricks.length; i++)
364     {
365 
366       if((bricks[i].isVisible() && bricks[i].intersects(ball2&& canvas.containsSprite(bricks[i])))
367       {
368         score++;
369         score += 9;
370         if(level==2)
371         {
372           double normal=Sprite.getNormalVector(bricks[i].getShape(), ball2.getShape());
373           tracker2.bounce(normal);
374           bricks[i].setVisible(false);
375         }
376         else 
377         {
378           double normal=Sprite.getNormalVector(bricks[i].getShape(), ball2.getShape());
379           tracker2.bounce(normal);
380           bricks[i].setVisible(false);
381           
382         }
383 
384       }
385     }
386     
387     
388   }
389     
390   
391     
392   
393 
394   /**adds the Sprites to the screen*/
395   private void updateScore()
396   {
397     scoreSprite.setText("Score: "+score);
398   }
399   /*Adds the score.*/
400   private void updateLives()
401   {
402     livesSprite.setText("Lives: "+lives);
403   }
404   /*Adds the lives.*/
405   
406   private void updateTimer()
407   {
408      timerSprite.setText("Timer: "+timeLeft);
409   }
410 
411   private void addSprites()
412   {
413     canvas.addSprite(scoreSprite);
414     canvas.addSprite(livesSprite);
415     
416   }
417   /*Adds the score and lives to the game.*/
418 
419   /**handle input and game events*/
420 
421   private void resetGame()
422   {
423     if(lives==0)
424     {
425       canvas.addSprite(gameOver);
426     }
427   }
428 
429   public void bonus()
430   {
431     if(score==260 && level==1)
432     {
433       level=2;
434       scheduleRelative(new TimeUpdater(),0);
435       canvas.addSprite(ball2);
436     }
437     if(score==200)
438     {
439       lives=lives+1;
440       score=score+10;
441     }
442     if(score==400)
443     {
444       lives=lives+1;
445       score=score+10;
446     }
447     updateLives();
448     updateScore();
449     
450     
451     
452 
453   }
454   public void advanceFrame(double timePassed)
455   {
456     
457     double x=getPlayer().getMouse().getLocation().x;
458     paddle.setLocation(x,paddle.getLocation().y);
459     wallBounce();
460     wallBounce2();
461     brickIntersects();
462     brickIntersects2();
463     randomColorBound();        //Jayson Helped.
464     resetGame();
465     bonus();
466     /*Game Cheat.*/
467     if(getPlayer().getKeyboard().getLastKey()=='v')
468     {
469       score=260;
470       canvas.removeSprite(bricks);
471       
472     }
473     
474     if(getPlayer().getKeyboard().getLastKey()=='l')
475     {
476       lives=lives+1;
477     }
478     
479     if (lives == && floor.intersects(ball))
480     {
481       lives--;
482     }
483     if(floor.intersects(ball&& lives > 1)
484     {
485       lives--;
486       ball.setLocation(.5,.6);
487     }
488     updateLives();
489     
490   }
491   /*Subtract lives everytime the ball drops below the floor.*/
492 }


Download/View Blake/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