You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
1.8 KiB

2 years ago
  1. package playground;
  2. import java.awt.Color;
  3. import gameobjects.GameObject;
  4. public class BreakoutLevel3 extends BreakoutLevel2 {
  5. gameobjects.TextObject textPoints, textLives;
  6. /** Creates flags and TextObjects to display scores and Lives.
  7. *
  8. * @param level String
  9. */
  10. @Override
  11. public void prepareLevel(String level) {
  12. setGlobalFlag("points", 0);
  13. getOrCreateGlobalFlag("lives", 3);
  14. textPoints = new gameobjects.TextObject("textPoints", this, 50 , 10, 0, 0, "Score: 0", 10, Color.black);
  15. textLives = new gameobjects.TextObject("textLives", this, 600 , 10, 0, 0, "Lives: "+(int)getOrCreateGlobalFlag("lives", 3), 10, Color.black);
  16. this.addObject(textPoints);
  17. this.addObject(textLives);
  18. super.prepareLevel(level);
  19. }
  20. /** Sets the Score if a Brick is hitted.
  21. *
  22. * @param ball GameObject
  23. * @param brick
  24. */
  25. @Override
  26. protected void actionIfBallHitsBrick(GameObject ball, GameObject brick) {
  27. super.actionIfBallHitsBrick(ball, brick);
  28. setGlobalFlag("points", (int)(getGlobalFlag("points")) + 10);
  29. textPoints.setText("Score: "+(int)getGlobalFlag("points"));
  30. }
  31. /** If the Ball goes under the Ego, Lives are decreased and Ball is set above Ego.
  32. */
  33. @Override
  34. public void applyGameLogic() {
  35. super.applyGameLogic();
  36. if(ball.getY() > ego.getY()) {
  37. setGlobalFlag("lives", (int)(getGlobalFlag("lives")) - 1);
  38. textLives.setText("Lives: "+(int)getGlobalFlag("lives"));
  39. if(ball.getVY() > 0) {
  40. ball.setVY(ball.getVY() *- 1);
  41. }
  42. ball.setY(ego.getY()-20.0);
  43. }
  44. }
  45. /** game ends if egoLives < 0 .
  46. *
  47. * @return gameOver boolean
  48. */
  49. @Override
  50. public boolean gameOver() {
  51. if((int)getGlobalFlag("lives") < 0) {
  52. return true;
  53. }
  54. return false;
  55. }
  56. }