Jayson/breakout/Breakout

From ggc

Jump to: navigation, search

001 package Jayson.breakout;
002 //package breakout;
003 
004 import wiki.Wiki;
005 import fang.*;
006 import java.awt.*;
007 import java.awt.geom.*;
008 import java.util.ArrayList;
009 
010 /**
011  * Classic Breakout with a twist.
012  @author Jayson Park
013  */
014 public class Breakout extends GameLoop
015 {
016   /**Array of bricks for the game to create*/
017   private Sprite[] bricks;
018   /**Array for the brick colors*/
019   private Color[] brickColor;
020   /**Number of Bricks in the X and Y directions*/
021   private int numBricksX, numBricksY;
022   /**Starting X and Y Coordinates*/
023   private double startYCoord, startXCoord;
024   /**Size of the bricks.*/
025   private double brickWidth, brickHeight;
026   /**Coordinates for the current brick*/
027   private double yCoord, xCoord;
028   /**Sprites for the boundaries.*/
029   private Sprite leftWall, rightWall, topCeiling;
030   /**Array for the areas where the ball calls death()*/
031   private Sprite[] deadArea = new Sprite[4];
032   /**Sprites for the paddle*/
033   private Sprite paddle;
034   /**Array for balls*/
035   private ArrayList<Sprite> ball = new ArrayList<Sprite>();
036   /**Sprites for the score, reset text, and number of lives*/
037   private StringSprite scoreText, resetText, livesText;
038   /**Score and lives values*/
039   private int score, lives;
040   /**Chance for a power up to drop*/
041   private int powerChance;
042   /**Game over status*/
043   private boolean gameOver = false;
044   /**Tracker for the ball*/
045   private ArrayList<ProjectileTracker> ballTracker = new ArrayList<ProjectileTracker>();
046   /**Double value for the normal value to bounce.*/
047   private double normal;
048   /**Amount of bricks left.*/
049   private int brickAmount;
050   /**Frame to add all Sprites for rotating frame*/
051   private SpriteFrame frame = new SpriteFrame();
052   /**Array containing all of the power ups*/
053   private Sprite[] powerUps = new Sprite[7];
054   /**Tracker for the power ups to fall*/
055   private ProjectileTracker powerUpTracker = new ProjectileTracker(.250);
056   /**So the rotation would work for the directions*/
057   private Sprite[] marker = new Sprite[2];
058   /**Toggle for the breakOut power up*/
059   private boolean breakoutToggle = false;
060   /**Length of the ball to perserve the ball array length*/
061   private int lengthBall = 1;
062 
063   /**sets up the game*/
064   public void startGame()
065   {
066     makeSprites();
067     setHelpText("<h1>Control the paddle</h1> Hit the bricks with the ball.");
068   }
069 
070   /**Temporary main method for the IDE to work*/
071   public static void main(String[] args)
072   {
073     //Constructor
074     Breakout breakout = new Breakout();
075     //Special method to fix some errors.
076     breakout.runAsApplication();
077   }
078 
079   /**makes the sprites*/
080   private void makeSprites()
081   {
082     levelOneAssign();
083     makeBrickColors();
084     makeBricks();
085     makePowerUps();
086     makePaddle();
087     makeBall(0);
088     makeScoreText();
089     makeLivesText();
090     makeLevelBoundaries();
091   }
092 
093   /**Sets the number/size of bricks
094    * and the Starting Coordinates for level One.*/
095   public void levelOneAssign()
096   {
097     lives = 5;
098     score = 0;
099     powerChance = 5;
100     lengthBall = 1;
101     numBricksX = 16;
102     numBricksY = 8;
103     startYCoord = .4;
104     startXCoord = .1;
105     brickWidth = (* startXCoord(numBricksX - 1);
106     brickHeight = brickWidth / 2.5;
107   }
108 
109   /**Creates colors for the bricks*/
110   public void makeBrickColors()
111   {
112     brickColor = new Color[numBricksY];
113     for (int colorInt = 0; colorInt < numBricksY; colorInt++)
114     {
115       brickColor[colorInt] = colorOrder(colorInt);
116     }
117   }
118 
119   /**Sets the order of which color is the strongest*/
120   public Color colorOrder(int colorNum)
121   {
122     Color colorReturn;
123     switch (colorNum)
124     {
125     case 0:
126       colorReturn = Color.blue;
127       break;
128     case 1:
129       colorReturn = Color.red;
130       break;
131     case 2:
132       colorReturn = Color.yellow;
133       break;
134     case 3:
135       colorReturn = Color.cyan;
136       break;
137     case 4:
138       colorReturn = Color.green;
139       break;
140     case 5:
141       colorReturn = Color.white;
142       break;
143     default:
144       colorReturn = Color.darkGray;
145     }
146     return (colorReturn);
147   }
148 
149   /**Creates the bricks*/
150   public void makeBricks()
151   {
152     bricks = new Sprite[numBricksX * numBricksY];
153     for (int = 0; i < numBricksY; i++)
154     {
155       yCoord = startYCoord - (i * brickHeight);
156       for (int = 0; a < numBricksX; a++)
157       {
158         xCoord = startXCoord + a * brickWidth;
159         bricks[(i * numBricksX+
160                a] = new RectangleSprite(brickWidth, brickHeight);
161         bricks[(i * numBricksX+ a].setColor(brickColor[i]);
162         bricks[(i * numBricksX+ a].setLocation(xCoord, yCoord);
163         bricks[(i * numBricksX+ a].setScale(0.05);
164         canvas.addSprite(bricks[(i * numBricksX+ a]);
165         frame.addSprite(bricks[(i * numBricksX+ a]);
166       }
167     }
168     brickAmount = bricks.length;
169   }
170 
171   /**Creates the power ups*/
172   public void makePowerUps()
173   {
174     for (int powerUpInt = 0; powerUpInt < powerUps.length; powerUpInt++)
175     {
176       powerUps[powerUpInt] = new OvalSprite(11);
177       powerUps[powerUpInt].setColor(brickColor[powerUpInt]);
178       powerUps[powerUpInt].setScale(.025);
179     }
180   }
181 
182   /**Makes the sides and the ceiling*/
183   public void makeLevelBoundaries()
184   {
185     makeDeadArea();
186     makeTopCeiling();
187     makeLeftWall();
188     makeRightWall();
189   }
190 
191   /**Creates the bottom boundary.*/
192   public void makeDeadArea()
193   {
194     deadArea[0] = new RectangleSprite(201);
195     deadArea[1] = new RectangleSprite(120);
196     deadArea[2] = new RectangleSprite(201);
197     deadArea[3] = new RectangleSprite(120);
198     deadArea[0].setLocation(.51.1);
199     deadArea[1].setLocation-.1.5);
200     deadArea[2].setLocation(.5, -.1);
201     deadArea[3].setLocation(1.1.5);
202     for (int areaInt = 0; areaInt < deadArea.length; areaInt++)
203     {
204       deadArea[areaInt].setScale(2);
205       deadArea[areaInt].setColor(Color.red);
206       canvas.addSprite(deadArea[areaInt]);
207       frame.addSprite(deadArea[areaInt]);
208     }
209   }
210 
211   /**Creates the top boundary.*/
212   public void makeTopCeiling()
213   {
214     topCeiling = new OvalSprite(10.5);
215     topCeiling.setLocation(.5.15);
216     topCeiling.setScale(2);
217     canvas.addSprite(topCeiling);
218     frame.addSprite(topCeiling);
219   }
220 
221   /**Creates the left boundary.*/
222   public void makeLeftWall()
223   {
224     leftWall = new OvalSprite(112);
225     leftWall.setLocation(startXCoord / 4.5.5);
226     leftWall.setScale(1.2);
227     canvas.addSprite(leftWall);
228     frame.addSprite(leftWall);
229   }
230 
231   /**Creates the right boundary.*/
232   public void makeRightWall()
233   {
234     rightWall = new OvalSprite(112);
235     rightWall.setLocation(- startXCoord / 4.5.5);
236     rightWall.setScale(1.2);
237     canvas.addSprite(rightWall);
238     frame.addSprite(rightWall);
239   }
240 
241   /**Makes the paddle*/
242   public void makePaddle()
243   {
244     paddle = new Sprite(new java.awt.geom.RoundRectangle2D.Double(00511,
245                         1));
246     //paddle.visible();
247     //paddle.setShape(new java.awt.geom.RoundRectangle2D.Double(0, 0, 5, 1, 1,
248     //                1));
249     paddle.setLocation(.5.8);
250     paddle.setScale(.2);
251     canvas.addSprite(paddle);
252     frame.addSprite(paddle);
253     makeMarker();
254   }
255 
256   /**Creates the marker for the rotation*/
257   public void makeMarker()
258   {
259     marker[0] = new RectangleSprite(11);
260     marker[0].setLocation(paddle.getLocation());
261     marker[1] = new RectangleSprite(11);
262     marker[1].setLocation(.5.1);
263     for (int markerInt = 0; markerInt < marker.length; markerInt++)
264     {
265       marker[markerInt].setVisible(false);
266       canvas.addSprite(marker[markerInt]);
267       frame.addSprite(marker[markerInt]);
268     }
269   }
270 
271   /**Makes the ball*/
272   public void makeBall(int ballInt)
273   {
274     while (ball.size() <= ballInt)
275     {
276       ball.add(new OvalSprite(11));
277       ball.get(ballInt).setLocation(.5.6);
278       ball.get(ballInt).setScale(.025);
279       makeBallTracker(ballInt);
280       canvas.addSprite(ball.get(ballInt));
281     }
282   }
283 
284   /**Creates the score portion of the text.*/
285   public void makeScoreText()
286   {
287     canvas.removeSprite(scoreText);
288     scoreText = new StringSprite("Score: " + score);
289     scoreText.setLocation(.7.05);
290     scoreText.setColor(Color.WHITE);
291     scoreText.setScale(0.2);
292     canvas.addSprite(scoreText);
293     frame.addSprite(scoreText);
294   }
295 
296   /**Creates the lives portion of the text.*/
297   public void makeLivesText()
298   {
299     canvas.removeSprite(livesText);
300     livesText = new StringSprite("Lives: " + lives);
301     livesText.setLocation(0.3.05);
302     livesText.setColor(Color.WHITE);
303     livesText.setScale(0.2);
304     canvas.addSprite(livesText);
305     frame.addSprite(livesText);
306   }
307 
308   /**Creates the tracker for the ball*/
309   public void makeBallTracker(int ballInt)
310   {
311     if (ball.get(ballInt).getTracker() == null && ball.get(ballInt!= null)
312     {
313       ballTracker.add(new ProjectileTracker(0.1, -.25));
314       ball.get(ballInt).setTracker(ballTracker.get(ballInt));
315     }
316   }
317 
318   /**Removes all Sprites then remakes the game.*/
319   public void resetGame()
320   {
321     frame.setRotation(0.00);
322     canvas.removeAllSprites();
323     ball.removeAll(ball);
324     ballTracker.removeAll(ballTracker);
325     lengthBall = 1;
326     makeSprites();
327   }
328 
329   /**Color Change for boundaries every frame*/
330   public void randomColorBound()
331   {
332     leftWall.setColor(new Color(random.nextInt()));
333     rightWall.setColor(leftWall.getColor());
334     topCeiling.setColor(leftWall.getColor());
335     ball.get(0).setColor(new Color(random.nextInt()));
336   }
337 
338   /**Sets the resetText if game over.*/
339   public void makeResetText()
340   {
341     resetText = new StringSprite(
342                     "Game Over\nPress r to reset the game.");
343     resetText.setScale(0.4);
344     resetText.setColor(Color.WHITE);
345     resetText.setLocation(.5.5);
346     canvas.addSprite(resetText);
347     gameOver = true;
348   }
349 
350   /**Watches if the player presses r to reset after game over.*/
351   public void resetWatch()
352   {
353     if (gameOver == true && getPlayer().getKeyboard().getLastKey() == 'r')
354     {
355       gameOver = false;
356       resetGame();
357       lives = 5;
358     }
359   }
360 
361   /**If ball goes under the paddle, lose a life and reset the ball's location*/
362   public void death()
363   {
364     for (int areaInt = 0; areaInt < deadArea.length; areaInt++)
365     {
366       for (int ballInt = 0; ballInt < ball.size(); ballInt++)
367       {
368         if (ball.get(ballInt).intersects(deadArea[areaInt]&& gameOver == false)
369         {
370           canvas.removeSprite(ball.get(ballInt));
371           for (int powerInt = 0; powerInt < powerUps.length; powerInt++)
372           {
373             canvas.removeSprite(powerUps[powerInt]);
374           }
375         }
376       }
377       if (ball.get(0).intersects(deadArea[areaInt]&& gameOver == false)
378       {
379         resetBall();
380         breakoutToggle = false;
381         lives--;
382         livesText.setText("Lives: " + lives);
383       }
384 
385     }
386   }
387 
388   /**Resets the ball location when it dies*/
389   public void resetBall()
390   {
391     if (lives > 1)
392     {
393       ball.get(0).setLocation(.5.5);
394       ball.removeAll(ball);
395       ballTracker.removeAll(ballTracker);
396       lengthBall = 1;
397       makeBall(0);
398     }
399     else
400     {
401       makeResetText();
402     }
403   }
404 
405   /**Sends the ball and the surface to ballBounce()*/
406   public void boundaryBounce()
407   {
408     for (int ballInt = 0; ballInt < ball.size(); ballInt++)
409     {
410       if (ball.get(ballInt).intersects(paddle))
411       {
412         ballBounce(paddle, ballInt);
413       }
414       if (ball.get(ballInt).intersects(topCeiling))
415       {
416         ballBounce(topCeiling, ballInt);
417         //avoidThrough(topCeiling, ballInt);
418       }
419       if (ball.get(ballInt).intersects(leftWall))
420       {
421         ballBounce(leftWall, ballInt);
422         //avoidThrough(leftWall, ballInt);
423       }
424       if (ball.get(ballInt).intersects(rightWall))
425       {
426         ballBounce(rightWall, ballInt);
427         //avoidThrough(rightWall, ballInt);
428       }
429     }
430   }
431 
432   /**A hack so it doesn't go through the walls*/
433   public void avoidThrough(Sprite bound, int ballInt)
434   {
435     while (ball.get(ballInt).intersects(bound&&
436             bound != null)
437     {
438       ballTracker.get(ballInt).advanceTime(0.001);
439     }
440   }
441 
442   /**When ball hits a brick, add score, remove brick, bounce ball*/
443   public void brickBounce()
444   {
445     for (int brickInt = 0; brickInt < bricks.length; brickInt++)
446     {
447       for (int ballInt = 0; ballInt < ball.size(); ballInt++)
448       {
449         if (ball.get(ballInt).intersects(bricks[brickInt]&&
450                 bricks[brickInt] != null)
451         {
452           score++;
453           scoreText.setText("Score: " + score);
454           if (breakoutToggle == false)
455           {
456             ballBounce(bricks[brickInt], ballInt);
457           }
458           brickColorChange(brickInt);
459         }
460       }
461     }
462   }
463 
464   /**Changes the color of the brick to one less.  If blue, remove brick.*/
465   public void brickColorChange(int brickInt)
466   {
467     if (bricks[brickInt].getColor() == brickColor[0] &&
468             bricks[brickInt].isVisible())
469     {
470       powerCreate(random.nextInt(powerChance), brickInt);
471       canvas.removeSprite(bricks[brickInt]);
472       brickAmount--;
473       bricks[brickInt] = new RectangleSprite(00);
474     }
475     else
476     {
477       for (int colorHit = 0; colorHit < numBricksY; colorHit++)
478       {
479         if (bricks[brickInt].getColor() == brickColor[colorHit])
480         {
481           bricks[brickInt].setColor(brickColor[colorHit - 1]);
482         }
483       }
484     }
485   }
486 
487   /**Creates the power up based on which number is selected*/
488   public void powerCreate(int chance, int brickInt)
489   {
490     int powerNum = random.nextInt(powerUps.length);
491     if (chance + == powerChance)
492     {
493       powerUps[powerNum].setTracker(powerUpTracker);
494       powerUps[powerNum].setLocation(bricks[brickInt].getLocation());
495       canvas.addSprite(powerUps[powerNum]);
496     }
497   }
498 
499   /**Sets it so the ball bounces when it hits the paddle or boundary*/
500   public void ballBounce(Sprite surface, int ballInt)
501   {
502     normal = Sprite.getNormalVector(surface.getShape(),
503                                     ball.get(ballInt).getShape());
504     ballTracker.get(ballInt).bounce(normal);
505   }
506 
507   /**If player wins, game is over*/
508   public void win()
509   {
510     if (brickAmount == 0)
511     {
512       gameOver = true;
513       makeResetText();
514       for (int ballInt = 0; ballInt < ball.size(); ballInt++)
515       {
516         ball.get(ballInt).setVisible(false);
517       }
518     }
519   }
520 
521   /**Watches if the paddle touches the power up*/
522   public void watchPower()
523   {
524     for (int powerInt = 0; powerInt < powerUps.length; powerInt++)
525     {
526       if (paddle.intersects(powerUps[powerInt]&&
527               powerUps[powerInt].isVisible())
528       {
529         canvas.removeSprite(powerUps[powerInt]);
530         powerCase(powerInt);
531       }
532       if (deadArea[0].intersects(powerUps[powerInt]))
533       {
534         canvas.removeSprite(powerUps[powerInt]);
535       }
536     }
537   }
538 
539   /**All of the cases for which power up is picked up*/
540   public void powerCase(int powerInt)
541   {
542     ArrayList<Point2D.Double> powerSpeed = new ArrayList<Point2D.Double>();
543     for (int speedInt = 0; speedInt < ballTracker.size(); speedInt++)
544     {
545       powerSpeed.add(ballTracker.get(speedInt).getVelocity());
546       switch (powerInt)
547       {
548       case 0:
549         increaseSpeed(powerSpeed.get(speedInt), speedInt);
550         break;
551       case 1:
552         decreaseSpeed(powerSpeed.get(speedInt), speedInt);
553         break;
554       case 2:
555         multiBall();
556         break;
557       case 3:
558         breakOut();
559         break;
560       case 4:
561         wider();
562         break;
563       case 5:
564         increaseLife();
565         break;
566       case 6:
567         increaseScore();
568         break;
569       }
570     }
571 
572   }
573 
574   /**Increases the speed of the ball*/
575   public void increaseSpeed(Point2D.Double powerSpeed, int speedInt)
576   {
577     canvas.removeSprite(powerUps[0]);
578     powerUps[0].setLocation(00);
579     powerUps[0].setTracker(null);
580     powerSpeed.x += .05;
581     powerSpeed.y += .05;
582     ballTracker.get(speedInt).setVelocity(powerSpeed);
583   }
584 
585   /**Dereases the speed of the ball*/
586   public void decreaseSpeed(Point2D.Double powerSpeed, int speedInt)
587   {
588     canvas.removeSprite(powerUps[1]);
589     powerUps[1].setLocation(00);
590     powerUps[1].setTracker(null);
591     powerSpeed.x -= .05;
592     powerSpeed.y -= .05;
593     ballTracker.get(speedInt).setVelocity(powerSpeed);
594   }
595 
596   /**Increases the number of balls in play*/
597   public void multiBall()
598   {
599     if (powerUps[2] != null && powerUps[2].isVisible())
600     {
601       canvas.removeSprite(powerUps[2]);
602       powerUps[2].setLocation(00);
603       powerUps[2].setTracker(null);
604       powerUps[2] = new Sprite();
605       lengthBall = 2;
606       makeBall(lengthBall - 1);
607     }
608   }
609 
610   /**Increases the width of the paddle*/
611   public void wider()
612   {
613     canvas.removeSprite(paddle);
614     powerUps[4].setLocation(00);
615     powerUps[4].setTracker(null);
616     canvas.removeSprite(powerUps[4]);
617     paddle = new Sprite(new java.awt.geom.RoundRectangle2D.Double(00101,
618                         11));
619     paddle.setRotation(frame.getRotation());
620     //paddle.setShape(new java.awt.geom.RoundRectangle2D.Double(0, 0, 10, 1,
621     //                1, 1));
622     paddle.setScale(.4);
623     paddle.setLocation(paddle.getLocation());
624     canvas.addSprite(paddle);
625     frame.addSprite(paddle);
626   }
627 
628   /**Increases the number of lives*/
629   public void increaseLife()
630   {
631     lives++;
632     livesText.setText("Lives: " + lives);
633     canvas.removeSprite(powerUps[5]);
634     powerUps[5].setLocation(00);
635     powerUps[5].setTracker(null);
636   }
637 
638   /**Increases the score*/
639   public void increaseScore()
640   {
641     score += 25;
642     scoreText.setText("Score: " + score);
643     canvas.removeSprite(powerUps[6]);
644     powerUps[6].setLocation(00);
645     powerUps[6].setTracker(null);
646   }
647 
648   /**Allows the ball to breakthrough bricks instead of bouncing*/
649   public void breakOut()
650   {
651     canvas.removeSprite(powerUps[3]);
652     powerUps[0].setLocation(00);
653     powerUps[0].setTracker(null);
654     breakoutToggle = true;
655     scheduleRelative(new BreakoutToggle(breakoutToggle)30);
656   }
657 
658   /**Controls the velocity of the ball*/
659   public void velocityControl(double timePassed)
660   {
661     for (int trackInt = 0; trackInt < ballTracker.size(); trackInt++)
662     {
663       Point2D.Double ballV[] = new Point2D.Double[ballTracker.size()];
664       ballV[trackInt] = ballTracker.get(trackInt).getVelocity();
665       ballV[trackInt].y += .1 * timePassed;
666       ballTracker.get(trackInt).setVelocity(ballV[trackInt]);
667     }
668   }
669 
670   /**Slowly rotates the frame*/
671   public void frameRotate()
672   {
673     frame.setCenter(.5.5);
674     frame.rotate(0.01);
675   }
676 
677   /**handle input and game events*/
678   public void advanceFrame(double timePassed)
679   {
680     resetWatch();
681     death();
682     powerUpTracker.setVelocityDirection(frame.getRotation() + 3.14 2);
683     if (gameOver == false)
684     {
685       Point2D.Double mouseLoc = getPlayer().getMouse().getLocation();
686       paddle.setLocation(( -.5 + mouseLoc.getX()) *
687                          Math.cos(frame.getRotation()) +
688                          marker[0].getLocation().x,
689                          -.5 + mouseLoc.getX()) *
690                          Math.sin(frame.getRotation()) +
691                          marker[0].getLocation().y);
692       velocityControl(timePassed);
693       frameRotate();
694       randomColorBound();
695       watchPower();
696       boundaryBounce();
697       brickBounce();
698       win();
699     }
700   }
701 
702   /**Class to create and Image a specified time after a click.*/
703   private class BreakoutToggle implements Alarm
704   {
705     private boolean breakoutToggle;
706     public BreakoutToggle(boolean toggle)
707     {
708       breakoutToggle = toggle;
709     }
710 
711     public void alarm()
712     {
713       breakoutToggle = false;
714     }
715   }
716 
717 }


Download/View Jayson/breakout/Breakout.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