Breakout Aufgabe 1.3
This commit is contained in:
parent
c9a88e7946
commit
881f3ae97c
@ -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()));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user