I wrote this code (as I thought) in accordance with the task conditions and then carefully studied the help section. It seems my code is all right. But the first two requirements are still not validated.
Out of fatigue, I tried inserting break; at line 76; with or without it, the code is not accepted. The comment I keep getting from the validator is, "The direction of the ball's movement after a collision with a brick (an element in the list of bricks) must be changed in accordance with the rule stipulated in the task conditions".
What am I missing?
package com.codegym.task.task24.task2413;
import java.util.ArrayList;
import java.util.List;
public class Arkanoid {
// NON-private static Arkanoid game variable,
// which will store a reference to the created Arkanoid object.
public static Arkanoid game;
// private fields
private int width;
private int height;
private Ball ball;
private Paddle paddle;
private List<Brick> bricks;
private boolean isGameOver;
// setters and getters
public void setWidth(int width) {
this.width = width;
}
public void setHeight(int height) {
this.height = height;
}
public void setBall(Ball ball) {
this.ball = ball;
}
public void setPaddle(Paddle paddle) { this.paddle = paddle; }
public void setBricks(List<Brick> bricks){
this.bricks = bricks;
}
public int getWidth() {
return width;
}
public int getHeight() { return height; }
public Ball getBall() { return ball; }
public Paddle getPaddle() { return paddle; }
public List<Brick> getBricks() { return bricks; }
// constructor with two int parameters (width and height)
// correctly set the values of the corresponding fields in the class.
public Arkanoid(int width, int height) {
this.width = width;
this.height = height;
}
// run() method, where we'll define the main logic of the program.
public void run() {}
// move() method, which will move by one step all objects that need to be moved.
public void move() {
ball.move();
paddle.move();
}
public void draw(Canvas canvas) {
ball.draw(canvas);
paddle.draw(canvas);
for (Brick brick : bricks) { brick.draw(canvas); }
}
public void checkPaddleCollision() {
//In this method, you need to check whether the ball has hit the paddle.
// Use the intersects method to test for a collision.
// If the ball does hit the paddle, then:
if (ball.intersects(paddle)) {
// the ball flies up in a random direction:
double angle = 90 + 20 * (Math.random() - 0.5);
ball.setDirection(angle);
}
}
public void checkBrickCollision() {
// Use the intersects method to test for a collision.
for (Brick brick : new ArrayList<>(bricks)) {
// If the ball does hit a brick, then:
if (ball.intersects(brick)) {
// a) the ball flies off in a random direction:
double angle = Math.random() * 360;
ball.setDirection(angle);
// b) the brick "dies" and must be removed from the list of bricks.
bricks.remove(brick);
}
}
}
public void checkGameOver() {
// If the ball's y coordinate is greater than the height of the game field (height),
// then the ball has flown off the bottom of the screen.
// If this happens, set the isGameOver variable to true.
if (ball.getY() > game.getHeight()) isGameOver = true;
}
public static void main(String[] args) {}
}