Blocktrial

From ggc

Jump to: navigation, search
  • curved walls
  • point power up
  • life power up

001 import fang.*;
002 import java.awt.*;
003 import java.awt.geom.*;
004 
005 /**
006  * BreakOut.
007  @author Chull
008 I received a lot of help from the following student's games. mainly the curved walls from Jayson's and how to removed blocks when the ball hits from Hemendras.
009  http://ggc.javawide.org/index.php/HemendraPavitra/Breakout
010 http://ggc.javawide.org/index.php/Jayson/breakout/Breakout
011  
012  */
013 
014 public class Blocktrial extends GameLoop
015 {
016 
017   /** the boundaries  for the game */
018   private Sprite rectangle, topCeiling, leftWall, rightWall, gameFloor;
019 
020   /**the coordinates for x and y that the game uses for spacing */
021   private double startYCoord, startXCoord;
022 
023   /**The score and lives*/
024   private int score, lives;
025 
026   /**an block*/
027   private Sprite[] block;
028 
029   /**a ball*/
030   private Sprite ball;
031 
032   /**give extra life*/
033   private Sprite extra;
034 
035   /**gives extra Points*/
036   private Sprite points;
037 
038   /**a paddle*/
039   private Sprite paddle;
040 
041   /**The Score, lives , game win and game over sections*/
042   private StringSprite scoreSprite, livesSprite, gameWin, gameOver;
043 
044   /**The Projectile Tracker which sets how the ball moves*/
045   private ProjectileTracker tracker;
046 
047 
048 
049   /**sets up the game*/
050   public void startGame()
051 
052   {
053     makePowerUpTwo();
054     scheduleRelative(new PowerUp()1);
055     scheduleRelative(new PowerUpTwo()5);
056     makeSprites();
057     makeBricks();
058     maketopCeiling();
059     makeLeftWall();
060     makeRightWall();
061     makeGameFloor();
062     makeGameParts();
063     makePowerUp();
064     makeGameStatus();
065 
066 
067     score=0;
068     lives=5;
069     setHelpText("Use the mouse to move the paddle to destroy blocks. To win break all the blocks or get 24 points. Each block is worth 1 point. There are 2 power ups which drop down Extra Life and Extra Point. The game is winnable without breaking all the blocks as long as you get 24 points.");
070     addSprites();
071 
072   }
073   /**Makes the bricks*/
074   private void makeBricks()
075   {
076     int index=0;
077     block=new Sprite[24];
078     int numBricksHigh=3;
079     double startLocationY=0.25;
080     double endLocationY=1-startLocationY;
081     double distanceHigh=endLocationY-startLocationY;
082     for(int j=0; j<numBricksHigh; j++)
083     {
084       double y=j*distanceHigh/(numBricksHigh-1)+startLocationY;
085       int numBricksAcross=8;
086       double startLocationX=0.1;
087       double endLocationX=1-startLocationX;
088       double distanceAcross=endLocationX-startLocationX;
089       for(int i=0; i<numBricksAcross; i++)
090 
091       {
092         double x=i*distanceAcross/(numBricksAcross-1)+startLocationX;
093         block[index]=new RectangleSprite(21);
094         block[index].setScale(0.055);
095         block[index].setLocation(x,y/3.5);
096         block[index].setColor(Color.RED);
097         canvas.addSprite(block[index]);
098         index++;
099       }
100     }
101   }
102   /**Creates the top wall.*/
103   public void maketopCeiling()
104   {
105     topCeiling = new RectangleSprite(20.5);
106     topCeiling.setLocation(.5.01);
107     topCeiling.setColor(Color.RED);
108     canvas.addSprite(topCeiling);
109   }
110 
111   /**Creates the left wall.*/
112   public void makeLeftWall()
113   {
114     leftWall = new OvalSprite(112);
115     leftWall.setLocation(startXCoord / 4.5.5);
116     leftWall.setScale(1.2);
117     leftWall.setColor(Color.RED);
118     canvas.addSprite(leftWall);
119   }
120 
121   /**Creates the right wall.*/
122   public void makeRightWall()
123   {
124     rightWall = new OvalSprite(112);
125     rightWall.setLocation(- startXCoord / 4.5.5);
126     leftWall.setScale(1.2);
127     rightWall.setColor(Color.RED);
128     canvas.addSprite(rightWall);
129   }
130 
131   /**Creates the bottom wall.*/
132   public void makeGameFloor()
133   {
134     gameFloor = new RectangleSprite(20,.5);
135     gameFloor.setLocation(01);
136     gameFloor.setColor(Color.BLUE);
137     gameFloor.setScale(10);
138     /** canvas.addSprite(gameFloor); **/
139 
140   }
141 
142   /**Creates the ball, paddle, and tracker.*/
143   public void makeGameParts()
144   {
145 
146     ball=new OvalSprite(11);
147     ball.setLocation(.5.6);
148     ball.setScale(.025);
149 
150     tracker=new ProjectileTracker(0.5);
151     ball.setTracker(tracker);
152 
153 
154 
155     paddle = new OvalSprite(61);
156     paddle.setScale(0.2);
157     paddle.setLocation(0.50.8);
158     paddle.setColor(Color.RED);
159 
160   }
161 
162   /**What happens when the ball hits the wall.*/
163   public void makeBallWall()
164   {
165     if(topCeiling.intersects(ball))
166     {
167       double normal=Sprite.getNormalVector(topCeiling.getShape(), ball.getShape());
168       tracker.bounce(normal);
169     }
170 
171     if(leftWall.intersects(ball))
172     {
173       double normal=Sprite.getNormalVector(leftWall.getShape(), ball.getShape());
174       tracker.bounce(normal);
175     }
176 
177     if(rightWall.intersects(ball))
178     {
179       double normal=Sprite.getNormalVector(rightWall.getShape(), ball.getShape());
180       tracker.bounce(normal);
181     }
182     if(extra.intersects(paddle))
183     {
184 
185       if(extra.isVisible() && extra.intersects(paddle))
186         lives++;
187       extra.setVisible(false);
188 
189     }
190 
191     if(points.intersects(paddle))
192     {
193       if(points.isVisible() && points.intersects(paddle))
194         score++;
195       points.setVisible(false);
196     }
197 
198 
199   }
200 
201   /**makes the sprites*/
202   private void makeSprites()
203   {
204     /*Adds score to the game which keeps track oh how well you are doing.*/
205     scoreSprite=new StringSprite("Score: "+score);
206     scoreSprite=new StringSprite("Score: 0");
207     scoreSprite.setScale(0.2);
208     scoreSprite.rightJustify();
209     scoreSprite.setColor(Color.BLUE);
210     scoreSprite.setLocation(.9.95);
211 
212     /*Adds lives to the game which determines how many trys you have left.*/
213     livesSprite=new StringSprite("Lives: "+lives);
214     livesSprite=new StringSprite("Lives: 3");
215     livesSprite.setScale(0.19);
216     livesSprite.setColor(Color.BLUE);
217     livesSprite.setLocation(.2.95);
218 
219   }
220   /**displays game won or lost*/
221   public void makeGameStatus()
222   {
223 
224     /*Lets player know that game is over and they have no more lives.*/
225     gameOver=new StringSprite("Game Over!");
226     gameOver.setScale(0.3);
227     gameOver.setColor(Color.RED);
228     gameOver.setLocation(.5.5);
229 
230     /*Lets player know that game is won.*/
231     gameWin=new StringSprite("You Win!");
232     gameWin.setScale(0.3);
233     gameWin.setColor(Color.RED);
234     gameWin.setLocation(.5.5);
235   }
236 
237 
238   /** Gives power ups*/
239   public void makePowerUp()
240   {
241     extra=new ImageSprite(Wiki.getMedia("Life.JPG"));
242     extra.setScale(0.20);
243     extra.setLocation(0.50.1);
244 
245 
246   }
247 
248   /** Gives power ups*/
249   public void makePowerUpTwo()
250   {
251 
252 
253     points=new ImageSprite(Wiki.getMedia("Points.JPG"));
254     points.setScale(0.20);
255     points.setLocation(0.50.1);
256   }
257 
258 
259 
260   /**calls PowerUp*/
261   class PowerUp implements Alarm
262   {
263     public void alarm()
264     {
265 
266       ProjectileTracker extraT;
267       extraT=new ProjectileTracker(0.5);
268       extra.setTracker(extraT);
269       canvas.addSprite(extra);
270       extra.setLocation(0.50.1);
271       scheduleRelative(this,10);
272       extra.setVisible(true);
273 
274     }
275   }
276 
277   /**calls PowerUpTwo*/
278   class PowerUpTwo implements Alarm
279   {
280     public void alarm()
281     {
282 
283 
284       ProjectileTracker pointsT;
285       pointsT=new ProjectileTracker(0.5);
286       points.setTracker(pointsT);
287       canvas.addSprite(points);
288       points.setLocation(0.50.1);
289       scheduleRelative(this,15);
290       points.setVisible(true);
291 
292 
293     }
294   }
295 
296   /**adds the sprites to the screen*/
297   private void addSprites()
298   {
299     canvas.addSprite(scoreSprite);
300     canvas.addSprite(livesSprite);
301     canvas.addSprite(block);
302     canvas.addSprite(paddle);
303     canvas.addSprite(ball);
304   }
305   /**This part of the code is for when the ball hits the block.*/
306   private void brickIntersects()
307   {
308     for(int i=0; i<block.length; i++)
309     {
310 
311       if(block[i].isVisible() && block[i].intersects(ball))
312       {
313         score++;
314 
315         {
316           double normal=Sprite.getNormalVector(block[i].getShape(), ball.getShape());
317           tracker.bounce(normal);
318           block[i].setVisible(false);
319         }
320       }
321     }
322     /*Increases score if ball intersects block.*/
323     updateScore();
324   }
325 
326   /**adds the sprites to the screen*/
327   private void updateScore()
328   {
329     scoreSprite.setText("Score: "+score);
330   }
331   /*Adds the score.*/
332   private void updateLives()
333   {
334     livesSprite.setText("Lives: "+lives);
335   }
336 
337   private void resetGame()
338   {
339     if(lives==0)
340     {
341       canvas.addSprite(gameOver);
342     }
343 
344     if(score==24)
345     {
346       canvas.addSprite(gameWin);
347     }
348   }
349 
350   /**handle input and game events*/
351   public void advanceFrame(double timePassed)
352   {
353     resetGame();
354     makeBallWall();
355 
356     if (lives == && gameFloor.intersects(ball))
357     {
358       lives--;
359     }
360     if(gameFloor.intersects(ball&& lives > 1)
361     {
362       lives--;
363       ball.setLocation(.5,.5);
364     }
365     updateLives();
366 
367     brickIntersects();
368     double x=getPlayer().getMouse().getLocation().x;
369 
370     paddle.setLocation(x , paddle.getLocation().y);
371 
372     if(paddle.intersects(ball))
373     {
374       double normal=Sprite.getNormalVector(paddle.getShape(), ball.getShape());
375       tracker.bounce(normal);
376     }
377   }
378 }
379 

Compiler Errors:
----------
1. ERROR in Blocktrial.java (at line 93)
	block[index]=new RectangleSprite(2, 1);
	             ^^^^^^^^^^^^^^^^^^^^^^^^^
The constructor RectangleSprite(int, int) is undefined
----------
2. ERROR in Blocktrial.java (at line 93)
	block[index]=new RectangleSprite(2, 1);
	             ^^^^^^^^^^^^^^^^^^^^^^^^^
The constructor ImageSprite(URL) is undefined
----------
8 problems (8 errors)

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