BlakeErskine/breakout

From ggc

Jump to: navigation, search

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


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