Charles/Breakout

From ggc

Jump to: navigation, search

001 package Charles;
002 //start auto-imports
003 import java.util.*;
004 //end auto-imports
005 
006 import fang.*;
007 import java.awt.*;
008 import java.awt.geom.*;
009 
010 /**
011  * All about my game.
012  @author Charles
013  @author Chanelle
014  */
015 /** used the "getColorName()" method after looking at Frank Anderson's game*/
016 public class Breakout extends Game
017 {
018   private ArrayList<Sprite> allBricks;
019   private LineSprite wall1, wall2, wall3;
020   private RectangleSprite box;
021   private OvalSprite ball, p1, p2, p3;
022   private PolygonSprite paddle;
023   private ProjectileTransformer ballTransformer;
024   private int p, life, l, drop;
025   private StringSprite score, lives, game;
026 
027 
028   /**Sets up the game*/
029   public void setup()
030   {
031     makeLong();
032     makeAndAddBoarder();
033     makeAndAddPaddle();
034     makeAndAddBall();
035     makeAndAddBrickSet1();
036     makeAndAddScoreBoard();
037     makeAndAddLives();
038     makeEndMessage();
039     ProjectileTransformer fall=new ProjectileTransformer(0.00.1);
040   }
041   /**Creates the boarders that the ball bounces off of in the program*/
042   private void makeAndAddBoarder()
043   {
044     wall1=new LineSprite(0.0,0.90.0,0.0);
045     wall1.setLocation(0.010.54);
046     wall1.setLineThickness(0.02);
047     wall1.setColor(Color.GREEN);
048     addSprite(wall1);
049 
050     wall2=new LineSprite(0.0,0.90.0,0.0);
051     wall2.setLocation(0.990.54);
052     wall2.setLineThickness(0.02);
053     wall2.setColor(Color.GREEN);
054     addSprite(wall2);
055 
056     wall3=new LineSprite(0.0,0.90.99,0.9);
057     wall3.setLocation(0.50.1);
058     wall3.setLineThickness(0.03);
059     wall3.setColor(Color.GREEN);
060     addSprite(wall3);
061   }
062   /**Makes the Color of the Ball and all three walls change randomly*/
063   public void randomColorBound()
064   {
065     ball.setColor(new Color(random.nextInt()));
066     wall1.setColor(new Color(random.nextInt()));
067     wall2.setColor(new Color(random.nextInt()));
068     wall3.setColor(new Color(random.nextInt()));
069     game.setColor(new Color(random.nextInt()));
070     paddle.setColor(new Color(random.nextInt()));
071   }
072 
073   /**Creates a paddle to make the ball bounce back up toward the squares*/
074   private void makeAndAddPaddle()
075   {
076     paddle=new PolygonSprite(0.35,0.50.45,0.450.55,0.450.65,0.5);
077     paddle.setSize(0.2);
078     paddle.setLocation(0.50.97);
079     paddle.setColor(Color.YELLOW);
080     addSprite(paddle);
081   }
082   /**Makes the paddle move with the mouse*/
083   private void movePaddle()
084   {
085     paddle.setX(getMouseX());
086   }
087   /**Creates a ball for the game and sets up projectile transformer for the ball to bounce*/
088   private void makeAndAddBall()
089   {
090     ball=new OvalSprite(1,1);
091     ball.setSize(0.035);
092     ball.setLocation(.5,.92);
093     ball.setColor(Palette.getColor("Fire Brick"));
094     addSprite(ball);
095     double x=random.nextInt(2);
096     double y=random.nextInt(2);
097     if(x==0)
098     {
099       x=0.5;
100     }
101     else
102     {
103       x=-0.5;
104     }
105     if(y==0)
106     {
107       y=-0.25;
108     }
109     else
110     {
111       y=-0.4;
112     }
113     ProjectileTransformer bounce=new ProjectileTransformer(x, y);
114     ball.setTracker(bounce);
115   }
116   /**Creates the bricks for the ball to hit*/
117   private void makeAndAddBrickSet1()
118   {
119     allBricks=new ArrayList<Sprite>();
120     int bricksAcross=8;
121     int bricksDown=4;
122     for(int j=0; j<bricksDown; j++)
123     {
124       for(int i=1; i<=bricksAcross; i++)
125       {
126         RectangleSprite brick=new RectangleSprite(1,1);
127         brick.setSize(0.6/bricksAcross);
128         double x=.5/bricksAcross+1.0/bricksAcross*(i-1);
129         double y=.75/bricksAcross+(j+0.25)/bricksAcross;
130         y=y+0.1;
131         brick.setLocation(x,y);
132         brick.setColor(getColor("White"));
133         addSprite(brick);
134         allBricks.add(brick);
135       }
136     }
137   }
138   /**Creates a score board for the game*/
139   private void makeAndAddScoreBoard()
140   {
141     setScore(0);
142     score=new StringSprite("Score:");
143     score.setSize(0.2);
144     score.setLocation(0.8,0.05);
145     score.setColor(Palette.getColor("Yellow"));
146     addSprite(score);
147   }
148   /**creates the display for the amount of lives the player has remaining*/
149   private void makeAndAddLives()
150   {
151     life=3;
152     lives=new StringSprite("Lives:"+life);
153     lives.setSize(0.2);
154     lives.setLocation(0.2,0.05);
155     lives.setColor(Palette.getColor("Yellow"));
156     addSprite(lives);
157   }
158   /** Creates the Game Over message that is displayed when player loses*/
159   private void makeEndMessage()
160   {
161     game=new StringSprite("GAME OVER");
162     game.setLocation(0.50.5);
163     game.setSize(0.5);
164     game.setVisible(false);
165     addSprite(game);
166   }
167   /**Allows the ball to bounce off of the walls*/
168   private void ballBounce()
169   {
170     ball.bounceOffOf(wall1);
171     ball.bounceOffOf(wall2);
172     ball.bounceOffOf(wall3);
173     ball.bounceOffOf(paddle);
174   }
175   private void makeLong()
176   {
177     p1=new OvalSprite(1,1);
178     p1.setSize(0.05);
179     p1.setColor(getColor("blue"));
180     p1.setVisible(false);
181 
182   }
183   private void dropLong()
184   {
185     p1.setLocation(0.50.3);
186     p1.setVisible(true);
187     ProjectileTransformer fall=new ProjectileTransformer(0.00.1);
188     p1.setTracker(fall);
189     addSprite(p1);
190   }
191   private void dropLife()
192   {}
193   private void dropVanish()
194   {}
195   /** tells which power up to drop if any*/
196   private void getDrops()
197   {
198     if(drop==0)
199     {
200       dropLong();
201     }
202     else if(drop==4)
203     {
204       dropLife();
205     }
206     else if(drop==2)
207     {
208       dropVanish();
209     }
210   }
211   /** Handles the reaction of the player catching the power ups with the paddle*/
212   private void handleDrops()
213   {
214     if(p1.intersects(paddle))
215     {
216       paddle.setScale(0.4);
217     }
218   }
219   /**Makes the bricks change color to yellow after first hit red after second and after third dissappear and adds points get*/
220   private void handleBallCollision()
221   {
222     for(Sprite single: allBricks)
223     {
224       if(single.isVisible() && ball.intersects(single))
225       {
226         int drop=random.nextInt(6);
227 
228         int l=life;
229         if(l>=&& getColorName(single.getColor())=="White")
230         {
231           int p=getScore();
232           setScore(p+5);
233           ball.bounceOffOf(single);
234           single.setVisible(false);
235           //single.setColor(getColor("yellow"));
236         }
237         /**if(l>=&& getColorName(single.getColor())=="yellow")
238       {
239           int p=getScore();
240           setScore(p+5);
241           ball.bounceOffOf(single);
242           single.setColor(getColor("red"));
243       }
244         /**else if(l>=&& getColorName(single.getColor())=="red")
245       {
246           int p=getScore();
247           setScore(p+5);
248           ball.bounceOffOf(single);
249           single.setVisible(false);
250       }
251         */
252       }
253     }
254   }
255   /**Returns the ball after hitting the bricks*/
256   private void droppedBall()
257   {
258     if(ball.getLocation().y>=1.0 && ball.getLocation().y<=1.01)
259     {
260       int l=life;
261       if(l>=1)
262       {
263         life=l-1;
264       }
265     }
266     else if(ball.getLocation().y>=1.02 && getClick2D()!=null)
267     {
268       int l=life;
269       if(l>=1)
270       {
271         double x=getMouseX();
272         ball.setLocation(x, 0.92);
273         ProjectileTransformer newBounce=new ProjectileTransformer(00.5);
274         ball.setTracker(newBounce);
275       }
276       else if(l<=0)
277       {
278         game.setVisible(true);
279       }
280     }
281   }
282 
283   /**Handle input and game events*/
284   public void advance()
285   {
286     handleDrops();
287     getDrops();
288     randomColorBound();
289     handleBallCollision();
290     movePaddle();
291     ballBounce();
292     droppedBall();
293     score.setText("Score:"+getScore());
294     lives.setText("Lives:"+life);
295     getDrops();
296   }
297 }


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