Schwarz/Assignment7

From ggc

Jump to: navigation, search

001 package Schwarz;
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 *  This is the classic game break out of wich the objective is to get rid of all the briks. 
012 *  The first time a brick is hit, it changes color from blue to red. When a red is hit, the brick disappears
013 *  and the score is increased. If the user does not hit the ball with the
014 *  paddle before it hits the botem of the screen, a life is lost. You begin with three lives.
015 *  Looked at Anam's code to get my items to spin. Also used some of the code from space invaders example code to start off.
016 *
017 *  @author Jacob Schwarz
018 */
019 
020 public class Assignment7 extends Game
021 {
022   private ArrayList<Sprite> bricks;
023   private RectangleSprite brick;
024   private OvalSprite ball, leftwall, rightwall;
025   private OutlineSprite boundary;
026   private LineSprite paddle;
027   private StringSprite score;
028   private StringSprite lives;
029   private ProjectileTransformer ballTransformer, brickTransformer;
030   private StringSprite gameover, win;
031   private int rows = 4;
032   private int columns = 10;
033   private int startlives;
034 
035   /**creates the bricks to be broken*/
036   public void totalBricks()
037   {
038     double brickwidth = 0.1;
039     bricks = new ArrayList<Sprite>();
040     for(int r=0; r<rows; r++)
041     {
042       for(int c=0; c<columns; c++)
043       {
044         brick = new RectangleSprite(21);
045         brick.setSize(.6/columns);
046         brick.setColor(getColor("blue"));
047         double = .18+c*0.7/columns;
048         double = .15+r*0.3/rows;
049         brick.setLocation(x, y);
050 
051         Spinner spin= new Spinner(0);
052         spin.setRotationDegrees(30);
053         brick.addTransformer(spin);
054         addSprite(brick);
055         bricks.add(brick);
056       }
057     }
058   }
059 
060   /**creates the paddle that the ball bounses off of*/
061   public void makePaddle()
062   {
063     paddle= new LineSprite(.4.9.6.9);
064     paddle.setLocation(.5.9);
065     paddle.setLineThickness(.06);
066     paddle.setColor(getColor("white"));
067 
068     Spinner spinning= new Spinner(0);
069     spinning.setRotationDegrees(50);
070     paddle.addTransformer(spinning);
071     addSprite(paddle);
072 
073   }
074 
075   /**creates the ball that is used to destroy the brcks*/
076   public void makeBall(double x, double y)
077   {
078     ball = new OvalSprite(1,1);
079     ball.setLocation(.5.7);
080     ball.setColor(getColor("yellow"));
081     ball.setSize(0.07);
082     addSprite(ball);
083     ballTransformer=new ProjectileTransformer(x, y);
084     ball.setTracker(ballTransformer);
085     ball.setBlurLength(3);
086 
087   }
088 
089   /**alows the paddle to move with the mouse*/
090   public void movePaddle()
091   {
092     paddle.setX(getMouseX());
093   }
094 
095   /**adds the score to the game board*/
096   public void score()
097   {
098     setScore(0);
099     score=new StringSprite ("Score:"+getScore());
100     score.setLocation(0.950.05);
101     score.setSize(0.25);
102     score.setColor(getColor("red"));
103     score.rightJustify();
104     addSprite(score);
105   }
106 
107   /**adds the number of lives the to the board*/
108   public void lives()
109   {
110     setLives(3);
111     lives=new StringSprite("Lives:"+getLives());
112     lives.setLocation(0.050.05);
113     lives.setSize(0.25);
114     lives.setColor(getColor("white"));
115     lives.leftJustify();
116     addSprite(lives);
117   }
118 
119   /**creates the boundry that the ball will be able to bounse off of*/
120   private void makeBoundary()
121   {
122     boundary=new OutlineSprite(new RectangleSprite(2,2));
123     boundary.setLineThickness(0.05);
124     boundary.setLocation(0.50.6);
125     boundary.setSize(1.0);
126     boundary.setColor(getColor("orange"));
127     addSprite(boundary);
128   }
129 
130   /**adds some bumpers to the game to make it harder to play*/
131   private void addBumpers()
132   {
133     leftwall = new OvalSprite(11);
134     leftwall.setColor(getColor("white"));
135     leftwall.setSize(.25);
136     leftwall.setLocation(.01,.5);
137     addSprite(leftwall);
138 
139     rightwall = new OvalSprite(11);
140     rightwall.setColor(getColor("white"));
141     rightwall.setSize(.25);
142     rightwall.setLocation(0.99,.5);
143     addSprite(rightwall);
144   }
145 
146   /**if the player looses all lives this string will be displayed*/
147   public void gameOver()
148   {
149     gameover = new StringSprite("Game Over!!!");
150     gameover.setSize(.9);
151     gameover.setColor(getColor("green"));
152     gameover.setLocation(.5.3);
153     removeAllSprites();
154     addSprite(gameover);
155     setLives(0);
156     pause();
157   }
158 
159   public void winGame()
160   {
161     win = new StringSprite("YOU WIN!!!!");
162     win.setSize(.9);
163     win.setColor(getColor("Yellow"));
164     win.setLocation(.5.3);
165     removeAllSprites();
166     addSprite(win);
167   }
168 
169   /** checks to see if the ball falls below the paddle and if so is reset and a new ball is placed*/
170   public void checkBallPosition()
171   {
172     if(ball.getLocation().y>.93)
173     {
174       ball.setVisible(false);
175       makeBall(.5,.5);
176       startlives= getLives();
177       setLives(startlives-1);
178       lives.setText("Lives: "+ getLives());
179     }
180     if(getLives()<1)
181     {
182       gameOver();
183     }
184 
185     if(getScore()>=1000)
186     {
187       winGame();
188     }
189   }
190 
191   /**registers when the ball hits a brick and cnages the color to red if blue and makes the brick disapere if the brick is hit when red*/
192   public void ballHitBrick()
193   {
194     for(Sprite single: bricks)
195     {
196       if(single.isVisible()== true && single.intersects(ball))
197       {
198         ball.bounceOffOf(single);
199         if(getColorName(single.getColor())=="Red")
200         {
201           single.setVisible(false);
202           int startscore = getScore();
203           setScore(startscore+25);
204           score.setText("Score:"+getScore());
205         }
206         if(getColorName(single.getColor())=="Blue")
207           single.setColor(getColor("Red"));
208       }
209     }
210   }
211 
212   /**handles the collision of th ball with the other objects causing them to bounce*/
213   public void handleCollisions()
214   {
215     ball.bounceOffOf(boundary);
216     ball.bounceOffOf(paddle);
217     ball.bounceOffOf(leftwall);
218     ball.bounceOffOf(rightwall);
219 
220     ballHitBrick();
221   }
222 
223   /**sets up the game*/
224   public void setup()
225   {
226     totalBricks();
227     makePaddle();
228     makeBall(.5.5);
229     score();
230     lives();
231     makeBoundary();
232     addBumpers();
233 
234     String helpText=
235         "Hello this page teaches you the rulls of the game.<br>"+
236         "The objective is to break all of the bricks without loosing all your lives.<br>"+
237         "To break the bricks you must make them go from blue to red by hitting them with the ball then hit them again when thier red to break them.<br>"+
238         "Press the button that says resume and select r to restart the game.";
239     setHelpText(helpText);
240   }
241 
242   /**handle input and game events*/
243   public void advance()
244   {
245     if(getLives()>0)
246     {
247       movePaddle();
248       handleCollisions();
249       checkBallPosition();
250     }
251 
252     if(getKeyPressed()=='r')
253     {
254       removeAllSprites();
255       setup();
256     }
257 
258   }
259 }


Download/View Schwarz/Assignment7.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