Merima/BreakOut

From ggc

Jump to: navigation, search

001 package Merima;
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 Merima Omanovic
011  */
012 public class BreakOut extends GameLoop
013 {
014   private Sprite[] bricks;
015   private Sprite paddle, ball, left, right, top, bottom;
016   int score, lives;
017   private StringSprite scorelives;
018   private ProjectileTracker tracker;
019 
020 
021   /**sets up the game*/
022   public void startGame()
023   {
024     score=0;
025     lives=3;
026     makePaddle();
027     makeBall();
028     makeWalls();
029     makescorelives();
030     makeTracker();
031     makeBricks();
032     setHelpText("1. Move the paddle with the mouse. 2. Try hitting the ball to make it bounce off and hit the bricks. 3. You have 3 lives. GOODLUCK");
033 
034   }
035   /**makes the Paddle*/
036   private void makePaddle()
037   {
038     paddle = new RectangleSprite(191);
039     paddle.setLocation(.5.9);
040     paddle.setScale(.23);
041     paddle.setColor(Color.WHITE);
042     canvas.addSprite(paddle);
043   }
044   /**score and lives*/
045   private void makescorelives()
046   {
047     scorelives=new StringSprite("Score: "+ score+ " Lives: "+lives);
048     scorelives.setLocation(.5,.027);
049     scorelives.setColor(Color.WHITE);
050     scorelives.setScale(.39);
051     canvas.addSprite(scorelives);
052   }
053 
054   /**makes walls*/
055   private void makeWalls()
056   {
057     left=new RectangleSprite(3100);
058     left.setLocation(0.0150.5);
059     left.setScale(1);
060     left.setColor(Color.RED);
061     canvas.addSprite(left);
062 
063     right=new RectangleSprite(3100);
064     right.setLocation(.9850.5);
065     right.setScale(1);
066     right.setColor(Color.RED);
067     canvas.addSprite(right);
068 
069     top=new RectangleSprite(1003);
070     top.setLocation(0.50);
071     top.setScale(1);
072     top.setColor(Color.RED);
073     canvas.addSprite(top);
074 
075     bottom=new RectangleSprite(11,1);
076     bottom.setLocation(.5,1);
077     bottom.setColor(Color.BLACK);
078     bottom.setScale(1);
079     canvas.addSprite(bottom);
080 
081   }
082   /**makes ball*/
083   private void makeBall()
084   {
085     ball = new OvalSprite(22);
086     ball.setLocation(.5.9);
087     ball.setScale(.018);
088     ball.setColor(Color.WHITE);
089     canvas.addSprite(ball);
090   }
091   /**make tracker*/
092   private void makeTracker()
093   {
094     tracker= new ProjectileTracker(.1,-.5);
095     ball.setTracker(tracker);
096   }
097   /**makes bricks*/
098   public void makeBricks()
099   {
100     int index=0;
101     bricks=new Sprite[70];
102     int numBricksHigh=5;
103     double startLocationY=.25;
104     double endLocationY=1-startLocationY;
105     double distanceHigh=endLocationY-startLocationY;
106     for(int j=0; j<numBricksHigh; j++)
107     {
108       double y=j*distanceHigh/(numBricksHigh-1)+startLocationY;
109       int numBricksAcross=14;
110       double startLocationX=0.1;
111       double endLocationX=1-startLocationX;
112       double distanceAcross=endLocationX-startLocationX;
113       for(int i=0; i<numBricksAcross; i++)
114       {
115         double x=i*distanceAcross/(numBricksAcross-1)+startLocationX;
116         bricks[index]=new RectangleSprite(21);
117         bricks[index].setScale(0.06);
118         bricks[index].setLocation(x,y/4);
119         bricks[index].setColor(Color.PINK);
120         canvas.addSprite(bricks[index]);
121         index++;
122       }
123     }
124   }
125 
126   /** make double timepass*/
127   public void advanceFrame(double timePassed)
128   {
129     double x=getPlayer().getMouse().getLocation().x;
130     paddle.setLocation(x,paddle.getLocation().y);
131     bounceOffObjects();
132     hitsBricks();
133   }
134   /**make bounce*/
135   private void bounceOffObjects()
136   {
137     bounceOffPaddle();
138     bounceOffLeft();
139     bounceOffRight();
140     bounceOffTop();
141   }
142   /**make the ball bounce off the paddle*/
143   public void bounceOffPaddle()
144   {
145     if(paddle.intersects(ball))
146     {
147       double normal=Sprite.getNormalVector(paddle.getShape(), ball.getShape());
148       tracker.bounce(normal);
149     }
150   }
151   /**makes the ball bounce off left*/
152   public void bounceOffLeft()
153   {
154     if(left.intersects(ball))
155     {
156       double normal=Sprite.getNormalVector(left.getShape(), ball.getShape());
157       tracker.bounce(normal);
158     }
159   }
160   /**make the ball bounce off right*/
161   public void bounceOffRight()
162   {
163     if(right.intersects(ball))
164     {
165       double normal=Sprite.getNormalVector(right.getShape(), ball.getShape());
166       tracker.bounce(normal);
167     }
168   }
169 
170   /**makes the ball bounce off top*/
171   public void bounceOffTop()
172   {
173     if(top.intersects(ball))
174     {
175       double normal=Sprite.getNormalVector(top.getShape(), ball.getShape());
176       tracker.bounce(normal);
177     }
178   }
179   /**breaks the bricks*/
180   private void hitsBricks()
181   {
182     for(int m=0; m<bricks.length; m++)
183     {
184       if(bricks[m].isVisible() && bricks[m].intersects(ball))
185       {
186         double normal=Sprite.getNormalVector(bricks[m].getShape(), ball.getShape());
187         score++;
188         scorelives.setText("Score: "+score+" Lives: "+lives);
189         tracker.bounce(normal);
190         bricks[m].setVisible(false);
191       }
192     }
193   }
194 }
195 
196 
197 


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