Derrick/AssignmentSevenBreakOut

From ggc

Jump to: navigation, search

001 package Derrick;
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  * Frank and I worked on this together but at the end we could not agree on a couple of *items so we went our seperate ways and he turned his completed version in and I turned in *my completed version.*  
012 *  A ball is set in motion to hit bricks.The first time a brick is hit, 
013 *  it changes color from red to green. When a green is hit, the brick disappears
014 *  and the score is increased. The buttons give you a chance to play
015  * with the ball moving rapidly or slowly. If the user does not
016 *  hit the ball with the paddle, a 'life' is lost. You have three lives.
017  @author Derrick and Frank
018  */
019 public class AssignmentSevenBreakOut extends Game
020 {
021   /** starttLives is the initial value of the
022     * number of times the player can miss the ball with the paddle. */
023   private int startScore, startLives, numClicks;
024 
025   /** allBricks is an ArrayList that stores the targets to be hit by the ball. */
026   private ArrayList<Sprite> allBricks;
027   /** ball is the object used to strike targets(bricks) and eventually resulting
028     * in  score. */
029   private OvalSprite ball;
030   /** boundary represents the top and sides of the game field against
031     * which the ball bounces. */
032   private OutlineSprite boundary ;
033   /** brick is an element of ArrayList, allBricks.  */
034   private RectangleSprite brick;
035   /** score is the number of green bricks hit. */
036   /** lives the the number of times remaining for the ball to go below the paddle  play again message and start message*/
037   private StringSprite score, lives, helpBox, playAgain, start;
038 
039   /** paddle is the horizontl line used to keep the ball in play. If the
040     * ball goes below the paddle, lives decreases by one.  */
041   private LineSprite paddle;
042   /** leftWall is a projection added as an enhncement on the left of the game field.  */
043   private PolygonSprite leftWall;
044   /** rightWall is a projection added as an enhncement on the right of the game field.  */
045   private PolygonSprite rightWall;
046   /** ballTransformer give the ball and initial velocity.
047       brickTransformer gives the brick and initial velocity.   */
048   private ProjectileTransformer  ballTransformer, brickTransformer;
049   /** numberOfRows is the number of horizontal rows of bricks displayed at the top of the screen.   */
050   private int numberOfRows = 3;
051   /** numberOfColumns is the number of vertical columns into which the bricks are arranged.   */
052   private int numberOfColumns = 8;
053   /**  bricksRemaining represents the number of bricks remaining as targets.  */
054   private int bricksRemaining;
055   /**  gameOver is a message indicating that the game is over.  */
056   private StringSprite gameOver;
057   /** buttonFast and buttonSlow allows the user to choose the speed of the ball.   */
058   private ButtonSprite buttonFast;
059   private ButtonSprite buttonSlow;
060   /**  ballXSpeed and ballYSpeed initializes the speed of the ball
061     *  in the x-direction and y-direction, respecively.  */
062   private double ballXSpeed;
063   private double ballYSpeed;
064 
065 
066   /**makes and creates help box.*/
067 
068   private void makeAndAddGetHelp()
069   {
070     String helpBox=
071         "Press start to begin.<br>"+
072         "Press 'r' and choose a button.<br>"+
073         "These buttons will increase or decrease the ball speed";
074 
075     setHelpText(helpBox);
076   }
077   /**Creates start set of instructions.*/
078   private void clickStartToPlay()
079   {
080     start = new StringSprite("Click Start to Play");
081     start.setSize(.9);
082     start.setColor(getColor("orange"));
083     start.setLocation(.5.5);
084     addSprite(start);
085   }
086   /**Creates buttons.*/
087   private void displayButtons()
088   {
089     buttonFast = new ButtonSprite("Faster?");
090     buttonFast.setLocation(.2,.95);
091     buttonFast.setSize(.2);
092     addSprite(buttonFast);
093 
094     buttonSlow = new ButtonSprite("Slower?");
095     buttonSlow.setSize(.2);
096     buttonSlow.setLocation(.8,.95);
097 
098     addSprite(buttonSlow);
099   }
100   /**Display halve of an oval for the left and right walls.*/
101   private void addCurvedWalls()
102   {
103     leftWall = new PolygonSprite(0,0,.05,.15,.1,.3,.2,.4,.1,.5,.05,.6,0,1);
104     leftWall.setColor(getColor("grey"));
105     leftWall.setSize(1.2);
106     leftWall.setLocation(.01,.5);
107     addSprite(leftWall);
108 
109     rightWall = new PolygonSprite(1,0,.95,.15,.9,.3,.8,.4,.9,.5,.95,.6,1,1);
110     rightWall.setColor(getColor("grey"));
111     rightWall.setSize(1.2);
112     rightWall.setLocation(0.99,.5);
113     addSprite(rightWall);
114   }
115 
116 
117   /** Determine the count, stored in bricksRemaining, of the number of visible bricks.   */
118   private int checkRemainingBricks()
119   {
120     bricksRemaining = 0;
121     for(Sprite single : allBricks)
122     {
123       if(single.isVisible() == true)
124         bricksRemaining++;
125     }
126     return bricksRemaining;
127   }
128 
129 
130 
131   /** Displays the statement "GAME OVER". The ball is also removed from view. */
132   private void displayGameOver()
133   {
134     lives.setVisible(false);
135     score.setVisible(false);
136     brick.setVisible(false);
137     gameOver = new StringSprite("GAME OVER");
138     gameOver.setSize(.9);
139     gameOver.setColor(getColor("orange"));
140     gameOver.setLocation(.5.3);
141     addSprite(gameOver);
142     pause();
143     ball.setVisible(false);
144     paddle.setVisible(false);
145     setLives(0);
146 
147   }
148 
149 
150   /** If the ball position falls below the paddle, it disappears and a life is lost. */
151   private void checkBallYPosition()
152   {
153     if(ball.getLocation().y>.93)
154     {
155       ball.setVisible(false);
156       makeAndAddBall(.1, -.4);
157       startLives = getLives();
158       setLives(startLives-1);
159       lives.setText("Lives:"+getLives());
160     }
161     if (getLives()<1)
162       displayGameOver();
163   }
164 
165   /** Check to see if the ball collides with a visible brick. If it does,
166     * make the brick disappear and increase the score by 10.  */
167   private void checkBallCollidingWithBrick()
168   {
169     for(Sprite single: allBricks)
170     {
171       if(single.isVisible()== true && single.intersects(ball))
172       {
173         ball.bounceOffOf(single);
174 
175         if(getColorName(single.getColor())=="Green")
176         {
177           single.setVisible(false);
178           startScore = getScore();
179           setScore(startScore+1);
180           score.setText("Score:"+getScore());
181         }
182         if(getColorName(single.getColor())=="Red")
183           single.setColor(getColor("green"));
184       }
185     }
186 
187   }
188 
189   /** Provide a surface from which the ball can bounce. The bottom boundary
190     * is not visible, but the ball never reaches it because of checks on the
191     * y-position of the ball.  */
192   private void makeAndAddBoundary()
193   {
194     boundary=new OutlineSprite(new RectangleSprite(2,2));
195     boundary.setLineThickness(0.05);
196     boundary.setLocation(0.50.6);
197     boundary.setSize(1.0);
198     boundary.setColor(getColor("Gray"));
199     addSprite(boundary);
200 
201   }
202 
203   /** Establish and display the number of lives remaining for the player
204     * and the score based on the number of bricks hit.   */
205   private void makeAndAddScoreAndLives()
206   {
207     setLives(3);
208     lives=new StringSprite("Lives:"+getLives());
209     lives.setLocation(0.050.05);
210     lives.setSize(0.25);
211     lives.setColor(getColor("white"));
212     lives.leftJustify();
213     addSprite(lives);
214 
215     setScore(0);
216     score=new StringSprite ("Score:"+getScore());
217     score.setLocation(0.950.05);
218     score.setSize(0.25);
219     score.setColor(getColor("white"));
220     score.rightJustify();
221     addSprite(score);
222   }
223 
224 
225    /** A ball will be used to hit the bricks. A ball is created at the point (.5, .85)
226       * and initially moves with speed provided as parameters.
227      * The parameters provide the speed in the x-direction as the y-direction. 
228       @param x is the the initial velocity of the ball in the x-direction.
229      @param y is the initial velocity of the ball in the y-direction. */
230   private void makeAndAddBall(double x, double y)
231   {
232     ball=new OvalSprite(11);
233     ball.setLocation(0.50.85);
234     ball.setSize(0.05);
235     ball.setColor(getColor("yellow"));
236     addSprite(ball);
237     ballTransformer=new ProjectileTransformer(x, y);
238     ball.setTracker(ballTransformer);
239     ball.setBlurLength(3);
240   }
241 
242   /** Create a line Sprite to be used as a paddle. */
243   private void makeAndAddPaddle()
244   {
245     paddle=new LineSprite (0.40.950.60.95);
246     paddle.setLocation(0.50.9);
247     paddle.setColor(getColor("yellow"));
248     paddle.setLineThickness(0.05);
249     addSprite(paddle);
250   }
251 
252   /** Move the paddle so that the ball can bounce off of the paddle. */
253   private void movePaddle()
254   {
255     paddle.setX(getMouseX());
256   }
257 
258   /* Cause the ball to bounce off the boundary, paddle, or brick. **/
259   private void handleCollisions()
260   {
261 
262 
263     ball.bounceOffOf(boundary);
264 
265     ball.bounceOffOf(paddle);
266 
267     ball.bounceOffOf(leftWall);
268     ball.bounceOffOf(rightWall);
269 
270     checkBallCollidingWithBrick();
271   }
272   /** Create bricks, display them on the screen, and add to an ArrayList, bricks. */
273   private void createBricks()
274   {
275     allBricks = new ArrayList<Sprite>();
276     for (int = 0; i<numberOfRows; i++)
277     {
278       forint = 0; j < numberOfColumns; j++)
279       {
280         brick = new RectangleSprite(2,1);
281         brick.setColor(getColor("Red"));
282         brick.setSize(.7/numberOfColumns);
283         brick.setLocation(.18+j*.8/numberOfColumns, .15+ i*.2/numberOfRows);
284         brick.setVisible(true);
285         addSprite(brick);
286         allBricks.add(brick);
287       }
288     }
289   }
290   /**Changes the speed of the ball based on player's choice.*/
291   private void changeSpeed()
292   {
293     ifgetClick2D() != null && getClick2D().intersects(buttonSlow))
294     {
295 
296       ball.setVisible(false);
297       ballXSpeed=.1;
298       ballYSpeed=-.2;
299       makeAndAddBall(ballXSpeed, ballYSpeed);
300       buttonSlow.setVisible(false);
301       buttonFast.setVisible(false);
302 
303     }
304     if(getClick2D() != null  && getClick2D().intersects(buttonFast))
305     {
306       ball.setVisible(false);
307       ballXSpeed=.1;
308       ballYSpeed=-.8;
309       makeAndAddBall(ballXSpeed, ballYSpeed);
310       buttonSlow.setVisible(false);
311       buttonFast.setVisible(false);
312 
313 
314     }
315   }
316   /**sets up the game*/
317   public void setup()
318   {
319     makeAndAddGetHelp();
320     createBricks();
321     makeAndAddPaddle();
322     makeAndAddBoundary();
323     makeAndAddScoreAndLives();
324     bricksRemaining = 0;
325     addCurvedWalls();
326     clickStartToPlay();
327     makeAndAddBall(.1,-.6);
328     numClicks++;
329 
330 
331   }
332 
333   /**handle input and game events*/
334   public void advance()
335   {
336     changeSpeed();
337 
338     /** Removes the start instruction */
339 
340     if(numClicks==1)
341     {
342       start.setVisible(false);
343       numClicks=0;
344     }
345 
346     /** If the player presses the r button the player gets the option of a faster or slower ball. */
347 
348     if(getKeyPressed()=='r')
349     {
350       displayButtons();
351     }
352 
353 
354 
355 
356     if(getLives()>0)
357     {
358       movePaddle();
359       // allows the user to move the paddle.
360       handleCollisions();
361       // bounces the ball of the walls; kills bricks.
362       checkBallYPosition();
363       // used to determine whether to end the game
364       if (checkRemainingBricks() <=0)
365 
366         // If there are no bricks remaining,
367         displayGameOver();
368 
369       //the game is over.
370     }
371   }
372 }


Download/View Derrick/AssignmentSevenBreakOut.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