cray/BreakOut

From ggc

Jump to: navigation, search

001 package cray;
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 Cray
011  */
012 public class BreakOut extends GameLoop
013 {
014   private Sprite[] bricks;
015   private Sprite paddle,ball;
016   private ProjectileTracker tracker;
017   private Sprite leftWall;
018   private Sprite rightWall;
019   private Sprite topWall;
020   private Sprite bottomWall;
021   private int score=0;
022   private int lives=3;
023   private StringSprite scoreSprite;
024   private StringSprite lifeSprite;
025   private StringSprite lose;
026   private StringSprite win;
027   private Sound clip=new Sound("Pong.wav");
028   private Sound clip2=new Sound("Doh.wav");
029   private Sound clip3=new Sound("Coin.wav");
030   private Sound clip4=new Sound("Kidscheer.wav");
031 
032 
033 
034   public void startGame()
035   {
036     makeSprites();
037     makeBricks();
038     makeWalls();
039   }
040 
041   private void makeBricks()
042   {
043     int index=0;
044     bricks=new Sprite[60];
045     int bricksHigh=6;
046     double startLocationY=.1;
047     double endLocationY=.4;
048     double brickDistanceHigh=endLocationY-startLocationY;
049     for(int i=0; i<bricksHigh; i++)
050     {
051 
052       double y=i*brickDistanceHigh/(bricksHigh-1)+startLocationY;
053       int bricksAcross=10;
054       double startLocationX=.1;
055       double endLocationX=1-startLocationX;
056       double brickDistanceAcross=endLocationX-startLocationX;
057       for(int j=0; j<bricksAcross; j++)
058       {
059         double x=j*brickDistanceAcross/(bricksAcross-1)+startLocationX;
060         bricks[index]=new RectangleSprite(2,1);
061         bricks[index].setScale(.08);
062         bricks[index].setLocation(x,y);
063         bricks[index].setColor(Color.GREEN);
064         canvas.addSprite(bricks[index]);
065         index++;
066       }
067     }
068   }
069 
070   private void makeWalls()
071   {
072     leftWall=new OvalSprite(1,20);
073     leftWall.setScale(1);
074     leftWall.setLocation(0,.5);
075     leftWall.setColor(Color.YELLOW);
076     canvas.addSprite(leftWall);
077 
078     rightWall=new OvalSprite(1,20);
079     rightWall.setScale(1);
080     rightWall.setLocation(1,.5);
081     rightWall.setColor(Color.YELLOW);
082     canvas.addSprite(rightWall);
083 
084     topWall=new OvalSprite(20,1);
085     topWall.setScale(1);
086     topWall.setLocation(.5,0);
087     topWall.setColor(Color.YELLOW);
088     canvas.addSprite(topWall);
089 
090   }
091 
092   private void makeSprites()
093   {
094     paddle=new OvalSprite(8,1);
095     paddle.setScale(.25);
096     paddle.setLocation(.5,.9);
097     paddle.setColor(Color.BLUE);
098     canvas.addSprite(paddle);
099 
100     ball=new OvalSprite(1,1);
101     ball.setScale(.05);
102     ball.setLocation(.5,.7);
103     ball.setColor(Color.RED);
104     canvas.addSprite(ball);
105 
106     double angle=random.nextDouble()*Math.PI;
107 
108     tracker=new ProjectileTracker(Math.cos(angle),
109                                   .5);
110 
111     scoreSprite=new StringSprite("Score: "+score);
112     scoreSprite.setHeight(.05);
113     scoreSprite.setColor(Color.ORANGE);
114     scoreSprite.setLocation(.15.95);
115     canvas.addSprite(scoreSprite);
116 
117     lifeSprite=new StringSprite("Lives: "+lives);
118     lifeSprite.setHeight(.05);
119     lifeSprite.setColor(Color.WHITE);
120     lifeSprite.setLocation(.15.87);
121     canvas.addSprite(lifeSprite);
122 
123     lose=new StringSprite("YOU LOSE");
124     lose.setHeight(.15);
125     lose.setColor(Color.RED);
126     lose.setLocation(.5.5);
127 
128 
129     win=new StringSprite("WINNER!!!");
130     win.setHeight(.15);
131     win.setColor(Color.GREEN);
132     win.setLocation(.5.5);
133 
134 
135   }
136 
137   public void advanceFrame(double timePassed)
138   {
139     if(lives>&& score<60)
140     {
141       double x=getPlayer().getMouse().getLocation().x;
142       paddle.setLocation(x,paddle.getLocation().y);
143 
144       Point2D.Double lClick=getPlayer().getMouse().getLeftClickLocation();
145       Point2D.Double rClick=getPlayer().getMouse().getRightClickLocation();
146 
147       if(lClick!=null)
148       {
149         ball.setTracker(tracker);
150       }
151 
152 
153       if(rClick!=null)
154       {
155         score=60;
156         updateScore();
157       }
158 
159 
160 
161       if(paddle.intersects(ball))
162       {
163         double normal=Sprite.getNormalVector(paddle.getShape(), ball.getShape());
164         tracker.bounce(normal);
165       }
166 
167       wallBounce();
168       hitBricks();
169     }
170     else if(lives==0)
171     {
172       canvas.addSprite(lose);
173     }
174     else if(score==60)
175     {
176       canvas.addSprite(win);
177       ball.setTracker(null);
178 
179     }
180 
181 
182 
183 
184 
185   }
186 
187 
188   private void hitBricks()
189   {
190     for(int i=0; i<bricks.length; i++)
191     {
192       if(bricks[i].intersects(ball&& bricks[i].isVisible())
193       {
194         double normal=Sprite.getNormalVector(bricks[i].getShape(), ball.getShape());
195         tracker.bounce(normal);
196         bricks[i].setVisible(false);
197         score++;
198         updateScore();
199         clip3.play();
200 
201       }
202     }
203   }
204 
205 
206   private void wallBounce()
207   {
208     if(leftWall.intersects(ball))
209     {
210       double normal=Sprite.getNormalVector(leftWall.getShape(), ball.getShape());
211       tracker.bounce(normal);
212       clip.play();
213     }
214     if(rightWall.intersects(ball))
215     {
216       double normal=Sprite.getNormalVector(rightWall.getShape(), ball.getShape());
217       tracker.bounce(normal);
218       clip.play();
219     }
220     if(topWall.intersects(ball))
221     {
222       double normal=Sprite.getNormalVector(topWall.getShape(), ball.getShape());
223       tracker.bounce(normal);
224       clip.play();
225     }
226     if(ball.getLocation().y>1)
227     {
228       ball.setLocation(.5,.7);
229       lives--;
230       updateScore();
231       ball.setTracker(null);
232       clip2.play();
233     }
234   }
235   private void updateScore()
236   {
237     scoreSprite.setText("Score: "+score);
238     lifeSprite.setText("Lives: "+lives);
239   }
240 }


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