Rohrer/Brick Breaker Assignment 7

From ggc

Jump to: navigation, search

001 package Rohrer;
002 //start auto-imports
003 //end auto-imports
004 
005 import java.util.*;
006 import fang.*;
007 import java.awt.*;
008 import java.awt.geom.*;
009 
010 /**
011  * My Brick Breaker Game.
012  @author Kevin Rohrer
013  *
014  * I planned to impliment gravity but I could not find it in the API
015  */
016 public class Brick_Breaker_Assignment_7 extends Game
017 {
018   /** bricks*/
019   private ArrayList<Sprite> bricks;
020   /** walls*/
021   private Sprite w_Wall, n_Wall, e_Wall;
022   /** ball and paddle*/
023   private Sprite ball, paddle;
024   /** lives and score display*/
025   private StringSprite livesDisplay, scoreDisplay;
026   /** actual score lives and click values*/
027   private int score, lives, clicks;
028   /** ball transformer*/
029   private ProjectileTransformer ballTransformer;
030   /** special bounce objects*/
031   private Sprite sBounce1, sBounce2;
032 
033   /**sets up the game*/
034   public void setup()
035   {
036     boundarys();
037     addBricks();
038     paddleBallSettup();
039     addScoreAndLives();
040     specialBounce();
041   }
042   /**handle input and game events*/
043   public void advance()
044   {
045     movePaddleBall();
046     bounceBall();
047     ballBrickCollision();
048     end();
049 
050     if(getClick2D()!=null)
051       clicks++;
052   }
053 
054   /** adds items at bottom od screen to allow more interesting bounces*/
055   public void specialBounce()
056   {
057     sBounce1 = new PolygonSprite(0,0,1,1,0,1);
058     sBounce1.setLocation(.1,.8);
059     sBounce1.setSize(.2);
060     sBounce1.setColor(getColor("Orange Red"));
061     addSprite(sBounce1);
062 
063     sBounce2 = new PolygonSprite(1,0,1,1,0,1);
064     sBounce2.setLocation(.9,.8);
065     sBounce2.setSize(.2);
066     sBounce2.setColor(getColor("Orange Red"));
067     addSprite(sBounce2);
068   }
069 
070 
071 
072   /** removes ball and paddle then displays game over message*/
073   public void end()
074   {
075     if(getLives() <= 0)
076     {
077       ball.removeTransformer(ballTransformer);
078       ball.setVisible(false);
079       paddle.setVisible(false);
080 
081       StringSprite end = new StringSprite("Game Over");
082       end.setWidth(.8);
083       end.setLocation(.5,.65);
084       addSprite(end);
085     }
086   }
087   /** allows you to move the paddle along the bottom of the screen
088       and sets the ball in motion when user clicks*/
089   public void movePaddleBall()
090   {
091     paddle.setX(getMouseX());
092 
093     if(getClick2D()!=null && clicks==0)
094     {
095       ballTransformer=new ProjectileTransformer(0.25, -0.5);
096       ball.addTransformer(ballTransformer);
097     }
098     if(getClick2D()!=null && clicks>0)
099     {}
100     if(ball.getLocation().y>1)
101     {
102       clicks = 0;
103       ball.removeTransformer(ballTransformer);
104       ball.setLocation(.5,.85);
105       setLives(getLives()-1);
106       livesDisplay.setText("Lives: "+getLives());
107     }
108   }
109   /** adds the score and lives display at top of screen*/
110   public void addScoreAndLives()
111   {
112     setLives(3);
113     livesDisplay = new StringSprite("Lives: "+getLives());
114     livesDisplay.setHeight(.08);
115     livesDisplay.leftJustify();
116     livesDisplay.setLocation(.02.05);
117     livesDisplay.setColor(getColor("White"));
118     addSprite(livesDisplay);
119 
120     scoreDisplay = new StringSprite("Score: "+getScore());
121     scoreDisplay.setHeight(.08);
122     scoreDisplay.leftJustify();
123     scoreDisplay.setLocation(.5,.05);
124     scoreDisplay.setColor(getColor("White"));
125     addSprite(scoreDisplay);
126   }
127   /** checks for collisions between the ball and any of the bricks and changes state acordingly*/
128   private void ballBrickCollision()
129   {
130     for(Sprite brick: bricks)
131     {
132       if(brick.isVisible() && brick.intersects(ball))
133       {
134         ball.bounceOffOf(brick);
135         if(getColorName(brick.getColor())=="Green")
136         {
137           brick.setColor(getColor("Yellow"));
138           setScore(getScore()+1);
139           scoreDisplay.setText("Score: "+getScore());
140         }
141         else if(getColorName(brick.getColor())=="Yellow")
142         {
143           brick.setColor(getColor("Red"));
144           setScore(getScore()+2);
145           scoreDisplay.setText("Score: "+getScore());
146         }
147         else if(getColorName(brick.getColor())=="Red")
148         {
149           brick.setVisible(false);
150           setScore(getScore()+3);
151           scoreDisplay.setText("Score: "+getScore());
152         }
153       }
154     }
155   }
156   /** adds the breakable bricks to the screen */
157   public void addBricks()
158   {
159     bricks=new ArrayList<Sprite>();
160     int bricksAcross=15;
161     int bricksDown=10;
162     for(int j=0; j<bricksDown; j++)
163     {
164       for(int i=1; i<=bricksAcross; i++)
165       {
166         RectangleSprite brick=new RectangleSprite(21);
167         brick.setSize(0.9/bricksAcross);
168         double x=0.5/bricksAcross+1.0/bricksAcross*(i-1);
169         double y=((0.5/bricksAcross+(j+0.0)/bricksAcross)/2)+.1;
170         brick.setLocation(x, y);
171         brick.setColor(getColor("Green"));
172         addSprite(brick);
173         bricks.add(brick);
174       }
175     }
176   }
177   /** sets the items that the ball will bounce off of */
178   public void bounceBall()
179   {
180     ball.bounceOffOf(w_Wall);
181     ball.bounceOffOf(n_Wall);
182     ball.bounceOffOf(e_Wall);
183     ball.bounceOffOf(paddle);
184     ball.bounceOffOf(sBounce1);
185     ball.bounceOffOf(sBounce2);
186   }
187   /** creates and adds paddle and ball*/
188   public void paddleBallSettup()
189   {
190     paddle = new RectangleSprite(6,1);
191     paddle.setSize(.15);
192     paddle.setLocation(.5,.9);
193     paddle.setColor(getColor("Lawn Green"));
194     addSprite(paddle);
195 
196     ball = new OvalSprite(1,1);
197     ball.setSize(.05);
198     ball.setLocation(.5,.85);
199     ball.setColor(getColor("Orange Red"));
200     addSprite(ball);
201   }
202   /** creates and adds the screen boundarys*/
203   public void boundarys()
204   {
205     w_Wall = new LineSprite(0,1,0,0);
206     w_Wall.setSize(1);
207     w_Wall.setLocation(0,.5);
208     w_Wall.setColor(getColor("black"));
209     addSprite(w_Wall);
210 
211     n_Wall = new LineSprite(0,.1,1,.1);
212     n_Wall.setSize(1);
213     n_Wall.setLocation(.5,0);
214     n_Wall.setColor(getColor("black"));
215     addSprite(n_Wall);
216 
217     e_Wall = new LineSprite(1,0,1,1);
218     e_Wall.setSize(1);
219     e_Wall.setLocation(1,.5);
220     e_Wall.setColor(getColor("black"));
221     addSprite(e_Wall);
222 
223   }
224 }


Download/View Rohrer/Brick_Breaker_Assignment_7.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