3 Commits
bfa742d479
...
83c29d5fc1
Author | SHA1 | Message | Date |
---|---|---|---|
Nico B | 83c29d5fc1 |
finished HA09
|
2 years ago |
Nico B | 47e9999a89 |
k
|
2 years ago |
jkonert | aa2436e4a4 |
added file base for HA09 (creating points and game over etc.)
|
2 years ago |
3 changed files with 182 additions and 1 deletions
-
6GameProject/src/base/BreakoutGame.java
-
119GameProject/src/playground/BreakoutLevel0.java
-
58GameProject/src/playground/BreakoutLevel3.java
@ -0,0 +1,119 @@ |
|||||
|
package playground; |
||||
|
|
||||
|
import java.awt.Color; |
||||
|
import org.apache.logging.log4j.LogManager; |
||||
|
import org.apache.logging.log4j.Logger; |
||||
|
import collider.RectCollider; |
||||
|
import controller.EgoController; |
||||
|
import controller.ReboundController2; |
||||
|
import gameobjects.FallingStar; |
||||
|
import gameobjects.GameObject; |
||||
|
import gameobjects.RectObject; |
||||
|
|
||||
|
/** |
||||
|
* simple solution example for Breakout game. |
||||
|
* |
||||
|
* not implemented: penalty if balls hits ground, no score for removed bricks, no bonus items, no |
||||
|
* lives. |
||||
|
* </ul> |
||||
|
* |
||||
|
*/ |
||||
|
|
||||
|
|
||||
|
public class BreakoutLevel0 extends BreakoutLevelBase { |
||||
|
|
||||
|
private static Logger logger = LogManager.getLogger(BreakoutLevelBase.class); |
||||
|
|
||||
|
|
||||
|
protected GameObject createEgoObject() { |
||||
|
RectObject ro = new RectObject("ego", this, 350, 550, 0, 0, 80, 10, Color.BLUE); |
||||
|
ro.generateColliders(); |
||||
|
EgoController ec = new EgoController(80,10); |
||||
|
ro.addController(ec); |
||||
|
logger.info("ego created."); |
||||
|
|
||||
|
return ro; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
protected GameObject createBall() { |
||||
|
GameObject co = new FallingStar("ball1", this, 350, 350, 100, 100, Color.RED, 5); |
||||
|
co.addController(new ReboundController2()); |
||||
|
logger.info("ball created."); |
||||
|
return co; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* creates one brick. For |
||||
|
* collision {@link RectCollider} is used. |
||||
|
*/ |
||||
|
@Override |
||||
|
protected GameObject createBrick(int row, int column) { |
||||
|
double xSize = 60; |
||||
|
double ySize = 30; |
||||
|
double xStart = 40; |
||||
|
double yStart = 40; |
||||
|
double space = 5; |
||||
|
Color c = Color.BLUE; |
||||
|
|
||||
|
RectObject ro = new RectObject("brick" + row + "/" + column, this, xStart + column * (xSize + space), |
||||
|
yStart + row * (ySize + space), 0, 0, xSize - 4, ySize - 4, c); |
||||
|
RectCollider rc = new RectCollider("egal", ro, xSize - 4, ySize - 4); |
||||
|
ro.addCollider(rc); |
||||
|
|
||||
|
return ro; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* lets ball bounce in Y direction, deletes brick. |
||||
|
*/ |
||||
|
@Override |
||||
|
protected void actionIfBallHitsBrick(GameObject ball, GameObject brick) { |
||||
|
|
||||
|
ball.setVY(ball.getVY() * -1); // bounce effect for ball |
||||
|
this.deleteObject(brick.getId()); // remove brick from field |
||||
|
logger.debug("deleted brick " + brick.getId()); |
||||
|
|
||||
|
} |
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* Let the ball bounce off in Y direction. |
||||
|
* |
||||
|
*/ |
||||
|
@Override |
||||
|
protected void actionIfBallHitsEgo(GameObject ball, GameObject ego) { |
||||
|
double ballY = ball.getY(); |
||||
|
double egoY = ego.getY(); |
||||
|
ball.setY(ballY < egoY ? ballY - 10 : ballY + 10); // radius 5 hard coded. |
||||
|
ball.setVY(ball.getVY() * -1); |
||||
|
logger.debug("ball bounces of ego object."); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* Prepares a Breakout level with a 3 x 3 matrix of blocks on top. This method relies on the |
||||
|
* methods {@link #createEgo}, {@link #createBall} and {@link #createBrick}, among others. |
||||
|
* |
||||
|
* @param level String passes by the game engine (not used currently and can be ignored). |
||||
|
*/ |
||||
|
@Override |
||||
|
public void prepareLevel(String level) { |
||||
|
for (int y = 3; y < 6; y++) { |
||||
|
for (int x = 0; x < 3; x++) { |
||||
|
logger.trace("trying to create brick X, Y (" + x + "," + y + ")"); |
||||
|
GameObject brick = this.createBrick(x, y); |
||||
|
this.addObject(brick); |
||||
|
} |
||||
|
} |
||||
|
this.ego = this.createEgoObject(); |
||||
|
this.ball = this.createBall(); |
||||
|
this.addObject(this.ego); |
||||
|
this.addObject(this.ball); |
||||
|
logger.info("level preperation succeeded."); |
||||
|
|
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
@ -0,0 +1,58 @@ |
|||||
|
package playground; |
||||
|
|
||||
|
import java.awt.Color; |
||||
|
|
||||
|
import gameobjects.GameObject; |
||||
|
import gameobjects.TextObject; |
||||
|
|
||||
|
public class BreakoutLevel3 extends BreakoutLevel0 { |
||||
|
|
||||
|
TextObject pointsText = new TextObject("pointsText", this, 50, 10, 0.,0, "0", 20, Color.BLACK); |
||||
|
TextObject livesText = new TextObject("livesText", this, 600,10,0,0,"3", 20, Color.GREEN); |
||||
|
|
||||
|
@Override |
||||
|
public void prepareLevel(String id) { |
||||
|
setLevelFlag("points", 0); |
||||
|
TextObject score = pointsText; |
||||
|
this.addObject(score); |
||||
|
setGlobalFlag("lives", 3); |
||||
|
TextObject lives = livesText; |
||||
|
this.addObject(lives); |
||||
|
super.prepareLevel(id); |
||||
|
|
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected void actionIfBallHitsBrick(GameObject ball, GameObject brick) { |
||||
|
super.actionIfBallHitsBrick(ball, brick); |
||||
|
int value = (int) getLevelFlag("points"); |
||||
|
setLevelFlag("points", value + 10); |
||||
|
pointsText.setText("" + getLevelFlag("points")); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void applyGameLogic (){ |
||||
|
super.applyGameLogic(); |
||||
|
int value = (int) getGlobalFlag("lives"); |
||||
|
if(this.ball.getY() > this.ego.getY()) { |
||||
|
setGlobalFlag("lives", --value); |
||||
|
livesText.setText("" + value); |
||||
|
this.ball.setY(this.ego.getY() - 20); |
||||
|
this.ball.setX(this.ego.getX()); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
||||
|
@Override |
||||
|
public boolean gameOver() { |
||||
|
int value = (int) getGlobalFlag("lives"); |
||||
|
if (value <0) { |
||||
|
return true; |
||||
|
} |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
||||
|
|
||||
|
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue