diff --git a/src/break_out/model/Ball.java b/src/break_out/model/Ball.java index 0d97474..af1cbf8 100644 --- a/src/break_out/model/Ball.java +++ b/src/break_out/model/Ball.java @@ -26,7 +26,11 @@ public class Ball implements IBall{ */ public Ball() { this.position = new Position(0, 0); - this.direction = new Vector2D(0,0); + this.direction = new Vector2D(Constants.BALL_SPEED,Constants.BALL_SPEED); + + // start at bottom-center + this.position.setX(Constants.SCREEN_WIDTH/2); + this.position.setY(Constants.SCREEN_HEIGHT-3*Constants.BALL_DIAMETER); } /** @@ -49,14 +53,29 @@ public class Ball implements IBall{ * */ public void updatePosition() { - + // @rxbn, Ruben Meyer + this.position.setX(this.position.getX()+this.direction.getDx()); + this.position.setY(this.position.getY()+this.direction.getDy()); } /** * */ public void reactOnBorder() { - + + // @rxbn, Ruben Meyer + + // left border + if(this.position.getX() < 0) this.direction.setDx(-(this.direction.getDx())); + + // right border + if(this.position.getX()+Constants.BALL_DIAMETER > Constants.SCREEN_WIDTH) this.direction.setDx(-(this.direction.getDx())); + + // top border + if(this.position.getY() < 0) this.direction.setDy(-(this.direction.getDy())); + + // bottom border + if(this.position.getY()+2*Constants.BALL_DIAMETER > Constants.SCREEN_HEIGHT) this.direction.setDy(-(this.direction.getDy())); } } diff --git a/src/break_out/model/Level.java b/src/break_out/model/Level.java index 6c2f177..a8653f5 100644 --- a/src/break_out/model/Level.java +++ b/src/break_out/model/Level.java @@ -92,10 +92,10 @@ public class Level extends Thread { if (ballWasStarted) { // Call here the balls method for updating his position on the playground - + this.ball.updatePosition(); // Call here the balls method for reacting on the borders of the playground - + this.ball.reactOnBorder(); // Tells the observer to repaint the components on the playground