mkim/breakout

From ggc

Jump to: navigation, search

001 package mkim;
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 import java.lang.Object.*;
010 /**
011  * All about my game.
012  @author My Name Here
013  */
014 public class breakout extends Game
015 {
016   /** lives set*/
017   private int startlives=3;
018   /**ovalSprite make circles*/
019   private OvalSprite ball, ball2;
020   /** square wall*/
021   private RectangleSprite rwall1, rwall2;
022   /**rectangle bricks*/
023   private RectangleSprite bricks;
024   /** text stuff*/
025   private StringSprite score, lives, gameOver, gameWin;
026   /**arrays of brick*/
027   private ArrayList<Sprite> allBricks;
028   /**arrays of powerup*/
029   private ArrayList<OvalSprite> powerUps;
030   /** outline of sprite*/
031   private OutlineSprite paddle, paddle2, wall1, wall2, wall3, wall4;
032   /** balll bounce setting transformer*/
033   private ProjectileTransformer projectile, projectile2, projectile3;
034   /**set remainingBlock to a int*/
035   private int remainingBrick;
036   /**sets up the game*/
037   public void setup()
038   {
039     bricks();
040     makePaddle();
041     makeBall();
042     makeWall();
043     helps();
044     score();
045     lives();
046     roundWalls();
047 
048     projectile=new ProjectileTransformer(0.10.5);
049     ball.setTracker(projectile);
050     projectile=new ProjectileTransformer(0.10.6);
051     ball2.setTracker(projectile);
052   }
053   /**make help*/
054   private void helps()
055   {
056     String helpText=
057         "get 100 points to get extra ball.<br>"+
058         "good luck";
059     setHelpText(helpText);
060   }
061   /**random color*/
062   public void randomColorBound()
063   {
064     ball.setColor(new Color(random.nextInt()));
065     ball2.setColor(new Color(random.nextInt()));
066     rwall1.setColor(new Color(random.nextInt()));
067     rwall2.setColor(new Color(random.nextInt()));
068   }
069   /**array of bricks (got it from example code)*/
070   private void bricks()
071   {
072     allBricks=new ArrayList<Sprite>();
073     int bricksAcross=11;
074     int bricksDown=6;
075     for(int j=0;j<bricksDown;j++)
076     {
077       for(int i=1;i<=bricksAcross;i++)
078       {
079         bricks=new RectangleSprite(2,1);
080         bricks.setSize(0.9/bricksAcross);
081         double x=.5/bricksAcross+1.0/bricksAcross*(i-1);
082         double y=.5/16+(j+0.0)/16;
083         bricks.setLocation(x, y);
084         bricks.setColor(Palette.getColor("Blue"));
085         addSprite(bricks);
086         allBricks.add(bricks);
087       }
088     }
089   }
090   /**make two paddles*/
091   private void makePaddle()
092   {
093     paddle=new OutlineSprite(new LineSprite(0010));
094     paddle.setSize(0.25);
095     paddle.setLineThickness(0.2);
096     paddle.setLocation(0.5,0.9);
097     paddle.setColor(Palette.getColor("green"));
098     addSprite(paddle);
099   }
100   /**make balls*/
101   private void makeBall()
102   {
103     ball=new OvalSprite(1,1);
104     ball.setSize(0.05);
105     ball.setLocation(0.5,0.6);
106     ball.setColor(Palette.getColor("yellow"));
107     addSprite(ball);
108 
109     ball2=new OvalSprite(1,1);
110     ball2.setSize(0.05);
111     ball2.setLocation(0.5,0.6);
112     ball2.setColor(Palette.getColor("yellow"));
113     ball2.setVisible(false);
114   }
115   /** make walls on the side*/
116   private void makeWall()
117   {
118     wall1=new OutlineSprite(new LineSprite(0001));
119     wall1.setSize(2);
120     wall1.setLocation(0,0);
121     wall1.setColor(Palette.getColor("red"));
122     addSprite(wall1);
123 
124     wall2=new OutlineSprite(new LineSprite(0001));
125     wall2.setSize(2);
126     wall2.setLocation(1,0);
127     wall2.setColor(Palette.getColor("red"));
128     addSprite(wall2);
129 
130     wall3=new OutlineSprite(new LineSprite(0010));
131     wall3.setSize(2);
132     wall3.setLocation(0,0);
133     wall3.setColor(Palette.getColor("red"));
134     addSprite(wall3);
135 
136     wall4=new OutlineSprite(new LineSprite(0010));
137     wall4.setSize(1);
138     wall4.setLocation(0,1);
139     wall4.setColor(Palette.getColor("black"));
140     addSprite(wall4);
141   }
142   /**make squares on the right and left side*/
143   private void roundWalls()
144   {
145     rwall1=new RectangleSprite(11);
146     rwall1.setSize(.2);
147     rwall1.setLocation(1.6);
148     rwall1.setColor(Palette.getColor("white"));
149     addSprite(rwall1);
150 
151     rwall2=new RectangleSprite(11);
152     rwall2.setSize(.2);
153     rwall2.setLocation(0.6);
154     rwall2.setColor(Palette.getColor("white"));
155     addSprite(rwall2);
156   }
157   /**when the ball hits the blocks changes color and than dissapear*/
158   private void handleBallblockCollision()
159   {
160     for(Sprite single: allBricks)
161     {
162       if(single.isVisible()== true && single.intersects(ball))
163       {
164         ball.bounceOffOf(single);
165         if(getColorName(single.getColor())=="Red")
166         {
167           single.setVisible(false);
168           int startscore = getScore();
169           setScore(startscore+10);
170           score.setText("Score:"+getScore());
171         }
172         if(getColorName(single.getColor())=="Blue")
173           single.setColor(getColor("Red"));
174       }
175     }
176   }
177   /**when the second ball hits the blocks changes color and than dissapear*/
178   private void handleBalltwoblockCollision()
179   {
180     for(Sprite single: allBricks)
181     {
182       if(single.isVisible()== true && single.intersects(ball2))
183       {
184         ball2.bounceOffOf(single);
185         if(getColorName(single.getColor())=="Red")
186         {
187           single.setVisible(false);
188           int startscore = getScore();
189           setScore(startscore+10);
190           score.setText("Score:"+getScore());
191         }
192         if(getColorName(single.getColor())=="Blue")
193           single.setColor(getColor("Red"));
194       }
195     }
196   }
197   /**block remaining*/
198   private int blocksLeft()
199   {
200     remainingBrick=0;
201     for(Sprite single: allBricks)
202     {
203       if(single.isVisible()== true)
204       {
205         remainingBrick++;
206       }
207     }
208     return remainingBrick;
209   }
210   /**set score on the bottom*/
211   private void score()
212   {
213     setScore(0);
214     score=new StringSprite ("Score:"+getScore());
215     score.setLocation(0.70.95);
216     score.setSize(0.15);
217     score.setColor(getColor("white"));
218     score.rightJustify();
219     addSprite(score);
220   }
221   /**set lives*/
222   private void lives()
223   {
224     setLives(3);
225     lives=new StringSprite("Lives:"+getLives());
226     lives.setLocation(0.10.95);
227     lives.setSize(0.15);
228     lives.setColor(getColor("white"));
229     lives.leftJustify();
230     addSprite(lives);
231   }
232   /**game over text and pause the game*/
233   private void showGameOver()
234   {
235     lives.setVisible(false);
236     score.setVisible(false);
237     bricks.setVisible(false);
238     ball.setVisible(false);
239     paddle.setVisible(false);
240     pause();
241     gameOver = new StringSprite("GAME OVER");
242     gameOver.setSize(.9);
243     gameOver.setColor(getColor("green"));
244     gameOver.setLocation(.5.5);
245     addSprite(gameOver);
246     setLives(0);
247   }
248   /**set winning message pause the game*/
249   private void showGameWin()
250   {
251     paddle.setVisible(false);
252     ball.setVisible(false);
253     pause();
254     gameWin =new StringSprite("You Win");
255     gameWin.setSize(.9);
256     gameWin.setColor(getColor("green"));
257     gameWin.setLocation(0.50.5);
258     addSprite(gameWin);
259   }
260   /** if it goes down y coordinate greater than.98 and ball is not visible
261    you loose a life and if life is less than 1 shows the game over
262   (got help from derrick) */
263   private void looseBall()
264   {
265     if(ball.getLocation().y>0.98)
266     {
267       ball.setLocation(0.50.5);
268       startlives = getLives();
269       setLives(startlives-1);
270       lives.setText("Lives:"+getLives());
271     }
272     if (getLives()<1)
273     {
274       showGameOver();
275     }
276   }
277   /**makes the paddle move left and right when mouse moves*/
278   private void movePaddle()
279   {
280     paddle.setX(getMouseX());
281   }
282   /**tells where to bounce off from*/
283   private void handleCollisions()
284   {
285     ball.bounceOffOf(paddle);
286     ball.bounceOffOf(wall1);
287     ball.bounceOffOf(wall2);
288     ball.bounceOffOf(wall3);
289     ball.bounceOffOf(rwall1);
290     ball.bounceOffOf(rwall2);
291 
292     ball2.bounceOffOf(paddle);
293     ball2.bounceOffOf(wall1);
294     ball2.bounceOffOf(wall2);
295     ball2.bounceOffOf(wall3);
296     ball2.bounceOffOf(rwall1);
297     ball2.bounceOffOf(rwall2);
298   }
299   /**double paddle at point 50 extra ball when score is reaches 100*/
300   private void power()
301   {
302     if(getScore()>=100)
303     {
304       ball2.setVisible(true);
305       addSprite(ball2);
306     }
307   }
308   /**handle input and game events*/
309   public void advance()
310   {
311     movePaddle();
312     handleCollisions();
313     handleBallblockCollision();
314     handleBalltwoblockCollision();
315     randomColorBound();
316     looseBall();
317     power();
318     if(blocksLeft()<=0)
319     {
320       showGameWin();
321     }
322   }
323 }


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