HemendraPavitra/Breakout

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  */
012 public class Breakout extends GameLoop
013 {
014   /**a rectangle*/
015   private Sprite rectangle, topCeiling, leftWall, rightWall, gameFloor;
016   private double startYCoord, startXCoord;
017   private Sprite paddle, ball;
018   private ProjectileTracker tracker;
019   private int level;
020   private Sprite[] bricks;
021   private int score, lives;
022   private StringSprite scoreSprite, livesSprite, gameOver, youWin,txtBonusLives;
023 
024   /**sets up the game*/
025   public void startGame()
026   {
027     makeSprites();
028     addSprites();
029     makeBricks();
030     maketopCeiling();
031     makeLeftWall();
032     makeRightWall();
033     makePaddle();
034     makeBall();
035     makeTracker();
036     makeGameFloor();
037     level=0;
038     score=0;
039     lives=5;
040     setHelpText ("<h1>Breakout</h1)" +
041                  "<br><br>Hit bricks with the ball to remove from screen.<br><br> Enjoy!");
042 
043   }
044 
045   /**makes the sprites*/
046   private void makeSprites()
047   {
048     scoreSprite=new StringSprite("Score: "+score);
049     scoreSprite=new StringSprite("Score: 0");
050     scoreSprite.setScale(0.2);
051     scoreSprite.rightJustify();
052     scoreSprite.setColor(Color.GREEN);
053     scoreSprite.setLocation(.9.95);
054     /*Adds score to the game.*/
055 
056     livesSprite=new StringSprite("Lives: "+lives);
057     livesSprite=new StringSprite("Lives: 5");
058     livesSprite.setScale(0.19);
059     livesSprite.setColor(Color.GREEN);
060     livesSprite.setLocation(.2.95);
061     /*Adds lives to the game.*/
062 
063     gameOver=new StringSprite("Game Over: Loser!!");
064     gameOver.setScale(0.3);
065     gameOver.setColor(Color.RED);
066     gameOver.setLocation(.5.5);
067     /*Lets player know that game is over.*/
068 
069     youWin=new StringSprite("Winner!!");
070     youWin.setScale(0.3);
071     youWin.setColor(Color.RED);
072     youWin.setLocation(.5.5);
073     /*Lets player know that they won the game.*/
074 
075     txtBonusLives=new StringSprite("Bonus Lives & Points!!");
076     txtBonusLives.setScale(0.3);
077     txtBonusLives.setColor(Color.RED);
078     txtBonusLives.setLocation(.5.95);
079   }
080 
081   private void makeBricks()
082   {
083     int index=0;
084     bricks=new Sprite[70];
085     int numBricksHigh=5;
086     double startLocationY=.25;
087     double endLocationY=1-startLocationY;
088     double distanceHigh=endLocationY-startLocationY;
089     for(int j=0; j<numBricksHigh; j++)
090     {
091       double y=j*distanceHigh/(numBricksHigh-1)+startLocationY;
092 
093       int numBricksAcross=14;
094       double startLocationX=0.1;
095       double endLocationX=1-startLocationX;
096       double distanceAcross=endLocationX-startLocationX;
097       for(int i=0; i<numBricksAcross; i++)
098       {
099         double x=i*distanceAcross/(numBricksAcross-1)+startLocationX;
100         bricks[index]=new RectangleSprite(21);
101         bricks[index].setScale(0.055);
102         bricks[index].setLocation(x,y/3.5);
103         bricks[index].setColor(Color.RED);
104         canvas.addSprite(bricks[index]);
105         index++;
106         /*Makes the bricks to add to the game.*/
107       }
108     }
109   }
110 
111   public void maketopCeiling()
112   {
113     topCeiling = new RectangleSprite(20.5);
114     topCeiling.setLocation(.5.01);
115     topCeiling.setColor(Color.RED);
116     canvas.addSprite(topCeiling);
117     /*Makes ceiling to add to the game.*/
118   }
119 
120   public void makeLeftWall()
121   {
122     leftWall = new RectangleSprite(110);
123     leftWall.setLocation(startXCoord / 4.5.5);
124     leftWall.setColor(Color.RED);
125     canvas.addSprite(leftWall);
126     /*Makes left wall to add to the game.*/
127   }
128 
129   public void makeRightWall()
130   {
131     rightWall = new RectangleSprite(110);
132     rightWall.setLocation(- startXCoord / 4.5.5);
133     rightWall.setColor(Color.RED);
134     canvas.addSprite(rightWall);
135     /*Makes right wall to add to the game.*/
136   }
137 
138   public void makeGameFloor()
139   {
140     gameFloor = new RectangleSprite(20,.5);
141     gameFloor.setLocation(.5.99);
142     gameFloor.setColor(Color.BLACK);
143     canvas.addSprite(gameFloor);
144     /*Makes floor to add to the game.*/
145   }
146 
147   public void makePaddle()
148   {
149     paddle = new OvalSprite(5.5);
150     paddle.setLocation(.5.8);
151     paddle.setScale(.15);
152     paddle.setColor(Color.BLACK);
153     canvas.addSprite(paddle);
154     /*Makes paddle to add to game.*/
155   }
156 
157   public void makeBall()
158   {
159     ball = new OvalSprite(11);
160     ball.setLocation(.5.7799);
161     ball.setScale(.02);
162     ball.setColor(Color.BLACK);
163     canvas.addSprite(ball);
164     /*Makes ball to add to game.*/
165   }
166 
167   public void randomColorBound()
168   {
169     ball.setColor(new Color(random.nextInt()));
170     paddle.setColor(new Color(random.nextInt()));
171     /*Makes the ball and paddle random colors.*/
172   }
173 
174   private void makeTracker()
175   {
176     tracker= new ProjectileTracker(0.35,-.35);
177     ball.setTracker(tracker);
178     /*Makes tracker for the ball.*/
179   }
180 
181   private void wallBounce()
182   {
183     if(paddle.intersects(ball))
184     {
185       double normal=Sprite.getNormalVector(paddle.getShape(), ball.getShape());
186       tracker.bounce(normal);
187       /*Makes ball bounce off of paddle.*/
188     }
189 
190     if(rightWall.intersects(ball))
191     {
192       double normal=Sprite.getNormalVector(rightWall.getShape(), ball.getShape());
193       tracker.bounce(normal);
194       /*Makes ball bounce off right wall.*/
195     }
196 
197     if(leftWall.intersects(ball))
198     {
199       double normal=Sprite.getNormalVector(leftWall.getShape(), ball.getShape());
200       tracker.bounce(normal);
201       /*Makes ball bounce off left wall.*/
202     }
203 
204     if(topCeiling.intersects(ball))
205     {
206       double normal=Sprite.getNormalVector(topCeiling.getShape(), ball.getShape());
207       tracker.bounce(normal);
208       /*Makes ball bounce off of top ceiling.*/
209     }
210   }
211 
212   private void brickIntersects()
213   {
214     for(int i=0; i<bricks.length; i++)
215     {
216 
217       if(bricks[i].isVisible() && bricks[i].intersects(ball))
218       {
219         score++;
220         if(level==0)
221         {
222           double normal=Sprite.getNormalVector(bricks[i].getShape(), ball.getShape());
223           tracker.bounce(normal);
224           bricks[i].setVisible(false);
225         }
226       }
227     }
228     /*Increases score if ball intersects brick.*/
229     updateScore();
230   }
231 
232   /**adds the sprites to the screen*/
233   private void updateScore()
234   {
235     scoreSprite.setText("Score: "+score);
236   }
237   /*Adds the score.*/
238   private void updateLives()
239   {
240     livesSprite.setText("Lives: "+lives);
241   }
242   /*Adds the lives.*/
243   private void addSprites()
244   {
245     canvas.addSprite(scoreSprite);
246     canvas.addSprite(livesSprite);
247   }
248   /*Adds the score and lives to the game.*/
249 
250   /**handle input and game events*/
251 
252   private void resetGame()
253   {
254     if(lives==0)
255     {
256       canvas.addSprite(gameOver);
257     }
258   }
259 
260   private void winGame()
261   {
262     if(score==71)
263     {
264       canvas.addSprite(youWin);
265       canvas.removeSprite(ball);
266       canvas.removeSprite(paddle);
267     }
268   }
269   /*gives play a bonus life if they achieve a score of 20*/
270   private void bonusLives()
271   {
272     if(score==35)
273     {
274       lives=lives+1;
275       score=score+1;
276       canvas.addSprite(txtBonusLives);
277     }
278     updateLives();
279   }
280   public void advanceFrame(double timePassed)
281   {
282     double x=getPlayer().getMouse().getLocation().x;
283     paddle.setLocation(x,paddle.getLocation().y);
284     wallBounce();
285     brickIntersects();
286     randomColorBound();        //Got help from Jayson.
287     winGame();
288     bonusLives();
289     resetGame();
290     if (lives == && gameFloor.intersects(ball))
291     {
292       lives--;
293     }
294     if(gameFloor.intersects(ball&& lives > 1)
295     {
296       lives--;
297       ball.setLocation(.5,.5);
298     }
299     updateLives();
300   }
301   /*Subtract lives everytime the ball drops below the floor.*/
302 }


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