NickandChris/BreakoutFinal

From ggc

Jump to: navigation, search

-Three different levels with different color schemes
-Pause(p), reset(r)(when you lose all your lives), and mute buttons(m)
-SFX
-Multiple balls
-Balls bounce off each other and jerks around
(Stage 1 will not work no matter what I try to do. For some reason, stage 1 will cause the ball to stick to the walls even when it was working earlier.)
(http://ggc.javawide.org/index.php?title=NickandChris/BreakoutFinal&oldid=14313 works but none of the additions)

001 package NickandChris;
002 
003 import wiki.Wiki;
004 import fang.*;
005 import java.awt.*;
006 import java.awt.geom.*;
007 
008 /**
009  * All about my game here.
010  @author Nmckee
011  */
012 public class BreakoutFinal extends GameLoop
013 {
014   private Sprite blocks[];
015   private StringSprite displayScore, displayLives, win, clear, lose;
016   private int lives, score;
017   private Sprite leftWall;
018   private Sprite rightWall;
019   private Sprite topWall;
020   private Sprite bottomWall;
021   private Sprite paddle;
022   private Sound sound1, sound2, sound3, sound4, sound5, sound6;
023   private Sprite ball, ball2, ball3;
024   private ProjectileTracker tracker, tracker2, tracker3;
025   private int stage;
026 
027   public void startGame()
028   {
029     stage=0;
030     stages();
031     togglePause();
032     toggleAudible();
033     toggleSound();
034     makeSounds();
035   }
036 
037 
038   public void stages()
039   {
040     if(stage==0)
041     {
042       lives=4;
043       score=49;
044       blocks=new Sprite[50];
045       makeBlocks();
046       makeSprites();
047       makeTracker();
048       makeBounces();
049       canvas.addSprite(ball);
050     }
051 
052   }
053 
054   private void toggleSound()
055   {
056     if(getPlayer().getKeyboard().getLastKey() == 'm')
057     {
058       toggleAudible();
059     }
060   }
061 
062   private void stageClear()
063   {
064     Point2D.Double click=getPlayer().getMouse().getClickLocation();
065     if(stage==&& score==50 && click!=null)
066     {
067       stageTwo();
068     }
069     if(stage==&& score==50 && click!=null)
070     {
071       stageThree();
072     }
073   }
074 
075   private void makeSprites()
076   {
077     makeScore();
078     makeLives();
079     makePaddleandBall();
080     makeWalls();
081     makeOutcome();
082   }
083 
084   private void stageTwo()
085   {
086     stage=1;
087     lives=4;
088     score=0;
089     canvas.removeAllSprites();
090     makeWalls();
091     //canvas.removeSprite(displayLives, displayScore, clear);
092     canvas.addSprite(ball, ball2, paddle);
093     ball.setLocation(.5.7);
094     ball2.setLocation(.5.7);
095     leftWall.setColor(Color.RED);
096     rightWall.setColor(Color.RED);
097     topWall.setColor(Color.RED);
098     paddle.setScale(.19);
099     makeBlocks();
100     makeOutcome();
101     makeScore();
102     makeLives();
103   }
104 
105   private void stageThree()
106   {
107     stage=2;
108     lives=6;
109     score=0;
110     canvas.removeAllSprites();
111     //canvas.removeSprite(displayLives, displayScore, clear);
112     canvas.addSprite(ball, ball2, ball3, paddle);
113     ball.setLocation(.5.7);
114     ball2.setLocation(.5.7);
115     ball3.setLocation(.5.7);
116     paddle.setScale(.15);
117     makeBlocks();
118     makeOutcome();
119     makeScore();
120     makeLives();
121   }
122 
123   public void makeBlocks()
124   {
125     int index=0;
126     int numBlocksHigh=5;
127     double startLocationY=0.1;
128     double endLocationY=0.3;
129     double distanceHigh=endLocationY-startLocationY;
130     for(int j=0; j<numBlocksHigh; j++)
131     {
132       double y=j*distanceHigh/(numBlocksHigh-1)+startLocationY;
133 
134       int numBlocksAcross=10;
135       double startLocationX=0.1;
136       double endLocationX=1-startLocationX;
137       double distanceAcross=endLocationX-startLocationX;
138       for(int i=0; i<numBlocksAcross; i++)
139       {
140         double x=i*distanceAcross/(numBlocksAcross-1)+startLocationX;
141         blocks[index]=new RectangleSprite(2.41.25);
142         blocks[index].setScale(.075);
143         blocks[index].setLocation(x, y);
144         blocks[index].setColor(Color.BLUE);
145         if(stage==1)
146         {
147           blocks[index].setColor(Color.WHITE);
148         }
149         if(stage==2)
150         {
151           blocks[index].setColor(Color.GREEN);
152         }
153         canvas.addSprite(blocks[index]);
154         index++;
155       }
156     }
157   }
158 
159   private void makeScore()
160   {
161     displayScore = new StringSprite("Score: "+score);
162     displayScore.setLocation(0.3.95);
163     displayScore.setColor(Color.WHITE);
164     displayScore.setScale(0.175);
165     canvas.addSprite(displayScore);
166   }
167 
168 
169   private void makeLives()
170   {
171     displayLives = new StringSprite("Lives: "+lives);
172     displayLives.setLocation(0.7.95);
173     displayLives.setColor(Color.WHITE);
174     displayLives.setScale(0.175);
175     canvas.addSprite(displayLives);
176   }
177 
178   private void resetGame()
179   {
180     if(lives==&& getPlayer().getKeyboard().getLastKey() == 'r')
181     {
182       canvas.removeAllSprites();
183       stage=0;
184       startGame();
185     }
186   }
187 
188   private void togglePause()
189   {
190     if(getPlayer().getKeyboard().getLastKey() == 'p')
191     {
192       pauseToggle();
193     }
194   }
195 
196   private void makePaddleandBall()
197   {
198     paddle=new RectangleSprite(251);
199     paddle.setColor(Color.YELLOW);
200     paddle.setScale(.15);
201     paddle.setLocation(.5.8);
202     canvas.addSprite(paddle);
203 
204     ball=new OvalSprite(11);
205     ball.setColor(Color.RED);
206     ball.setScale(0.03);
207     ball.setLocation(.5.7);
208 
209     ball2=new OvalSprite(11);
210     ball2.setColor(Color.BLUE);
211     ball2.setScale(0.03);
212     ball2.setLocation(.5.7);
213 
214     ball3=new OvalSprite(11);
215     ball3.setColor(Color.GREEN);
216     ball3.setScale(0.03);
217     ball3.setLocation(.5.7);
218   }
219 
220   private void makeWalls()
221   {
222     leftWall=new RectangleSprite(350);
223     leftWall.setLocation(0.0150.5);
224     leftWall.setScale(1);
225     leftWall.setColor(Color.YELLOW);
226     canvas.addSprite(leftWall);
227 
228     rightWall=new RectangleSprite(350);
229     rightWall.setLocation(.9850.5);
230     rightWall.setScale(1);
231     rightWall.setColor(Color.YELLOW);
232     canvas.addSprite(rightWall);
233 
234     topWall=new RectangleSprite(503);
235     topWall.setLocation(0.50);
236     topWall.setScale(1);
237     topWall.setColor(Color.YELLOW);
238     canvas.addSprite(topWall);
239 
240     bottomWall=new RectangleSprite(501);
241     bottomWall.setLocation(0.50.99);
242     bottomWall.setScale(1);
243     bottomWall.setColor(Color.BLACK);
244     canvas.addSprite(bottomWall);
245   }
246 
247   private void makeTracker()
248   {
249     tracker=new ProjectileTracker(.1,-0.5);
250     tracker2=new ProjectileTracker(.1,-0.5);
251     tracker3=new ProjectileTracker(.1, -0.5);
252     ball.setTracker(tracker);
253     ball2.setTracker(tracker2);
254     ball3.setTracker(tracker3);
255 
256   }
257 
258   private void makeSounds()
259   {
260     sound1 =new Sound(Wiki.getMedia("Applause22.wav"));
261     sound2 =new Sound(Wiki.getMedia("Boingfinal.wav"));
262     sound3 =new Sound(Wiki.getMedia("Doh22.wav"));
263     sound4 =new Sound(Wiki.getMedia("Hithard.wav"));
264     sound5 =new Sound(Wiki.getMedia("Byebyefinal.wav"));
265     sound6 =new Sound(Wiki.getMedia("Drums.wav"));
266   }
267 
268 
269   public void advanceFrame(double timePassed)
270   {
271     double x=getPlayer().getMouse().getLocation().x;
272     paddle.setLocation(x,paddle.getLocation().y);
273     makeBounces();
274     hitsBlocks();
275     stageClear();
276     togglePause();
277     toggleSound();
278     resetGame();
279   }
280 
281 
282   private void makeOutcome()
283   {
284     if(lives==0)
285     {
286       lose=new StringSprite("You Lose! Press R to reset the game.");
287       lose.setLocation(0.50.5);
288       lose.setScale(.8);
289       lose.setColor(Color.WHITE);
290       canvas.addSprite(lose);
291       canvas.removeSprite(ball, ball2, ball3, paddle);
292       sound3.play();
293     }
294 
295     if(score==50 && stage==2)
296     {
297       canvas.removeAllSprites();
298       win=new StringSprite("You Win!");
299       win.setLocation(0.50.5);
300       win.setScale(.4);
301       win.setColor(Color.WHITE);
302       canvas.addSprite(win);
303       sound1.play();
304     }
305     if(score==50)
306     {
307       if(stage==|| stage==1)
308       {
309         clear=new StringSprite("Stage Cleared! Click to Continue.");
310         clear.setLocation(0.50.5);
311         clear.setScale(.8);
312         clear.setColor(Color.WHITE);
313         canvas.addSprite(clear);
314         sound6.play();
315         canvas.removeSprite(ball, ball2, ball3, paddle);
316 
317       }
318 
319     }
320   }
321 
322 
323 
324 
325   private void makeBounces()
326   {
327     if(ball.intersects(ball2|| ball.intersects(ball3))
328     {
329       double normal2=Sprite.getNormalVector(ball2.getShape(), ball2.getShape());
330       double normal3=Sprite.getNormalVector(ball3.getShape(), ball3.getShape());
331       tracker2.bounce(normal2);
332       tracker3.bounce(normal3);
333 
334     }
335     if(ball2.intersects(ball|| ball2.intersects(ball3))
336     {
337       double normal=Sprite.getNormalVector(ball.getShape(), ball.getShape());
338       double normal3=Sprite.getNormalVector(ball3.getShape(), ball3.getShape());
339       tracker.bounce(normal);
340       tracker3.bounce(normal3);
341 
342     }
343     if(ball3.intersects(ball2|| ball3.intersects(ball))
344     {
345       double normal=Sprite.getNormalVector(ball.getShape(), ball.getShape());
346       double normal2=Sprite.getNormalVector(ball2.getShape(), ball2.getShape());
347       tracker.bounce(normal);
348       tracker2.bounce(normal2);
349 
350     }
351 
352 
353     if(paddle.intersects(ball|| paddle.intersects(ball2|| paddle.intersects(ball3))
354     {
355       double normal=Sprite.getNormalVector(paddle.getShape(), ball.getShape());
356       double normal2=Sprite.getNormalVector(paddle.getShape(), ball2.getShape());
357       double normal3=Sprite.getNormalVector(paddle.getShape(), ball3.getShape());
358       tracker.bounce(normal);
359       tracker2.bounce(normal2);
360       tracker3.bounce(normal3);
361       sound2.play();
362 
363     }
364 
365     if(rightWall.intersects(ball|| rightWall.intersects(ball2|| rightWall.intersects(ball3))
366     {
367       double normal=Sprite.getNormalVector(rightWall.getShape(), ball.getShape());
368       double normal2=Sprite.getNormalVector(rightWall.getShape(), ball2.getShape());
369       double normal3=Sprite.getNormalVector(rightWall.getShape(), ball3.getShape());
370       tracker.bounce(normal);
371       tracker2.bounce(normal2);
372       tracker3.bounce(normal3);
373 
374     }
375 
376     if(leftWall.intersects(ball|| leftWall.intersects(ball2|| leftWall.intersects(ball3))
377     {
378       double normal=Sprite.getNormalVector(leftWall.getShape(), ball.getShape());
379       double normal2=Sprite.getNormalVector(leftWall.getShape(), ball2.getShape());
380       double normal3=Sprite.getNormalVector(leftWall.getShape(), ball3.getShape());
381       tracker.bounce(normal);
382       tracker2.bounce(normal2);
383       tracker3.bounce(normal3);
384 
385     }
386 
387     if(topWall.intersects(ball|| topWall.intersects(ball2|| topWall.intersects(ball3))
388     {
389       double normal=Sprite.getNormalVector(topWall.getShape(), ball.getShape());
390       double normal2=Sprite.getNormalVector(topWall.getShape(), ball2.getShape());
391       double normal3=Sprite.getNormalVector(topWall.getShape(), ball3.getShape());
392       tracker.bounce(normal);
393       tracker2.bounce(normal2);
394       tracker3.bounce(normal3);
395 
396     }
397 
398 
399 
400   }
401 
402   private void hitsBlocks()
403   {
404     for(int i=0; i<blocks.length; i++)
405     {
406       if(blocks[i].isVisible() && blocks[i].intersects(ball|| blocks[i].isVisible() && blocks[i].intersects(ball2|| blocks[i].isVisible() && blocks[i].intersects(ball3))
407       {
408         double normal=Sprite.getNormalVector(blocks[i].getShape(), ball.getShape());
409         double normal2=Sprite.getNormalVector(blocks[i].getShape(), ball2.getShape());
410         double normal3=Sprite.getNormalVector(blocks[i].getShape(), ball3.getShape());
411         score++;
412         displayScore.setText("Score: "+score);
413         tracker.bounce(normal);
414         tracker2.bounce(normal2);
415         tracker3.bounce(normal3);
416         blocks[i].setVisible(false);
417         makeOutcome();
418         sound4.play();
419 
420       }
421     }
422 
423     if(bottomWall.intersects(ball|| bottomWall.intersects(ball2|| bottomWall.intersects(ball3))
424     {
425       lives--;
426       displayLives.setText("Lives: "+lives);
427       if(bottomWall.intersects(ball))
428       {
429         ball.setLocation(.5,.5);
430       }
431       if(bottomWall.intersects(ball2))
432       {
433         ball2.setLocation(.5,.5);
434       }
435       if(bottomWall.intersects(ball3))
436       {
437         ball3.setLocation(.5,.5);
438       }
439       makeOutcome();
440       sound5.play();
441     }
442 
443   }
444 }

Compiler Errors:
----------
1. ERROR in NickandChris/BreakoutFinal.java (at line 54)
	private void toggleSound()
	             ^^^^^^^^^^^^^
Cannot reduce the visibility of the inherited method from GameLoop
----------
2. ERROR in NickandChris/BreakoutFinal.java (at line 188)
	private void togglePause()
	             ^^^^^^^^^^^^^
Cannot reduce the visibility of the inherited method from GameLoop
----------
2 problems (2 errors)

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