HemendraPavitra/Breakout2

From ggc

Jump to: navigation, search

001 package HemendraPavitra;
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 Ppullay,Hpullay
011  * Enhancements:
012  *  1. Curved Walls
013  *  2. Timer: Adds 30 seconds at score equal at 40
014  *  3. Two balls: Added on the second level
015  *  4. Second Level: Two ball playing at the same time and a fifth row
016  *  5. Sound when ball hits bricks 
017 */
018 public class Breakout2 extends GameLoop
019 {
020   /**a rectangle*/
021   private Sprite rectangle, topCeiling, leftWall, leftWall1, rightWall,rightWall1, gameFloor;
022   private double startYCoord, startXCoord;
023   private Sprite paddle, ball, ball2;
024   private ProjectileTracker tracker;
025   private ProjectileTracker tracker1;
026   private int level;
027   private Sprite[] bricks;
028   private int score, lives, glevel=1;
029   private StringSprite scoreSprite, livesSprite, gameOver, youWin,txtBonusLives;
030   Sound tick=new Sound(Wiki.getMedia("Mouse1.wav"));
031   Sound boo=new Sound(Wiki.getMedia("Boo.wav"));
032   Sound applause=new Sound(Wiki.getMedia("Applause2.wav"));
033   private int timeLeft=200;
034   private StringSprite timerSprite;
035 
036   /**sets up the game*/
037   public void startGame()
038   {
039     removeCursor();
040     makeSprites();
041     addSprites();
042     makeBricks();
043     maketopCeiling();
044     makeLeftWall();
045     makeLeftWall1();
046     makeRightWall();
047     makeRightWall1();
048     makePaddle();
049     makeBall();
050     makeBall2();
051     makeTracker();
052     makeGameFloor();
053     addTime();
054     score=0;
055     lives=5;
056     scheduleRelative(new TimeUpdater()1);
057     setHelpText ("<h1>Breakout</h1)" +
058                  "<br><br>Hit bricks with the ball to remove from screen.<br><br> Enjoy!");
059 
060   }
061 
062   class TimeUpdater implements Alarm
063   {
064     public void alarm()
065     {
066       if(score==57)
067       {
068         timeLeft=timeLeft;
069       }
070       timeLeft--;
071       updateTimer();
072       if(timeLeft>0)
073       {
074         scheduleRelative(this1);
075       }
076       if(timeLeft==0)
077       {
078         canvas.addSprite(gameOver);
079       }
080     }
081   }
082 
083   /**makes the sprites*/
084   private void makeSprites()
085   {
086     scoreSprite=new StringSprite("Score: "+score);
087     scoreSprite=new StringSprite("Score: 0");
088     scoreSprite.setScale(0.2);
089     scoreSprite.rightJustify();
090     scoreSprite.setColor(Color.GREEN);
091     scoreSprite.setLocation(.9.95);
092     /*Adds score to the game.*/
093 
094     livesSprite=new StringSprite("Lives: "+lives);
095     livesSprite=new StringSprite("Lives: 5");
096     livesSprite.setScale(0.18);
097     livesSprite.setColor(Color.GREEN);
098     livesSprite.setLocation(.2.95);
099     /*Adds lives to the game.*/
100 
101     gameOver=new StringSprite("Game Over: You Lose!!");
102     gameOver.setScale(0.3);
103     gameOver.setColor(Color.RED);
104     gameOver.setLocation(.5.5);
105     /*Lets player know that game is over.*/
106 
107     youWin=new StringSprite("Winner!!");
108     youWin.setScale(0.3);
109     youWin.setColor(Color.RED);
110     youWin.setLocation(.5.5);
111     /*Lets player know that they won the game.*/
112 
113     txtBonusLives=new StringSprite("Bonus Lives & Points!!");
114     txtBonusLives.setScale(0.3);
115     txtBonusLives.setColor(Color.RED);
116     txtBonusLives.setLocation(.5.90);
117     /**Lets player know when they receive bonus lives and points*/
118 
119     timerSprite=new StringSprite("Timer: "+timeLeft);
120     timerSprite.setScale(.23);
121     timerSprite.setColor(Color.GREEN);
122     timerSprite.setLocation(.5.96);
123     /**Lets player know how much time they have left*/
124   }
125 
126   private void makeBricks()
127   {
128     int index=0;
129     bricks=new Sprite[56];
130     int numBricksHigh=4;
131     double startLocationY=.25;
132     double endLocationY=1-startLocationY;
133     double distanceHigh=endLocationY-startLocationY;
134     for(int j=0; j<numBricksHigh; j++)
135     {
136       double y=j*distanceHigh/(numBricksHigh-1)+startLocationY;
137 
138       int numBricksAcross=14;
139       double startLocationX=0.1;
140       double endLocationX=1-startLocationX;
141       double distanceAcross=endLocationX-startLocationX;
142       for(int i=0; i<numBricksAcross; i++)
143       {
144         double x=i*distanceAcross/(numBricksAcross-1)+startLocationX;
145         bricks[index]=new RectangleSprite(21);
146         bricks[index].setScale(0.055);
147         bricks[index].setLocation(x,y/3.5);
148         bricks[index].setColor(Color.RED);
149         canvas.addSprite(bricks[index]);
150         index++;
151         /*Makes the bricks to add to the game.*/
152       }
153     }
154   }
155 
156   private void makeBricks2()
157   {
158     int index=0;
159     bricks=new Sprite[70];
160     int numBricksHigh=5;
161     double startLocationY=.25;
162     double endLocationY=1-startLocationY;
163     double distanceHigh=endLocationY-startLocationY;
164     for(int j=0; j<numBricksHigh; j++)
165     {
166       double y=j*distanceHigh/(numBricksHigh-1)+startLocationY;
167 
168       int numBricksAcross=14;
169       double startLocationX=0.1;
170       double endLocationX=1-startLocationX;
171       double distanceAcross=endLocationX-startLocationX;
172       for(int i=0; i<numBricksAcross; i++)
173       {
174         double x=i*distanceAcross/(numBricksAcross-1)+startLocationX;
175         bricks[index]=new RectangleSprite(21);
176         bricks[index].setScale(0.055);
177         bricks[index].setLocation(x,y/3.5);
178         bricks[index].setColor(Color.BLUE);
179         canvas.addSprite(bricks[index]);
180         index++;
181         /*Makes the bricks to add to the game.*/
182       }
183     }
184   }
185 
186   public void maketopCeiling()
187   {
188     topCeiling = new OvalSprite(201);
189     topCeiling.setLocation(.5.001);
190     topCeiling.setColor(Color.RED);
191     canvas.addSprite(topCeiling);
192     /*Makes ceiling to add to the game.*/
193   }
194 
195   public void makeLeftWall()
196   {
197     leftWall = new OvalSprite(110);
198     leftWall.setLocation(startXCoord / 4.5.12);
199     leftWall.setColor(Color.RED);
200     canvas.addSprite(leftWall);
201     /*Makes left wall to add to the game.*/
202   }
203 
204   public void makeLeftWall1()
205   {
206     leftWall1 = new OvalSprite(110);
207     leftWall1.setLocation(startXCoord / 4.5.88);
208     leftWall1.setColor(Color.RED);
209     canvas.addSprite(leftWall1);
210     /*Makes second left wall to add to the game.*/
211   }
212 
213   public void makeRightWall()
214   {
215     rightWall = new OvalSprite(110);
216     rightWall.setLocation(- startXCoord / 4.5.12);
217     rightWall.setColor(Color.RED);
218     canvas.addSprite(rightWall);
219     /*Makes right wall to add to the game.*/
220   }
221 
222   public void makeRightWall1()
223   {
224     rightWall1 = new OvalSprite(110);
225     rightWall1.setLocation(- startXCoord / 4.5.88);
226     rightWall1.setColor(Color.RED);
227     canvas.addSprite(rightWall1);
228     /*Makes right wall to add to the game.*/
229   }
230 
231   public void makeGameFloor()
232   {
233     gameFloor = new RectangleSprite(20,.5);
234     gameFloor.setLocation(.5.99);
235     gameFloor.setColor(Color.BLACK);
236     canvas.addSprite(gameFloor);
237     /*Makes floor to add to the game.*/
238   }
239 
240   public void makePaddle()
241   {
242     paddle = new OvalSprite(5.5);
243     paddle.setLocation(.5.8);
244     paddle.setScale(.15);
245     paddle.setColor(Color.BLACK);
246     canvas.addSprite(paddle);
247     tick.play();
248     /*Makes paddle to add to game.*/
249   }
250 
251   public void makeBall()
252   {
253     ball = new OvalSprite(11);
254     ball.setLocation(.5.7799);
255     ball.setScale(.02);
256     ball.setColor(Color.BLACK);
257     canvas.addSprite(ball);
258     /*Makes ball to add to game.*/
259   }
260 
261   public void makeBall2()
262   {
263     ball2 = new OvalSprite(11);
264     ball2.setLocation(.5.7799);
265     ball2.setScale(.02);
266     ball2.setColor(Color.BLACK);
267     /*Makes ball to add to game.*/
268   }
269 
270   private void updateTimer()
271   {
272     timerSprite.setText("Timer: "+timeLeft);
273   }
274 
275   public void randomColorBound()
276   {
277     ball.setColor(new Color(random.nextInt()));
278     paddle.setColor(new Color(random.nextInt()));
279     ball2.setColor(new Color(random.nextInt()));
280     /*Makes the ball and paddle random colors.*/
281   }
282 
283   private void makeTracker()
284   {
285     tracker= new ProjectileTracker(0.30,-.30);
286     ball.setTracker(tracker);
287     tracker1= new ProjectileTracker(0.25,-.25);
288     ball2.setTracker(tracker1);
289     /*Makes tracker for the ball.*/
290   }
291 
292   private void wallBounce()
293   {
294     if(ball.intersects(ball2))
295     {
296       double normal=Sprite.getNormalVector(paddle.getShape(), ball.getShape());
297       tracker.bounce(normal);
298       tick.play();
299       /*Makes ball bounce off of paddle.*/
300     }
301     if(paddle.intersects(ball))
302     {
303       double normal=Sprite.getNormalVector(paddle.getShape(), ball.getShape());
304       tracker.bounce(normal);
305       tick.play();
306       /*Makes ball bounce off of paddle.*/
307     }
308 
309     if(rightWall.intersects(ball))
310     {
311       double normal=Sprite.getNormalVector(rightWall.getShape(), ball.getShape());
312       tracker.bounce(normal);
313       tick.play();
314       /*Makes ball bounce off right wall.*/
315     }
316 
317 
318     if(rightWall1.intersects(ball))
319     {
320       double normal=Sprite.getNormalVector(rightWall1.getShape(), ball.getShape());
321       tracker.bounce(normal);
322       tick.play();
323       /*Makes ball bounce off right wall.*/
324     }
325 
326     if(leftWall.intersects(ball))
327     {
328       double normal=Sprite.getNormalVector(leftWall.getShape(), ball.getShape());
329       tracker.bounce(normal);
330       tick.play();
331       /*Makes ball bounce off left wall.*/
332     }
333 
334     if(leftWall1.intersects(ball))
335     {
336       double normal=Sprite.getNormalVector(leftWall1.getShape(), ball.getShape());
337       tracker.bounce(normal);
338       tick.play();
339       /*Makes ball bounce off left wall.*/
340     }
341 
342     if(topCeiling.intersects(ball))
343     {
344       double normal=Sprite.getNormalVector(topCeiling.getShape(), ball.getShape());
345       tracker.bounce(normal);
346       tick.play();
347       /*Makes ball bounce off of top ceiling.*/
348     }
349   }
350   private void ballBounce2()
351   {
352     if(paddle.intersects(ball2))
353     {
354       double normal=Sprite.getNormalVector(paddle.getShape(), ball2.getShape());
355       tracker1.bounce(normal);
356       tick.play();
357       /*Makes ball bounce off of paddle.*/
358     }
359 
360     if(rightWall.intersects(ball2))
361     {
362       double normal=Sprite.getNormalVector(rightWall.getShape(), ball2.getShape());
363       tracker1.bounce(normal);
364       tick.play();
365       /*Makes ball bounce off right wall.*/
366     }
367 
368 
369     if(rightWall1.intersects(ball2))
370     {
371       double normal=Sprite.getNormalVector(rightWall1.getShape(), ball2.getShape());
372       tracker1.bounce(normal);
373       tick.play();
374       /*Makes ball bounce off right wall.*/
375     }
376 
377     if(leftWall.intersects(ball2))
378     {
379       double normal=Sprite.getNormalVector(leftWall.getShape(), ball2.getShape());
380       tracker1.bounce(normal);
381       tick.play();
382       /*Makes ball bounce off left wall.*/
383     }
384 
385     if(leftWall1.intersects(ball2))
386     {
387       double normal=Sprite.getNormalVector(leftWall1.getShape(), ball2.getShape());
388       tracker1.bounce(normal);
389       tick.play();
390       /*Makes ball bounce off left wall.*/
391     }
392 
393     if(topCeiling.intersects(ball2))
394     {
395       double normal=Sprite.getNormalVector(topCeiling.getShape(), ball2.getShape());
396       tracker1.bounce(normal);
397       tick.play();
398       /*Makes ball bounce off of top ceiling.*/
399     }
400   }
401 
402   private void brickIntersects() //help from Blake.
403   {
404     for(int i=0; i<bricks.length; i++)
405     {
406       if(bricks[i].isVisible() && bricks[i].intersects(ball&& canvas.containsSprite(bricks[i]))
407       {
408         score++;
409         tick.play();
410         if(level==0)
411         {
412           double normal=Sprite.getNormalVector(bricks[i].getShape(), ball.getShape());
413           tracker.bounce(normal);
414           bricks[i].setVisible(false);
415         }
416         else
417         {
418           double normal=Sprite.getNormalVector(bricks[i].getShape(), ball.getShape());
419           tracker.bounce(normal);
420           bricks[i].setVisible(false);
421         }
422       }
423     }
424     /*Increases score if ball intersects brick.*/
425     updateScore();
426   }
427 
428   private void brickIntersects2() //help from Blake.
429   {
430     for(int i=0; i<bricks.length; i++)
431     {
432       if(bricks[i].isVisible() && bricks[i].intersects(ball2&& canvas.containsSprite(bricks[i]))
433       {
434         score++;
435         tick.play();
436         if(level==0)
437         {
438           double normal=Sprite.getNormalVector(bricks[i].getShape(), ball2.getShape());
439           tracker1.bounce(normal);
440           bricks[i].setVisible(false);
441         }
442         else
443         {
444           double normal=Sprite.getNormalVector(bricks[i].getShape(), ball2.getShape());
445           tracker1.bounce(normal);
446           bricks[i].setVisible(false);
447         }
448       }
449     }
450     /*Increases score if ball intersects brick.*/
451     updateScore();
452   }
453 
454   /**adds the sprites to the screen*/
455   private void updateScore()
456   {
457     scoreSprite.setText("Score: "+score);
458   }
459   /*Adds the score.*/
460   private void updateLives()
461   {
462     livesSprite.setText("Lives: "+lives);
463   }
464   /*Adds the lives.*/
465   private void addSprites()
466   {
467     canvas.addSprite(scoreSprite);
468     canvas.addSprite(livesSprite);
469     canvas.addSprite(timerSprite);
470   }
471   /*Adds the score and lives to the game.*/
472 
473   private void youLose()
474   {
475     if((lives==0|| (timeLeft==0))
476     {
477       canvas.addSprite(gameOver);
478       canvas.removeSprite(ball);
479     }
480   }
481   /**Let's you know that Game is over once the number of lives is equal to zero or time is equal to zero.*/
482   private void winGame()
483   {
484     if(score==127)
485     {
486       canvas.addSprite(youWin);
487       canvas.removeSprite(ball);
488       canvas.removeSprite(paddle);
489       applause.play();
490     }
491   }
492   /*gives play a bonus life if they achieve a score of 20*/
493 
494   private void addTime()
495   {
496     if(score==45 && timeLeft<35)
497     {
498       timeLeft=timeLeft+35;
499     }
500   }
501 
502   private void bonusLives()
503   {
504     if(score==57 && glevel==1)
505     {
506       glevel=2;
507       makeBricks2();
508       canvas.addSprite(ball2);
509     }
510     /**Adds the second ball to the game at level two*/
511     if(score==40)
512     {
513       lives=lives+2;
514       score=score+1;
515       canvas.addSprite(txtBonusLives);
516       applause.play();
517     }
518     /**Adds life and score to game while playing a sound and adding text letting player knows they received a bonus*/
519     if(score==45)
520     {
521       canvas.removeSprite(txtBonusLives);
522     }
523     updateLives();
524     /**Removes text letting player know that they have received a bonus*/
525   }
526 
527   public void advanceFrame(double timePassed)
528   {
529     if(timeLeft>0)
530     {
531       double x=getPlayer().getMouse().getLocation().x;
532       paddle.setLocation(x,paddle.getLocation().y);
533       wallBounce();
534       ballBounce2();
535       brickIntersects();
536       brickIntersects2();
537       randomColorBound();        //Got help from Jayson.
538       bonusLives();
539       addTime();
540       youLose();
541       winGame();
542       if(getPlayer().getKeyboard().getLastKey()=='v')
543       {
544         score=57;
545         canvas.removeSprite(bricks);             //got help from Blake.
546       }
547       if(getPlayer().getKeyboard().getLastKey()=='g')
548       {
549         score=127;
550         canvas.removeSprite(bricks);             //got help from Blake.
551         canvas.removeSprite(ball);
552         canvas.removeSprite(ball2);
553       }
554       if (lives == && gameFloor.intersects(ball))
555       {
556         lives--;
557         boo.play();
558       }
559       if (lives == && gameFloor.intersects(ball2))
560       {
561         lives--;
562         boo.play();
563       }
564       if(gameFloor.intersects(ball&& lives > 1)
565       {
566         lives--;
567         boo.play();
568         ball.setLocation(.5,.4);
569       }
570       if(gameFloor.intersects(ball2&& lives > 1)
571       {
572         lives--;
573         boo.play();
574         ball2.setLocation(.6,.5);
575       }
576 
577       updateLives();
578     }
579 
580   }
581   /*Subtract lives everytime the ball drops below the floor.*/
582 }


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