Wilson/Breakout

From ggc

Jump to: navigation, search

001 package Wilson;
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 Wilson Green
013  */
014 public class Breakout extends Game
015 {
016   /**Global variables
017   */
018   ArrayList<Sprite> topEnemies;
019   RectangleSprite heroPaddle = new RectangleSprite(12,1);
020   OvalSprite ball;
021   ProjectileTransformer projectile;
022   StringSprite score, scoreInt, lives;
023   OutlineSprite boundary = new OutlineSprite(new RectangleSprite(2,2));
024   OutlineSprite bottomBoundary = new OutlineSprite(new LineSprite(0,1,1,1));
025   PolygonSprite leftTriangle = new PolygonSprite(3);
026   PolygonSprite rightTriangle = new PolygonSprite(3);
027   int blocks;
028 
029   /**runs the various methods that set up the game
030   */
031   public void setup()
032   {
033     setScore(0);
034     setLives(3);
035     enemySetup();
036     createPlayer();
037     makeAndAddBall();
038     handleCollision();
039     makeBoundary();
040     score();
041     lives();
042   }
043 
044   /**Creates the paddle sprite that the player controls
045   */
046   private void createPlayer()
047   {
048     heroPaddle.setSize(.2);
049     heroPaddle.setLocation(.5.9);
050     heroPaddle.setColor(getColor("WHITE"));
051     addSprite(heroPaddle);
052   }
053 
054   /**Lets you move the heroPaddle to where the mouse is horizontally along the x-axis
055   */
056   private void movePaddle()
057   {
058     heroPaddle.setX(getMouseX());
059   }
060 
061   /**Creates a stringSprite for lives and adds it
062   */
063   public void lives()
064   {
065     lives=new StringSprite("Lives: " + getLives());
066     lives.setSize(0.2);
067     lives.setColor(getColor("white"));
068     lives.setLocation(0.3.95);
069     lives.setVisible(true);
070     addSprite(lives);
071   }
072 
073   /**Updates the lives stringSprite
074   */
075   private void updateLives()
076   {
077     lives.setText("Lives: " + getLives());
078   }
079 
080   /**Creates and initalizes a stringSprite for your score
081   */
082   private void score()
083   {
084     score=new StringSprite ("Score: " + getScore());
085     score.setSize(0.2);
086     score.setColor(getColor("WHITE"));
087     score.setLocation(.7.95);
088     addSprite(score);
089   }
090 
091   /**Updates your score
092   */
093   private void updateScore()
094   {
095     score.setText("Score: " + getScore());
096   }
097 
098   /**This creates and adds a OvalSprite for the ball in the center of the game
099   */
100   private void makeAndAddBall()
101   {
102     ball=new OvalSprite(0.10.1);
103     ball.setLocation(0.50.5);
104     ball.setColor(getColor("WHITE"));
105     ball.setSize(.05);
106     addSprite(ball);
107     projectile=new ProjectileTransformer(0.20.35);
108     ball.setTracker(projectile);
109   }
110 
111   /**This method creates the outline boundary for the game, so the ball wont leave.
112     Also creates another boundary at the bottom for determining when you lose a life
113     And the two triangles in the bottom left and right corner are created here.
114   */
115   private void makeBoundary()
116   {
117     boundary.setLineThickness(0.01);
118     boundary.setLocation(0.50.5);
119     boundary.setSize(1.0);
120     boundary.setColor(getColor("BLACK"));
121     addSprite(boundary);
122     bottomBoundary.setLineThickness(0.01);
123     bottomBoundary.setLocation(0.50.99);
124     bottomBoundary.setSize(1.0);
125     bottomBoundary.setColor(getColor("BLACK"));
126     addSprite(bottomBoundary);
127     leftTriangle.setLocation(01);
128     leftTriangle.setScale(.5);
129     leftTriangle.setColor(getColor("red"));
130     leftTriangle.setVisible(true);
131     addSprite(leftTriangle);
132     rightTriangle.setLocation(11);
133     rightTriangle.setScale(.5);
134     rightTriangle.setColor(getColor("red"));
135     rightTriangle.setVisible(true);
136     addSprite(rightTriangle);
137   }
138 
139   /**Sets up the enemy and uses nested for statements to create the enemies from an arraylist.
140   */
141   private void enemySetup()
142   {
143     topEnemies=new ArrayList<Sprite>();
144     int enemiesAcross=10;
145     int enemiesDown=3;
146     double enemyWidth=1;
147     for(int j=0; j<enemiesDown; j++)
148     {
149       for(int i=1; i<=enemiesAcross; i++)
150       {
151         RectangleSprite enemy=new RectangleSprite(11);
152         enemy.setSize(0.9/enemiesAcross);
153         enemy.setColor(getColor("RED"));
154         double x=0.5/enemiesAcross+1.0/enemiesAcross*(i-1);
155         x=x*enemyWidth;
156         enemy.scale(enemyWidth);
157         double y=0.5/enemiesAcross+(j+0.0)/enemiesAcross;
158         y=y*enemyWidth;
159         enemy.setLocation(x, y);
160         addSprite(enemy);
161         topEnemies.add(enemy);
162       }
163     }
164     blocks = topEnemies.size();
165   }
166 
167   /**This sets up the string that shows if you lose all of your lives.
168   */
169   private void gameOver()
170   {
171     StringSprite end = new StringSprite("Game Over!");
172     end.setSize(.3);
173     end.setLocation(.5.5);
174     end.setColor(getColor("WHITE"));
175     ball.hide();
176     end.setVisible(true);
177     addSprite(end);
178     stop();
179   }
180 
181   /**This sets up the string that shows if you destroy all the bricks.
182   */
183   public void win()
184   {
185     StringSprite winner = new StringSprite("You Win!");
186     winner.setSize(.3);
187     winner.setLocation(.5.5);
188     winner.setColor(getColor("WHITE"));
189     ball.hide();
190     winner.setVisible(true);
191     addSprite(winner);
192     stop();
193   }
194 
195   /**This method handles what happens when the ball touches the  boundarys.
196   */
197   private void handleCollision()
198   {
199     for(Sprite single: topEnemies)
200     {
201       if(single.isVisible()==true && single.intersects(ball))
202       {
203         ball.bounceOffOf(single);
204         if(getColorName(single.getColor()) == "Green")
205         {
206           single.hide();
207           blocks--;
208         }
209         else
210           single.setColor(getColor("green"));
211         int original=getScore();
212         setScore(original+10);
213       }
214     }
215     if(ball.intersects(heroPaddle))
216       ball.bounceOffOf(heroPaddle);
217     if(ball.intersects(leftTriangle))
218       ball.bounceOffOf(leftTriangle);
219     if(ball.intersects(rightTriangle))
220       ball.bounceOffOf(rightTriangle);
221     if(ball.intersects(boundary))
222       ball.bounceOffOf(boundary);
223     if(ball.intersects(bottomBoundary))
224     {
225       if(getLives() 0)
226       {
227         setLives(getLives() 1);
228         updateLives();
229         ball.setLocation(.5.5);
230       }
231       else
232         gameOver();
233     }
234   }
235 
236   public void checkBlocks()
237   {
238     if (blocks == 0)
239       win();
240   }
241 
242   /**handle input and game events*/
243   public void advance()
244   {
245     checkBlocks();
246     movePaddle();
247     handleCollision();
248     updateScore();
249   }
250 }


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