1
0
Fork 0

Breakout Aufgabe 1 - Fehlerkorrektur

This commit is contained in:
rxbn 2019-11-26 23:45:59 +01:00
parent 3d74d9e5e0
commit 10ec639943
3 changed files with 33 additions and 16 deletions

View File

@ -31,8 +31,8 @@ public class Ball implements IBall{
this.direction.rescale();
// start at bottom-center
this.position.setX(Constants.SCREEN_WIDTH/2);
this.position.setY(Constants.SCREEN_HEIGHT-3*Constants.BALL_DIAMETER);
this.position.setX((Constants.SCREEN_WIDTH - Constants.BALL_DIAMETER)/2);
this.position.setY(Constants.SCREEN_HEIGHT-Constants.BALL_DIAMETER);
}
/**
@ -67,16 +67,28 @@ public class Ball implements IBall{
*/
public void reactOnBorder() {
// reacts on left border
if(this.position.getX() < 0) this.direction.setDx(-(this.direction.getDx()));
if(this.position.getX() <= 0) {
this.position.setX(0);
this.direction.setDx(-(this.direction.getDx()));
}
// reacts on right border (+Diameter because of hitbox)
if(this.position.getX()+Constants.BALL_DIAMETER > Constants.SCREEN_WIDTH) this.direction.setDx(-(this.direction.getDx()));
// reacts on right border (-Diameter because of hitbox)
if(this.position.getX() >= Constants.SCREEN_WIDTH - Constants.BALL_DIAMETER) {
this.position.setX(Constants.SCREEN_WIDTH - Constants.BALL_DIAMETER);
this.direction.setDx(-(this.direction.getDx()));
}
// reacts on top border
if(this.position.getY() < 0) this.direction.setDy(-(this.direction.getDy()));
if(this.position.getY() <= 0) {
this.position.setY(0);
this.direction.setDy(-(this.direction.getDy()));
}
// reacts on bottom border (+Diameter because of hitbox)
if(this.position.getY()+Constants.BALL_DIAMETER > Constants.SCREEN_HEIGHT) this.direction.setDy(-(this.direction.getDy()));
if(this.position.getY() >= Constants.SCREEN_HEIGHT - Constants.BALL_DIAMETER) {
this.position.setY(Constants.SCREEN_HEIGHT - Constants.BALL_DIAMETER);
this.direction.setDy(-(this.direction.getDy()));
}
}
}

View File

@ -73,8 +73,8 @@ public class Vector2D implements IVector2D {
*/
public void rescale() {
// calc unit vector and set it
double vectorlength = Math.sqrt(Math.pow(getDx(), 2) + Math.pow(getDy(), 2)); //using the square root of x and y
setDx((1/vectorlength) * getDx() * Constants.BALL_SPEED);
setDy((1/vectorlength) * getDy() * Constants.BALL_SPEED);
double vectorLength = Math.sqrt(Math.pow(getDx(), 2) + Math.pow(getDy(), 2)); //using the square root of x and y
setDx((1/vectorLength) * getDx() * Constants.BALL_SPEED);
setDy((1/vectorLength) * getDy() * Constants.BALL_SPEED);
}
}

View File

@ -119,14 +119,19 @@ public class Field extends JPanel {
* @param g2 The graphics object
*/
private void drawGrid(Graphics2D g2) {
// vertical lines
for(int i = 1 ; i < Math.round(Constants.SCREEN_WIDTH/Constants.SQUARES_X); i++) {
g2.drawLine(i*Constants.SQUARES_X, 0, i*Constants.SQUARES_X, Constants.SCREEN_HEIGHT);
// size of grid blocks
int blockWidth = Constants.SCREEN_WIDTH / Constants.SQUARES_X;
int blockHeight = Constants.SCREEN_HEIGHT / Constants.SQUARES_Y;
// draw vertical lines
for(int i = 1 ; i < Constants.SQUARES_X; i++) {
g2.drawLine(i*blockWidth, 0, i*blockWidth, Constants.SCREEN_HEIGHT);
}
// horizontal lines
for(int i = 1 ; i < Math.round(Constants.SCREEN_HEIGHT/Constants.SQUARES_Y); i++) {
g2.drawLine(0, i*Constants.SQUARES_Y, Constants.SCREEN_WIDTH, i*Constants.SQUARES_Y);
// draw horizontal lines
for(int i = 1 ; i < Constants.SQUARES_Y; i++) {
g2.drawLine(0, i*blockHeight, Constants.SCREEN_WIDTH, i*blockHeight);
}
}