NickandChris/Breakout

From ggc

Jump to: navigation, search

001 package NickandChris;
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 Nmckee
011  */
012 public class Breakout extends GameLoop
013 {
014   private Sprite blocks[];
015   private StringSprite displayScore, displayLives, win, lose;
016   private int lives, score;
017   private Sprite leftWall;
018   private Sprite rightWall;
019   private Sprite topWall;
020   private Sprite bottomWall;
021   private Sprite paddle;
022   private Sprite ball;
023   private ProjectileTracker tracker;
024 
025   public void startGame()
026   {
027     lives=4;
028     score=0;
029     blocks=new Sprite[50];
030     makeBlocks();
031     makeSprites();
032     makeTracker();
033     makeBounces();
034   }
035 
036   private void makeSprites()
037   {
038     makeScore();
039     makeLives();
040     makePaddleandBall();
041     makeWalls();
042     makeOutcome();
043   }
044 
045   public void makeBlocks()
046   {
047     int index=0;
048     int numBlocksHigh=5;
049     double startLocationY=0.1;
050     double endLocationY=0.3;
051     double distanceHigh=endLocationY-startLocationY;
052     for(int j=0; j<numBlocksHigh; j++)
053     {
054       double y=j*distanceHigh/(numBlocksHigh-1)+startLocationY;
055 
056       int numBlocksAcross=10;
057       double startLocationX=0.1;
058       double endLocationX=1-startLocationX;
059       double distanceAcross=endLocationX-startLocationX;
060       for(int i=0; i<numBlocksAcross; i++)
061       {
062         double x=i*distanceAcross/(numBlocksAcross-1)+startLocationX;
063         blocks[index]=new RectangleSprite(2.41.25);
064         blocks[index].setScale(.075);
065         blocks[index].setLocation(x, y);
066         blocks[index].setColor(Color.BLUE);
067         canvas.addSprite(blocks[index]);
068         index++;
069       }
070     }
071   }
072 
073   private void makeScore()
074   {
075     displayScore = new StringSprite("Score: "+score);
076     displayScore.setLocation(0.3.95);
077     displayScore.setColor(Color.WHITE);
078     displayScore.setScale(0.175);
079     canvas.addSprite(displayScore);
080   }
081 
082   private void makeLives()
083   {
084     displayLives = new StringSprite("Lives: "+lives);
085     displayLives.setLocation(0.7.95);
086     displayLives.setColor(Color.WHITE);
087     displayLives.setScale(0.175);
088     canvas.addSprite(displayLives);
089   }
090 
091   private void makePaddleandBall()
092   {
093     paddle=new RectangleSprite(251);
094     paddle.setColor(Color.YELLOW);
095     paddle.setScale(.15);
096     paddle.setLocation(.5.8);
097     canvas.addSprite(paddle);
098 
099     ball=new OvalSprite(11);
100     ball.setColor(Color.RED);
101     ball.setScale(0.03);
102     ball.setLocation(.5.7);
103     canvas.addSprite(ball);
104   }
105 
106   private void makeWalls()
107   {
108     leftWall=new RectangleSprite(350);
109     leftWall.setLocation(0.0150.5);
110     leftWall.setScale(1);
111     leftWall.setColor(Color.YELLOW);
112     canvas.addSprite(leftWall);
113 
114     rightWall=new RectangleSprite(350);
115     rightWall.setLocation(.9850.5);
116     rightWall.setScale(1);
117     rightWall.setColor(Color.YELLOW);
118     canvas.addSprite(rightWall);
119 
120     topWall=new RectangleSprite(503);
121     topWall.setLocation(0.50);
122     topWall.setScale(1);
123     topWall.setColor(Color.YELLOW);
124     canvas.addSprite(topWall);
125 
126     bottomWall=new RectangleSprite(501);
127     bottomWall.setLocation(0.50.99);
128     bottomWall.setScale(1);
129     bottomWall.setColor(Color.BLACK);
130     canvas.addSprite(bottomWall);
131   }
132 
133   private void makeTracker()
134   {
135     tracker=new ProjectileTracker(.1,-0.5);
136     ball.setTracker(tracker);
137   }
138 
139   public void advanceFrame(double timePassed)
140   {
141     double x=getPlayer().getMouse().getLocation().x;
142     paddle.setLocation(x,paddle.getLocation().y);
143     makeBounces();
144     hitsBlocks();
145   }
146 
147 
148   private void makeOutcome()
149   {
150     if(lives==0)
151     {
152       lose=new StringSprite("You Lose!");
153       lose.setLocation(0.50.5);
154       lose.setScale(.4);
155       lose.setColor(Color.WHITE);
156       canvas.addSprite(lose);
157       canvas.removeSprite(ball, paddle);
158     }
159 
160     if(score==50)
161     {
162       win=new StringSprite("You Win!");
163       win.setLocation(0.50.5);
164       win.setScale(.4);
165       win.setColor(Color.WHITE);
166       canvas.addSprite(win);
167       canvas.removeSprite(ball, paddle);
168     }
169 
170   }
171 
172 
173   private void makeBounces()
174   {
175     if(paddle.intersects(ball))
176     {
177       double normal=Sprite.getNormalVector(paddle.getShape(), ball.getShape());
178       tracker.bounce(normal);
179     }
180 
181     if(rightWall.intersects(ball))
182     {
183       double normal=Sprite.getNormalVector(rightWall.getShape(), ball.getShape());
184       tracker.bounce(normal);
185     }
186 
187     if(leftWall.intersects(ball))
188     {
189       double normal=Sprite.getNormalVector(leftWall.getShape(), ball.getShape());
190       tracker.bounce(normal);
191     }
192 
193     if(topWall.intersects(ball))
194     {
195       double normal=Sprite.getNormalVector(topWall.getShape(), ball.getShape());
196       tracker.bounce(normal);
197     }
198 
199 
200 
201   }
202 
203   private void hitsBlocks()
204   {
205     for(int i=0; i<blocks.length; i++)
206     {
207       if(blocks[i].isVisible() && blocks[i].intersects(ball))
208       {
209         double normal=Sprite.getNormalVector(blocks[i].getShape(), ball.getShape());
210         score++;
211         displayScore.setText("Score: "+score);
212         tracker.bounce(normal);
213         blocks[i].setVisible(false);
214 
215       }
216     }
217 
218     if(bottomWall.intersects(ball))
219     {
220       lives--;
221       displayLives.setText("Lives: "+lives);
222       ball.setLocation(.5,.5);
223       makeOutcome();
224     }
225   }
226 
227 }


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