Aufgabe 3.2
This commit is contained in:
parent
ba286f548f
commit
0548e10e6b
@ -120,6 +120,18 @@ public class Controller implements ActionListener, KeyListener {
|
|||||||
if(!game.getLevel().ballWasStarted()) game.getLevel().startBall();
|
if(!game.getLevel().ballWasStarted()) game.getLevel().startBall();
|
||||||
else game.getLevel().stopBall();
|
else game.getLevel().stopBall();
|
||||||
break;
|
break;
|
||||||
|
// arrow facing left was pressed
|
||||||
|
case KeyEvent.VK_LEFT:
|
||||||
|
// paddle shall go left
|
||||||
|
game.getLevel().getPaddleBottom().setDirection(-1);
|
||||||
|
game.getLevel().getPaddleTop().setDirection(-1);
|
||||||
|
break;
|
||||||
|
// arrow facing right was pressed
|
||||||
|
case KeyEvent.VK_RIGHT:
|
||||||
|
// paddle shall go right
|
||||||
|
game.getLevel().getPaddleBottom().setDirection(+1);
|
||||||
|
game.getLevel().getPaddleTop().setDirection(+1);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -131,7 +143,14 @@ public class Controller implements ActionListener, KeyListener {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void keyReleased(KeyEvent e) {
|
public void keyReleased(KeyEvent e) {
|
||||||
|
switch(e.getKeyCode()) {
|
||||||
|
case KeyEvent.VK_LEFT: // arrow facing left was released
|
||||||
|
case KeyEvent.VK_RIGHT: // arrow facing right was released
|
||||||
|
// paddle shall stop
|
||||||
|
game.getLevel().getPaddleBottom().setDirection(0);
|
||||||
|
game.getLevel().getPaddleTop().setDirection(0);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -111,30 +111,32 @@ public class Level extends Thread implements ILevel {
|
|||||||
// endless loop
|
// endless loop
|
||||||
while (true) {
|
while (true) {
|
||||||
// if ballWasStarted is true, the ball is moving
|
// if ballWasStarted is true, the ball is moving
|
||||||
if (ballWasStarted) {
|
if (ballWasStarted()) {
|
||||||
|
|
||||||
// Call here the balls method for updating his position on the playground
|
// Call here the balls method for updating his position on the playground
|
||||||
this.ball.updatePosition();
|
getBall().updatePosition();
|
||||||
|
|
||||||
// Call here the balls method for reacting on the borders of the playground
|
// Call here the balls method for reacting on the borders of the playground
|
||||||
this.ball.reactOnBorder();
|
getBall().reactOnBorder();
|
||||||
|
|
||||||
// if ball hits paddle (top|bottom), reflect ball
|
// if ball hits paddle (top|bottom), reflect ball
|
||||||
if(this.ball.hitsPaddle(paddleTop)) this.ball.reflectOnPaddle(paddleTop);
|
if(getBall().hitsPaddle(paddleTop)) getBall().reflectOnPaddle(paddleTop);
|
||||||
if(this.ball.hitsPaddle(paddleBottom)) this.ball.reflectOnPaddle(paddleBottom);
|
if(getBall().hitsPaddle(paddleBottom)) getBall().reflectOnPaddle(paddleBottom);
|
||||||
if(this.ball.hitsPaddle(paddleTop) || this.ball.hitsPaddle(paddleBottom)) {
|
if(getBall().hitsPaddle(paddleTop) || getBall().hitsPaddle(paddleBottom)) {
|
||||||
this.ball.setHitState(true);
|
getBall().setHitState(true);
|
||||||
// DEBUG OUTPUT
|
// DEBUG OUTPUT
|
||||||
//System.out.println(String.format("hitstate: %s, color: %s", this.ball.getHitState(), this.ball.getColor().getRGB()));
|
//System.out.println(String.format("hitstate: %s, color: %s", this.ball.getHitState(), this.ball.getColor().getRGB()));
|
||||||
}
|
}
|
||||||
|
|
||||||
// if ball has hit a paddle set a new random color to it
|
// if ball has hit a paddle set a new random color to it
|
||||||
else if(this.ball.getHitState()) {
|
else if(getBall().getHitState()) {
|
||||||
this.ball.setHitState(false);
|
getBall().setHitState(false);
|
||||||
this.ball.newRandomColor();
|
getBall().newRandomColor();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// update paddles position
|
||||||
|
getPaddleTop().updatePosition(getBall());
|
||||||
|
getPaddleBottom().updatePosition(getBall());
|
||||||
|
|
||||||
|
|
||||||
// Tells the observer to repaint the components on the playground
|
// Tells the observer to repaint the components on the playground
|
||||||
|
@ -16,6 +16,12 @@ public class Paddle implements IPaddle {
|
|||||||
*/
|
*/
|
||||||
private Position position;
|
private Position position;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The paddles direction on the playground
|
||||||
|
* only -1,0,+1 are valid values
|
||||||
|
*/
|
||||||
|
private int direction;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The paddles sizing
|
* The paddles sizing
|
||||||
*/
|
*/
|
||||||
@ -121,7 +127,7 @@ public class Paddle implements IPaddle {
|
|||||||
* @return direction The paddles current direction
|
* @return direction The paddles current direction
|
||||||
*/
|
*/
|
||||||
public int getDirection() {
|
public int getDirection() {
|
||||||
return 0;
|
return direction;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -130,7 +136,11 @@ public class Paddle implements IPaddle {
|
|||||||
* @param direction The paddles new direction
|
* @param direction The paddles new direction
|
||||||
*/
|
*/
|
||||||
public void setDirection(int direction) {
|
public void setDirection(int direction) {
|
||||||
|
// normalize to valid values
|
||||||
|
if(direction > 0) direction = 1;
|
||||||
|
if(direction < 0) direction = -1;
|
||||||
|
|
||||||
|
this.direction = direction;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -139,6 +149,22 @@ public class Paddle implements IPaddle {
|
|||||||
* @param ball The ball
|
* @param ball The ball
|
||||||
*/
|
*/
|
||||||
public void updatePosition(Ball ball) {
|
public void updatePosition(Ball ball) {
|
||||||
|
// move paddle, if it is the nearest paddle
|
||||||
|
if(ball.getPosition().getY() < Constants.SCREEN_HEIGHT/2.0 && getPosition().getY() < Constants.SCREEN_HEIGHT/2.0 ||
|
||||||
|
ball.getPosition().getY() > Constants.SCREEN_HEIGHT/2.0 && getPosition().getY() > Constants.SCREEN_HEIGHT/2.0) {
|
||||||
|
|
||||||
|
// temporary position to modify x value
|
||||||
|
Position tmp = getPosition();
|
||||||
|
|
||||||
|
// modify x value; getDirection is normalized to the values {-1, 0, 1}
|
||||||
|
// therefore we can just multiply it with DX_MOVEMENT
|
||||||
|
tmp.setX(getPosition().getX() + getDirection()*Constants.DX_MOVEMENT);
|
||||||
|
|
||||||
|
// out of border boundaries
|
||||||
|
if(tmp.getX() <= 0) tmp.setX(0);
|
||||||
|
if(tmp.getX() >= Constants.SCREEN_WIDTH-getWidth()) tmp.setX(Constants.SCREEN_WIDTH-getWidth());
|
||||||
|
|
||||||
|
setPosition(tmp);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user