FA/Consolidated Assignment 7

From ggc

Jump to: navigation, search

001 package FA;
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  * Assignment 7: Breakdot.
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 *
018 @author Derrick Dixon
019 @author Frank Anderson
020 */
021 public class Consolidated_Assignment_7 extends Game
022 {
023   /** startLives is the initial value of the
024     * number of times the player can miss the ball with the paddle. */
025   private int startLives;
026   /** startScore initializes the score and keeps track of the score. */
027   private int startScore;
028   /** allBricks is an ArrayList that stores the targets to be hit by the ball. */
029   private ArrayList<Sprite> allBricks;
030   /** ball is the object used to strike targets(bricks) and eventually resulting
031     * in  score. */
032   private OvalSprite ball;
033   /** boundary represents the top and sides of the game field against
034     * which the ball bounces. */
035   private OutlineSprite boundary ;
036   /** brick is an element of ArrayList, allBricks.  */
037   private RectangleSprite brick;
038   /** score is the number of green bricks hit. */
039   /** lives the the number of times remaining for the ball to go below the paddle */
040   private StringSprite score, lives;
041   /** paddle is the horizontl line used to keep the ball in play. If the
042     * ball goes below the paddle, lives decreases by one.  */
043   private LineSprite paddle;
044   /** leftWall is a projection added as an enhncement on the left of the game field.  */
045   private PolygonSprite leftWall;
046   /** rightWall is a projection added as an enhncement on the right of the game field.  */
047   private PolygonSprite rightWall;
048   /** ballTransformer give the ball and initial velocity.
049       brickTransformer gives the brick and initial velocity.   */
050   private ProjectileTransformer  ballTransformer, brickTransformer;
051   /** numberOfRows is the number of horizontal rows of bricks displayed at the top of the screen.   */
052   private int numberOfRows = 3;
053   /** numberOfColumns is the number of vertical columns into which the bricks are arranged.   */
054   private int numberOfColumns = 8;
055   /**  bricksRemaining represents the number of bricks remaining as targets.  */
056   private int bricksRemaining;
057   /**  gameOver is a message indicating that the game is over.  */
058   private StringSprite gameOver;
059   /** buttonFast and buttonSlow allows the user to choose the speed of the ball.   */
060   private ButtonSprite buttonFast;
061   private ButtonSprite buttonSlow;
062   /**  ballXSpeed and ballYSpeed initializes the speed of the ball
063     *  in the x-direction and y-direction, respecively.  */
064   private double ballXSpeed;
065   private double ballYSpeed;
066 
067   /** Place two buttons near the bottom of the screen to allow the user
068     * to select the speed of the ball in play.  */
069   private void displayButtons()
070   {
071     buttonFast = new ButtonSprite("Speed=Fast");
072     buttonFast.setLocation(.4,.95);
073     buttonFast.setSize(.2);
074     addSprite(buttonFast);
075 
076     buttonSlow = new ButtonSprite("Speed=Slow");
077     buttonSlow.setSize(.2);
078     buttonSlow.setLocation(.6,.95);
079     addSprite(buttonSlow);
080   }
081 
082   /** Add an obstacle to the left and right walls.  */
083   private void addCurvedWalls()
084   {
085     leftWall = new PolygonSprite(0,0,.05,.15,.1,.3,.2,.4,.1,.5,.05,.6,0,1);
086     leftWall.setColor(getColor("blue"));
087     leftWall.setSize(1.2);
088     leftWall.setLocation(.01,.5);
089     addSprite(leftWall);
090 
091     rightWall = new PolygonSprite(1,0,.95,.15,.9,.3,.8,.4,.9,.5,.95,.6,1,1);
092     rightWall.setColor(getColor("green"));
093     rightWall.setSize(1.2);
094     rightWall.setLocation(0.99,.5);
095     addSprite(rightWall);
096   }
097 
098 
099   /** Determine the count, stored in bricksRemaining, of the number of visible bricks.   */
100   private int checkRemainingBricks()
101   {
102     bricksRemaining = 0;
103     for(Sprite single : allBricks)
104     {
105       if(single.isVisible() == true)
106         bricksRemaining++;
107     }
108     return bricksRemaining;
109   }
110 
111 
112 
113   /** Displays the statement "GAME OVER". The ball is also removed from view.
114     * When the game is over, the game pauses. */
115   private void displayGameOver()
116   {
117     buttonFast.setVisible(false);
118     buttonSlow.setVisible(false);
119     gameOver = new StringSprite("GAME OVER");
120     gameOver.setSize(.9);
121     gameOver.setColor(getColor("orange"));
122     gameOver.setLocation(.5.3);
123     addSprite(gameOver);
124     ball.setVisible(false);
125     paddle.setVisible(false);
126     setLives(0);
127     pause();
128   }
129 
130 
131   /** If the ball position falls below the paddle, it disappears and a life is lost. */
132   private void checkBallYPosition
133   ()
134   {
135     if(ball.getLocation().y>.93)
136     {
137       ball.setVisible(false);
138       makeAndAddBall(.1, -.4);
139       startLives = getLives();
140       setLives(startLives-1);
141       lives.setText("Lives:"+getLives());
142     }
143     if (getLives()<1)
144       displayGameOver();
145   }
146 
147   /** Check to see if the ball collides with a visible brick. If the brick is red,
148     * change the brick's color to green. If the brick hit is green, 
149     * make the brick disappear and increase the score by 1.  */
150   private void checkBallCollidingWithBrick()
151   {
152     for(Sprite single: allBricks)
153     {
154       if(single.isVisible()== true && single.intersects(ball))
155       {
156         ball.bounceOffOf(single);
157 
158         if(getColorName(single.getColor())=="Green")
159         {
160           single.setVisible(false);
161           startScore = getScore();
162           setScore(startScore+1);
163           score.setText("Score:"+getScore());
164         }
165         if(getColorName(single.getColor())=="Red")
166           single.setColor(getColor("green"));
167       }
168     }
169 
170   }
171 
172   /** Provide a surface from which the ball can bounce. The bottom boundary
173     * is not visible, but the ball never reaches it because of checks on the
174     * y-position of the ball.  */
175   private void makeAndAddBoundary()
176   {
177     boundary=new OutlineSprite(new RectangleSprite(2,2));
178     boundary.setLineThickness(0.05);
179     boundary.setLocation(0.50.6);
180     boundary.setSize(1.0);
181     boundary.setColor(getColor("Gray"));
182     addSprite(boundary);
183   }
184 
185   /** Establish and display the number of lives remaining for the player
186     * and the score based on the number of bricks hit.   */
187   private void makeAndAddScoreAndLives()
188   {
189     setLives(3);
190     lives=new StringSprite("Lives:"+getLives());
191     lives.setLocation(0.050.05);
192     lives.setSize(0.25);
193     lives.setColor(getColor("white"));
194     lives.leftJustify();
195     addSprite(lives);
196 
197     setScore(0);
198     score=new StringSprite ("Score:"+getScore());
199     score.setLocation(0.950.05);
200     score.setSize(0.25);
201     score.setColor(getColor("white"));
202     score.rightJustify();
203     addSprite(score);
204   }
205 
206   /** A ball will be used to hit the bricks. A ball is created at the point (.5, .85)
207     * and initially moves with speed provided as parameters.
208     * The parameters provide the speed in the x-direction as the y-direction. 
209     @param x is the the initial velocity of the ball in the x-direction.
210     @param y is the initial velocity of the ball in the y-direction. */
211   private void makeAndAddBall(double x, double y)
212   {
213     ball=new OvalSprite(11);
214     ball.setLocation(0.50.85);
215     ball.setSize(0.05);
216     ball.setColor(getColor("yellow"));
217     addSprite(ball);
218     ballTransformer=new ProjectileTransformer(x, y);
219     ball.setTracker(ballTransformer);
220     ball.setBlurLength(3);
221   }
222 
223   /** Create a line Sprite to be used as a paddle. */
224   private void makeAndAddPaddle()
225   {
226     paddle=new LineSprite (0.40.950.60.95);
227     paddle.setLocation(0.50.9);
228     paddle.setColor(getColor("yellow"));
229     paddle.setLineThickness(0.05);
230     addSprite(paddle);
231   }
232 
233   /** Move the paddle horizontally with the mouse position
234     * so that the ball can bounce off of the paddle. */
235   private void movePaddle()
236   {
237     paddle.setX(getMouseX());
238   }
239 
240   /** Cause the ball to bounce off the boundary, paddle, left wall,
241     * right wall, or brick. **/
242   private void handleCollisions()
243   {
244     ball.bounceOffOf(boundary);
245     ball.bounceOffOf(paddle);
246 
247     ball.bounceOffOf(leftWall);
248     ball.bounceOffOf(rightWall);
249 
250     checkBallCollidingWithBrick();
251   }
252 
253   /** Create red bricks, display them on the screen, and add to an ArrayList, allBricks. */
254   private void createBricks()
255   {
256     allBricks = new ArrayList<Sprite>();
257     for (int = 0; i<numberOfRows; i++)
258     {
259       forint = 0; j < numberOfColumns; j++)
260       {
261         brick = new RectangleSprite(2,1);
262         brick.setColor(getColor("Red"));
263         brick.setSize(.7/numberOfColumns);
264         brick.setLocation(.18+j*.8/numberOfColumns, .15+ i*.2/numberOfRows);
265         brick.setVisible(true);
266         addSprite(brick);
267         allBricks.add(brick);
268       }
269     }
270   }
271 
272   /**sets up the game*/
273   public void setup()
274   {
275     createBricks();
276     makeAndAddPaddle();
277     makeAndAddBoundary();
278     makeAndAddScoreAndLives();
279     bricksRemaining = 0;
280     addCurvedWalls();
281     displayButtons();
282     makeAndAddBall(.1,-.4);
283 
284   }
285 
286 
287   /**handle input and game events*/
288   public void advance()
289   {
290 
291     /**Clicking on a button selects the initial ball speed and allows the user
292       * to change the speed during the game.  */
293     ifgetClick2D() != null && getClick2D().intersects(buttonSlow))
294     {
295 
296       ball.setVisible(false);
297       ballXSpeed=.1;
298       ballYSpeed=-.2;
299       makeAndAddBall(ballXSpeed, ballYSpeed);
300 
301     }
302     if(getClick2D() != null  && getClick2D().intersects(buttonFast))
303     {
304       ball.setVisible(false);
305       ballXSpeed=.4;
306       ballYSpeed=-.4;
307       makeAndAddBall(ballXSpeed, ballYSpeed);
308 
309     }
310 
311 
312     /** Play the game as long as lives > 0.  */
313     if(getLives()>0)
314     {
315 
316       movePaddle();
317       handleCollisions();
318       checkBallYPosition();
319       if (checkRemainingBricks() <=0)
320         displayGameOver();
321     }
322   }
323 }


Download/View FA/Consolidated_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