Breakout Aufgabe 1 - Fehlerkorrektur
This commit is contained in:
parent
3d74d9e5e0
commit
10ec639943
@ -31,8 +31,8 @@ public class Ball implements IBall{
|
|||||||
this.direction.rescale();
|
this.direction.rescale();
|
||||||
|
|
||||||
// start at bottom-center
|
// start at bottom-center
|
||||||
this.position.setX(Constants.SCREEN_WIDTH/2);
|
this.position.setX((Constants.SCREEN_WIDTH - Constants.BALL_DIAMETER)/2);
|
||||||
this.position.setY(Constants.SCREEN_HEIGHT-3*Constants.BALL_DIAMETER);
|
this.position.setY(Constants.SCREEN_HEIGHT-Constants.BALL_DIAMETER);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -67,16 +67,28 @@ public class Ball implements IBall{
|
|||||||
*/
|
*/
|
||||||
public void reactOnBorder() {
|
public void reactOnBorder() {
|
||||||
// reacts on left border
|
// 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)
|
// reacts on right border (-Diameter because of hitbox)
|
||||||
if(this.position.getX()+Constants.BALL_DIAMETER > Constants.SCREEN_WIDTH) this.direction.setDx(-(this.direction.getDx()));
|
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
|
// 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)
|
// 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()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -73,8 +73,8 @@ public class Vector2D implements IVector2D {
|
|||||||
*/
|
*/
|
||||||
public void rescale() {
|
public void rescale() {
|
||||||
// calc unit vector and set it
|
// 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
|
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);
|
setDx((1/vectorLength) * getDx() * Constants.BALL_SPEED);
|
||||||
setDy((1/vectorlength) * getDy() * Constants.BALL_SPEED);
|
setDy((1/vectorLength) * getDy() * Constants.BALL_SPEED);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -119,14 +119,19 @@ public class Field extends JPanel {
|
|||||||
* @param g2 The graphics object
|
* @param g2 The graphics object
|
||||||
*/
|
*/
|
||||||
private void drawGrid(Graphics2D g2) {
|
private void drawGrid(Graphics2D g2) {
|
||||||
// vertical lines
|
|
||||||
for(int i = 1 ; i < Math.round(Constants.SCREEN_WIDTH/Constants.SQUARES_X); i++) {
|
// size of grid blocks
|
||||||
g2.drawLine(i*Constants.SQUARES_X, 0, i*Constants.SQUARES_X, Constants.SCREEN_HEIGHT);
|
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
|
// draw horizontal lines
|
||||||
for(int i = 1 ; i < Math.round(Constants.SCREEN_HEIGHT/Constants.SQUARES_Y); i++) {
|
for(int i = 1 ; i < Constants.SQUARES_Y; i++) {
|
||||||
g2.drawLine(0, i*Constants.SQUARES_Y, Constants.SCREEN_WIDTH, i*Constants.SQUARES_Y);
|
g2.drawLine(0, i*blockHeight, Constants.SCREEN_WIDTH, i*blockHeight);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user