Code styled 2
diff --git a/src/sample/ArtRico.java b/src/sample/ArtRico.java
index fbed54d..6bcba10 100644
--- a/src/sample/ArtRico.java
+++ b/src/sample/ArtRico.java
@@ -8,6 +8,8 @@
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.input.KeyEvent;
+import javafx.scene.layout.Background;
+import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
@@ -27,10 +29,10 @@
final int playerPosY = 570;
final int playerWidthEasy = 280;
final int playerWidthMedium = 200;
- final int playerWidthHard = 140;
+ final int playerWidthHard = 200;
final int playerSpeedEasy = 10;
final int playerSpeedMedium = 10;
- final int playerSpeedHard = 10;
+ final int playerSpeedHard = 20;
final int ballSpeedEasy = 5;
final int ballSpeedMedium = 5;
final int ballSpeedHard = 10;
@@ -42,255 +44,283 @@
final int buttonWidth = 260;
final int buttonHeight = 80;
- Pane gameRoot;
- Pane menuRoot;
- Scene scene;
- ArrayList<SimpleObject> bricks;
- Player player;
- Ball ball;
- SimpleObject leftWall;
- SimpleObject rightWall;
- SimpleObject top;
- SimpleObject bottom;
- static int score;
- Label scoreLabel;
- Bot bot;
- static HashSet<String> currentlyActiveKeys;
- static HashSet<String> lastKey;
- boolean pause;
- AnimationTimer timer;
+ String exitButton;
+ String pauseButton;
+ String botButton;
+ String turnLeftButton;
+ String turnRightButton;
+
+ Pane gameRoot;
+ Pane menuRoot;
+ Scene scene;
+ ArrayList<SimpleObject> bricks;
+ Player player;
+ Ball ball;
+ SimpleObject leftWall;
+ SimpleObject rightWall;
+ SimpleObject top;
+ SimpleObject bottom;
+ static int score;
+ Label scoreLabel;
+ Bot bot;
+ static HashSet<String> currentlyActiveKeys;
+ static HashSet<String> lastKey;
+ boolean pause;
+ AnimationTimer timer;
- @Override
- public void start(Stage primaryStage) {
- primaryStage.setTitle("ArtRik");
- showMenu(primaryStage);
- }
+ @Override
+ public void start(Stage primaryStage) {
+ primaryStage.setTitle("ArtRik");
+ showMenu(primaryStage);
+ }
- private void startGame(Stage primaryStage, int mode){
- scene = new Scene(createContent(mode));
- prepareActionHandlers();
- primaryStage.setScene(scene);
- primaryStage.show();
+ private void startGame(Stage primaryStage, int mode) {
+ scene = new Scene(createContent(mode));
+ prepareActionHandlers();
+ primaryStage.setScene(scene);
+ primaryStage.show();
- timer = new AnimationTimer() {
- @Override
- public void handle(long now)
- {
- if (ball.isGameWon()) {
- showMessage("You won! Your score is: ");
- }
- if(ball.isGameOver()) {
- showMessage("You lose! Your score is: ");
- }
- if (lastKey.contains("SPACE") ) {
+ timer = new AnimationTimer() {
+ @Override
+ public void handle(long now) {
+ if (ball.isGameWon()) {
+ showMessage("You won! Your score is: ");
+ }
+ if (ball.isGameOver()) {
+ showMessage("You lose! Your score is: ");
+ }
+ if (lastKey.contains("SPACE")) {
+ if (pause == false) {
+ pause = true;
+ } else {
+ pause = false;
+ }
+ }
+ if (lastKey.contains("ESCAPE")) {
+ gameRoot.getChildren().clear();
+ timer.stop();
+ showMenu(primaryStage);
+ }
+ if (lastKey.contains("B")) {
+ if (bot.isOn()) {
+ bot.setOff();
+ } else {
+ bot.setOn();
+ }
+ }
+ lastKey.clear();
+ if (!pause) {
+ update();
+ }
+ }
+ };
+ timer.start();
+ }
- if (pause == false) {
- pause = true;
- } else {
- pause = false;
- }
- }
- if (lastKey.contains("ESCAPE") ) {
- timer.stop();
- showMenu(primaryStage);
- }
- lastKey.clear();
- if(!pause) {
- update();
- }
+ private Parent createContent(int mode) {
+ pause = true;
+ gameRoot = new Pane();
+ gameRoot.setPrefSize(1280, 640);
+ Rectangle bg = new Rectangle(sceneWidth, sceneHeight, Color.GREY);
+ leftWall = new SimpleObject(0, 0, wallWidth, sceneHeight,
+ strokeWidth, Color.ORANGE, Color.BEIGE);
+ rightWall = new SimpleObject(sceneWidth - wallWidth, 0, wallWidth,
+ sceneWidth, strokeWidth, Color.ORANGE, Color.BEIGE);
+ bottom = new SimpleObject(0, sceneHeight, sceneWidth, bottomHeight,
+ strokeWidth, Color.ORANGE, Color.BEIGE);
+ top = new SimpleObject(0, 0, sceneWidth, topHeight,
+ strokeWidth, Color.ORANGE, Color.BEIGE);
+ score = 0;
+ scoreLabel = new Label("Score : " + score);
+ scoreLabel.setLayoutX(40);
+ scoreLabel.setLayoutY(0);
+ scoreLabel.setFont(javafx.scene.text.Font.font(20));
- }
- };
- timer.start();
- }
-
- private Parent createContent(int mode) {
- pause = true;
- gameRoot = new Pane();
- gameRoot.setPrefSize(1280, 640);
- Rectangle bg = new Rectangle(sceneWidth, sceneHeight, Color.GREY);
- leftWall = new SimpleObject(0, 0, wallWidth, sceneHeight,
- strokeWidth, Color.ORANGE, Color.BEIGE);
- rightWall = new SimpleObject(sceneWidth-wallWidth, 0, wallWidth, sceneWidth,
- strokeWidth, Color.ORANGE, Color.BEIGE);
- bottom = new SimpleObject(0, sceneHeight, sceneWidth, bottomHeight,
- strokeWidth, Color.ORANGE, Color.BEIGE);
- top = new SimpleObject(0, 0, sceneWidth, topHeight,
- strokeWidth, Color.ORANGE, Color.BEIGE);
- score = 0;
- scoreLabel = new Label("Score : " + score);
- scoreLabel.setLayoutX(40);
- scoreLabel.setLayoutY(0);
- scoreLabel.setFont(javafx.scene.text.Font.font(20));
-
- switch(mode){
- case 0:{
- player = new Player(sceneWidth / 2 - playerWidthEasy / 2, playerPosY, playerWidthEasy,
- playerHeight, strokeWidth, Color.ORANGE, Color.BEIGE,
- wallWidth, sceneWidth-wallWidth, playerSpeedEasy);
- ball = new Ball(sceneWidth/2-ballSize/2, playerPosY-ballSize, ballSize, ballSize,
- strokeWidth, Color.ORANGE, Color.BEIGE, wallWidth, sceneWidth - wallWidth,
- topHeight, sceneHeight, ballSpeedEasy, ballSpeedEasy);
+ switch (mode) {
+ case 0: {
+ player = new Player(sceneWidth / 2 - playerWidthEasy / 2,
+ playerPosY, playerWidthEasy, playerHeight,
+ strokeWidth, Color.ORANGE, Color.BEIGE,
+ wallWidth, sceneWidth - wallWidth, playerSpeedEasy);
+ ball = new Ball(sceneWidth / 2 - ballSize / 2, playerPosY - ballSize,
+ ballSize, ballSize, strokeWidth, Color.ORANGE,
+ Color.BEIGE, wallWidth, sceneWidth - wallWidth,
+ topHeight, sceneHeight, ballSpeedEasy);
break;
}
- case 1:{
- player = new Player(sceneWidth / 2 - playerWidthMedium / 2, playerPosY, playerWidthMedium,
- playerHeight, strokeWidth, Color.ORANGE,Color.BEIGE,
- wallWidth, sceneWidth-wallWidth, playerSpeedMedium);
- ball = new Ball(sceneWidth/2-ballSize/2, playerPosY-ballSize, ballSize, ballSize,
- strokeWidth, Color.ORANGE, Color.BEIGE, wallWidth, sceneWidth - wallWidth,
- topHeight, sceneHeight, ballSpeedMedium, ballSpeedMedium);
+ case 1: {
+ player = new Player(sceneWidth / 2 - playerWidthMedium / 2,
+ playerPosY, playerWidthMedium,
+ playerHeight, strokeWidth, Color.ORANGE, Color.BEIGE,
+ wallWidth, sceneWidth - wallWidth, playerSpeedMedium);
+ ball = new Ball(sceneWidth / 2 - ballSize / 2, playerPosY - ballSize,
+ ballSize, ballSize, strokeWidth, Color.ORANGE,
+ Color.BEIGE, wallWidth, sceneWidth - wallWidth,
+ topHeight, sceneHeight, ballSpeedMedium);
break;
}
- case 2:{
- player = new Player(sceneWidth / 2 - playerWidthHard / 2, playerPosY, playerWidthHard,
- playerHeight, strokeWidth, Color.ORANGE, Color.BEIGE,
- wallWidth, sceneWidth-wallWidth, playerSpeedHard);
- ball = new Ball(sceneWidth/2-ballSize/2, playerPosY-ballSize, ballSize, ballSize,
- strokeWidth, Color.ORANGE, Color.BEIGE, wallWidth, sceneWidth - wallWidth,
- topHeight, sceneHeight, ballSpeedHard, ballSpeedHard);
+ case 2: {
+ player = new Player(sceneWidth / 2 - playerWidthHard / 2,
+ playerPosY, playerWidthHard, playerHeight,
+ strokeWidth, Color.ORANGE, Color.BEIGE,
+ wallWidth, sceneWidth - wallWidth, playerSpeedHard);
+ ball = new Ball(sceneWidth / 2 - ballSize / 2,
+ playerPosY - ballSize, ballSize,
+ ballSize, strokeWidth, Color.ORANGE,
+ Color.BEIGE, wallWidth, sceneWidth - wallWidth,
+ topHeight, sceneHeight, ballSpeedHard);
break;
}
}
- ball.setPlayer(player);
- ball.setFlagsInFalse();
- bot = new Bot(player, ball);
- gameRoot.getChildren().addAll(bg, player, ball, leftWall, rightWall, bottom, top, scoreLabel);
- bricks = new ArrayList<>();
- createBricks(bricksN, bricksM, bricksWidth, bricksHeight);
- return gameRoot;
- }
-
- private void createBricks(int colomn, int raw, int w, int h) {
- for(SimpleObject b : bricks) {
- b.setVisible(false);
- }
- bricks.clear();
- for (int i = 0; i < raw; i++) {
- for(int j = 0; j < colomn; j++) {
- SimpleObject brick = new SimpleObject(j * w + 40,i * h + 65, w, h, 2, Color.ORANGE, Color.BEIGE);
- bricks.add(brick);
- gameRoot.getChildren().add(brick);
- }
- }
- ball.setBricks(bricks);
- }
-
- private void restart() {
- gameRoot.getChildren().remove(bricks);
+ ball.setPlayer(player);
+ ball.setFlagsInFalse();
+ bot = new Bot(player, ball);
+ gameRoot.getChildren().addAll(bg, player, ball, leftWall, rightWall,
+ bottom, top, scoreLabel);
+ bricks = new ArrayList<>();
createBricks(bricksN, bricksM, bricksWidth, bricksHeight);
- score = 0;
- player.setTranslateX(sceneWidth / 2 - player.getWidth() / 2);
- player.setTranslateY(playerPosY);
- ball.setTranslateX(sceneWidth / 2 - ballSize / 2);
- ball.setTranslateY(playerPosY - ballSize);
- ball.gameOver = false;
- ball.setFlagsInFalse();
- }
+ return gameRoot;
+ }
- private void update() {
- if (currentlyActiveKeys.contains("LEFT"))
- {
- player.moveLeft();
- }
- if (currentlyActiveKeys.contains("RIGHT"))
- {
- player.moveRight();
- }
- //bot.Execute();
- ball.move();
- scoreLabel.setText("Score : " + score);
- }
+ private void createBricks(int colomn, int raw, int w, int h) {
+ gameRoot.getChildren().removeAll(bricks);
+ bricks.clear();
+ for (int i = 0; i < raw; i++) {
+ for (int j = 0; j < colomn; j++) {
+ SimpleObject brick = new SimpleObject(j * w + 40, i * h + 75,
+ w, h, strokeWidth, Color.ORANGE, Color.BEIGE);
+ bricks.add(brick);
- private void prepareActionHandlers()
- {
- // use a set so duplicates are not possible
- currentlyActiveKeys = new HashSet<String>();
- lastKey = new HashSet<String>();
- scene.setOnKeyPressed(new EventHandler<KeyEvent>()
- {
- @Override
- public void handle(KeyEvent event)
- {
- currentlyActiveKeys.add(event.getCode().toString());
- lastKey.add(event.getCode().toString());
- }
- });
- scene.setOnKeyReleased(new EventHandler<KeyEvent>()
- {
- @Override
- public void handle(KeyEvent event)
- {
- currentlyActiveKeys.remove(event.getCode().toString());
- }
- });
- }
+ }
+ }
+ gameRoot.getChildren().addAll(bricks);
+ ball.setBricks(bricks);
+ }
- private void showMessage (String msg) {
- pause = true;
- Button button = new Button(msg + score);
- button.setLayoutX(510);
- button.setLayoutY(240);
- button.setPrefSize(buttonWidth, buttonHeight);
- button.setOnAction(e -> {
- pause = false;
- button.setVisible(false);
- gameRoot.getChildren().remove(button);
- });
- ball.gameWon = false;
- ball.gameOver = false;
- restart();
- gameRoot.getChildren().add(button);
- }
+ private void restart() {
+ gameRoot.getChildren().removeAll(bricks);
+ createBricks(bricksN, bricksM, bricksWidth, bricksHeight);
+ score = 0;
+ player.setTranslateX(sceneWidth / 2 - player.getWidth() / 2 + strokeWidth / 2);
+ player.setTranslateY(playerPosY);
+ ball.setTranslateX(sceneWidth / 2 - ballSize / 2);
+ ball.setTranslateY(playerPosY - ballSize);
+ ball.gameOver = false;
+ ball.setFlagsInFalse();
+ }
- private void showMenu(Stage primaryStage){
- menuRoot = new Pane();
- menuRoot.setPrefSize(sceneWidth, sceneHeight);
- Rectangle bg = new Rectangle(sceneWidth, sceneHeight, Color.GREY);
- Button startButton = new Button("Start game");
- startButton.setLayoutX(510);
- startButton.setLayoutY(140);
- startButton.setPrefSize(260, 80);
- startButton.setOnAction(e -> {
- startButton.setVisible(false);
- Button[] button = new Button[4];
- for(int i = 0; i < 4; i++) {
- button[i] = new Button();
- button[i].setLayoutX(510);
- button[i].setLayoutY(i * 100 + 140);
- button[i].setPrefSize(buttonWidth, buttonHeight);
- menuRoot.getChildren().add(button[i]);
- }
- button[0].setText("Easy");
- button[1].setText("Medium");
- button[2].setText("Hard");
- button[3].setText("Back");
- button[0].setOnAction(event -> {
- startGame(primaryStage, 0);
- });
- button[1].setOnAction(event -> {
- startGame(primaryStage, 1);
- });
- button[2].setOnAction(event -> {
- startGame(primaryStage, 2);
- });
- button[3].setOnAction(event -> {
- for(Button b : button) {
- b.setVisible(false);
- startButton.setVisible(true);
- }
- });
- });
- menuRoot.getChildren().addAll(bg,startButton);
- scene = new Scene(menuRoot);
- primaryStage.setScene(scene);
- primaryStage.show();
- }
+ private void update() {
+ if (bot.isOn()) {
+ bot.Execute();
+ } else if (currentlyActiveKeys.contains("LEFT")) {
+ player.moveLeft();
+ } else if (currentlyActiveKeys.contains("RIGHT")) {
+ player.moveRight();
+ }
+ ball.move();
+ scoreLabel.setText("Score : " + score);
+ }
- public static void main(String[] args) {
- launch(args);
- }
+ private void prepareActionHandlers() {
+ // use a set so duplicates are not possible
+ currentlyActiveKeys = new HashSet<String>();
+ lastKey = new HashSet<String>();
+ scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
+ @Override
+ public void handle(KeyEvent event) {
+ currentlyActiveKeys.add(event.getCode().toString());
+ lastKey.add(event.getCode().toString());
+ }
+ });
+ scene.setOnKeyReleased(new EventHandler<KeyEvent>() {
+ @Override
+ public void handle(KeyEvent event) {
+ currentlyActiveKeys.remove(event.getCode().toString());
+ }
+ });
+ }
+
+ private void showMessage(String msg) {
+ pause = true;
+ Button button = new Button(msg + score);
+ button.setLayoutX(510);
+ button.setLayoutY(240);
+ button.setPrefSize(buttonWidth, buttonHeight);
+ button.setOnAction(e -> {
+ pause = false;
+ button.setVisible(false);
+ gameRoot.getChildren().remove(button);
+ });
+ ball.gameWon = false;
+ ball.gameOver = false;
+ restart();
+ gameRoot.getChildren().add(button);
+ }
+
+ private void showMenu(Stage primaryStage) {
+ menuRoot = new Pane();
+ menuRoot.setPrefSize(sceneWidth, sceneHeight);
+ Rectangle bg = new Rectangle(sceneWidth, sceneHeight, Color.GREY);
+ Button[] button1 = new Button[4];
+
+ for (int i = 0; i < 4; i++) {
+ button1[i] = new Button();
+ button1[i].setLayoutX(510);
+ button1[i].setLayoutY(i * 100 + 140);
+ button1[i].setPrefSize(buttonWidth, buttonHeight);
+ menuRoot.getChildren().add(button1[i]);
+ }
+
+ button1[0].setText("NEW GAME");
+ button1[1].setText("SETTINGS");
+ button1[2].setText("ABOUT");
+ button1[3].setText("EXIT");
+
+ button1[0].setOnAction(e -> {
+ menuRoot.getChildren().removeAll(button1);
+ Button[] button2 = new Button[4];
+ for (int i = 0; i < 4; i++) {
+ button2[i] = new Button();
+ button2[i].setLayoutX(510);
+ button2[i].setLayoutY(i * 100 + 140);
+ button2[i].setPrefSize(buttonWidth, buttonHeight);
+ menuRoot.getChildren().add(button2[i]);
+ }
+ button2[0].setText("Easy");
+ button2[1].setText("Medium");
+ button2[2].setText("Hard");
+ button2[3].setText("Back");
+ button2[0].setOnAction(event -> {
+ menuRoot.getChildren().clear();
+ startGame(primaryStage, 0);
+ });
+ button2[1].setOnAction(event -> {
+ menuRoot.getChildren().clear();
+ startGame(primaryStage, 1);
+ });
+ button2[2].setOnAction(event -> {
+ menuRoot.getChildren().clear();
+ startGame(primaryStage, 2);
+ });
+ button2[3].setOnAction(event -> {
+ menuRoot.getChildren().removeAll(button2);
+ menuRoot.getChildren().addAll(button1);
+ });
+ });
+
+ button1[3].setOnAction(event -> {
+ System.exit(0);
+ });
+
+ scene = new Scene(menuRoot);
+ primaryStage.setScene(scene);
+ primaryStage.show();
+ }
+
+ public static void main(String[] args) {
+ launch(args);
+ }
}
diff --git a/src/sample/Ball.java b/src/sample/Ball.java
index b5c3373..9455a77 100644
--- a/src/sample/Ball.java
+++ b/src/sample/Ball.java
@@ -9,198 +9,236 @@
import java.util.Set;
public class Ball extends SimpleObject {
- Player player;
- ArrayList<SimpleObject> bricks;
- ArrayList<Integer> indexs;
- Rectangle rect;
- double speedX, kx;
- double speedY, ky;
- double leftBorder;
- double rightBorder;
- double topBorder;
- double bottomBorder;
- boolean gameOver;
- boolean gameWon;
- boolean lc, rc, tc, bc, pc;
+ Player player;
+ ArrayList<SimpleObject> bricks;
+ ArrayList<Integer> indexs;
+ Rectangle rect;
+ double speed, kx, ky;
+ double leftBorder;
+ double rightBorder;
+ double topBorder;
+ double bottomBorder;
+ boolean gameOver;
+ boolean gameWon;
+ boolean lc, rc, tc, bc, pc, tcow, bcow;
- public Ball(double x, double y, double w, double h, double sw, Color fillColor, Color strokeColor,
- double lb, double rb, double tb, double bb, double speedX, double SpeedY) {
- super(x, y, w, h, sw, fillColor, strokeColor);
- gameOver = false;
- gameWon = false;
- leftBorder = lb;
- rightBorder = rb;
- topBorder = tb;
- bottomBorder = bb;
- indexs = new ArrayList<>();
- bricks = new ArrayList<>();
- this.speedX = speedX;
- this.speedY = SpeedY;
- ky = 1;
- kx = 0;
- }
+ public Ball(double x, double y, double w, double h, double sw,
+ Color fillColor, Color strokeColor,
+ double lb, double rb, double tb,
+ double bb, double speed) {
+ super(x, y, w, h, sw, fillColor, strokeColor);
+ gameOver = false;
+ gameWon = false;
+ leftBorder = lb;
+ rightBorder = rb;
+ topBorder = tb;
+ bottomBorder = bb;
+ indexs = new ArrayList<>();
+ bricks = new ArrayList<>();
+ this.speed = speed;
+ ky = 1;
+ kx = 0;
+ }
- public void checkCollision(){
- if (this.getTranslateX() + speedX*kx <= leftBorder) {
- lc = true;
- }
- if (this.getTranslateX() + this.getWidth() + speedX*kx >= rightBorder) {
- rc = true;
- }
- if (this.getTranslateY() + speedY*ky <= topBorder) {
- tc = true;
- }
- if (this.getTranslateY() + this.getHeight() + speedY*ky >= bottomBorder) {
+ public void checkCollision() {
+ if (this.getTranslateX() + speed * kx <= leftBorder) {
+ lc = true;
+ }
+ if (this.getTranslateX() + this.getWidth() + speed * kx
+ >= rightBorder) {
+ rc = true;
+ }
+ if (this.getTranslateY() + speed * ky <= topBorder) {
+ tc = true;
+ }
+ if (this.getTranslateY() + this.getHeight() + speed * ky
+ >= bottomBorder) {
- gameOver = true;
- }
- // collision with player
- if(ky > 0) {
- if (this.getTranslateX() + this.getWidth() > player.getTranslateX() &&
- this.getTranslateX() < player.getTranslateX() + player.getWidth()) {
- if (this.getTranslateY() <= player.getTranslateY() &&
- this.getTranslateY() + this.getHeight() >= player.getTranslateY()) {
- pc = true;
- }
- }
- }
+ gameOver = true;
+ }
// collision with player
- if(kx > 0) {
- if (this.getTranslateY() + this.getHeight() > player.getTranslateY() &&
- this.getTranslateY() < player.getTranslateY() + player.getHeight()) {
- if (this.getTranslateX() <= player.getTranslateX() &&
- this.getTranslateX() + this.getWidth() >= player.getTranslateX()) {
- rc = true;
- }
- }
- }
+ if (ky > 0) {
+ if (this.getTranslateX() + this.getWidth()
+ > player.getTranslateX() &&
+ this.getTranslateX() < player.getTranslateX()
+ + player.getWidth()) {
+ if (this.getTranslateY() <= player.getTranslateY() &&
+ this.getTranslateY() + this.getHeight()
+ >= player.getTranslateY()) {
+ pc = true;
+ }
+ }
+ }
// collision with player
- if(kx < 0) {
- if (this.getTranslateY() + this.getHeight() > player.getTranslateY() &&
- this.getTranslateY() < player.getTranslateY() + player.getHeight()) {
- if (this.getTranslateX() <= player.getTranslateX() + player.getWidth() &&
- this.getTranslateX() + this.getWidth() >= player.getTranslateX() + player.getWidth()) {
- lc = true;
- }
- }
- }
+ if (kx > 0) {
+ if (this.getTranslateY() + this.getHeight()
+ > player.getTranslateY() &&
+ this.getTranslateY() < player.getTranslateY()
+ + player.getHeight()) {
+ if (this.getTranslateX() <= player.getTranslateX() &&
+ this.getTranslateX() + this.getWidth()
+ >= player.getTranslateX()) {
+ rc = true;
+ }
+ }
+ }
+ // collision with player
+ if (kx < 0) {
+ if (this.getTranslateY() + this.getHeight()
+ > player.getTranslateY() &&
+ this.getTranslateY() < player.getTranslateY()
+ + player.getHeight()) {
+ if (this.getTranslateX() <= player.getTranslateX()
+ + player.getWidth() &&
+ this.getTranslateX() + this.getWidth()
+ >= player.getTranslateX()
+ + player.getWidth()) {
+ lc = true;
+ }
+ }
+ }
// collision with brick
- if(ky > 0) {
- for (int i = 0; i < bricks.size(); i++) {
- SimpleObject brick = bricks.get(i);
- if (this.getTranslateX() + this.getWidth() > brick.getTranslateX() &&
- this.getTranslateX() < brick.getTranslateX() + brick.getWidth()) {
- if (this.getTranslateY() <= brick.getTranslateY() &&
- this.getTranslateY() + this.getHeight() >= brick.getTranslateY()) {
- bc = true;
- indexs.add(i);
- }
- }
- }
- }
+
+ for (int i = 0; i < bricks.size(); i++) {
+ SimpleObject brick = bricks.get(i);
+ if (this.getTranslateX() + this.getWidth()
+ > brick.getTranslateX() &&
+ this.getTranslateX() < brick.getTranslateX()
+ + brick.getWidth()) {
+ if (this.getTranslateY() <= brick.getTranslateY() &&
+ this.getTranslateY() + this.getHeight()
+ >= brick.getTranslateY()) {
+ if (ky > 0) {
+ bc = true;
+ } else {
+ bcow = true;
+ }
+ indexs.add(i);
+ }
+ }
+ }
+
// collision with brick
- if(ky < 0) {
- for (int i = 0; i < bricks.size(); i++) {
- SimpleObject brick = bricks.get(i);
- if (this.getTranslateX() + this.getWidth() > brick.getTranslateX() &&
- this.getTranslateX() < brick.getTranslateX() + brick.getWidth()) {
- if (this.getTranslateY() <= brick.getTranslateY() + brick.getHeight() &&
- this.getTranslateY() + this.getHeight() >= brick.getTranslateY() + brick.getHeight()) {
- tc = true;
- indexs.add(i);
- }
- }
- }
- }
+ for (int i = 0; i < bricks.size(); i++) {
+ SimpleObject brick = bricks.get(i);
+ if (this.getTranslateX() + this.getWidth()
+ > brick.getTranslateX() &&
+ this.getTranslateX() < brick.getTranslateX()
+ + brick.getWidth()) {
+ if (this.getTranslateY() <= brick.getTranslateY()
+ + brick.getHeight() &&
+ this.getTranslateY() + this.getHeight()
+ >= brick.getTranslateY()
+ + brick.getHeight()) {
+ if (ky < 0) {
+ tc = true;
+ } else {
+ tcow = true;
+ }
+ indexs.add(i);
+ }
+ }
+ }
+
// collision with brick
- if(kx > 0) {
- for (int i = 0; i < bricks.size(); i++) {
- SimpleObject brick = bricks.get(i);
- if (this.getTranslateY() + this.getHeight() > brick.getTranslateY() &&
- this.getTranslateY() < brick.getTranslateY() + brick.getHeight()) {
- if (this.getTranslateX() <= brick.getTranslateX() &&
- this.getTranslateX() + this.getWidth() >= brick.getTranslateX()) {
- lc = true;
- indexs.add(i);
- }
- }
- }
- }
+ if (kx > 0) {
+ for (int i = 0; i < bricks.size(); i++) {
+ SimpleObject brick = bricks.get(i);
+ if (this.getTranslateY() + this.getHeight()
+ > brick.getTranslateY() &&
+ this.getTranslateY() < brick.getTranslateY()
+ + brick.getHeight()) {
+ if (this.getTranslateX() <= brick.getTranslateX() &&
+ this.getTranslateX() + this.getWidth()
+ >= brick.getTranslateX()) {
+ lc = true;
+ indexs.add(i);
+ }
+ }
+ }
+ }
// collision with brick
- if(kx < 0) {
- for (int i = 0; i < bricks.size(); i++) {
- SimpleObject brick = bricks.get(i);
- if (this.getTranslateY() + this.getHeight() > brick.getTranslateY() &&
- this.getTranslateY() < brick.getTranslateY() + brick.getHeight()) {
- if (this.getTranslateX() <= brick.getTranslateX() + brick.getWidth() &&
- this.getTranslateX() + this.getWidth() >= brick.getTranslateX() + brick.getWidth()) {
- lc = true;
- indexs.add(i);
- }
- }
- }
- }
- }
+ if (kx < 0) {
+ for (int i = 0; i < bricks.size(); i++) {
+ SimpleObject brick = bricks.get(i);
+ if (this.getTranslateY() + this.getHeight()
+ > brick.getTranslateY() &&
+ this.getTranslateY() < brick.getTranslateY()
+ + brick.getHeight()) {
+ if (this.getTranslateX() <= brick.getTranslateX()
+ + brick.getWidth() &&
+ this.getTranslateX() + this.getWidth()
+ >= brick.getTranslateX()
+ + brick.getWidth()) {
+ lc = true;
+ indexs.add(i);
+ }
+ }
+ }
+ }
+ }
- public void move() {
- checkCollision();
- if (tc ^ bc) {
- ky = -ky;
- }
- else if(lc ^ rc) {
- kx = -kx;
- }
- if (pc) {
- double k;
- k = (this.getTranslateX()+this.getWidth()/2-player.getTranslateX()
- -player.getWidth()/2) / (player.getWidth()/2);
- kx = k;
- ky = -0.8 - (1 - Math.abs(k));
- }
- // destroying bricks
- Set ixs = new HashSet(indexs);
- indexs.clear();
- indexs.addAll(ixs);
- indexs.sort(Comparator.naturalOrder());
- for(int k = 0; indexs.size() > 0; k++) {
- SimpleObject b = bricks.get(indexs.get(0) - k);
- b.setVisible(false);
- bricks.remove(b);
- indexs.remove(0);
- ArtRico.score += 1;
- }
- if(bricks.size() == 0) {
- gameWon = true;
- }
+ public void move() {
+ checkCollision();
+ if ((tc && !bcow) ^ (bc && !tcow)) {
+ ky = -ky;
+ } else if (lc ^ rc) {
+ kx = -kx;
+ }
+ if (pc) {
+ double k;
+ k = (this.getTranslateX() + this.getWidth() / 2
+ - player.getTranslateX()
+ - player.getWidth() / 2) / (player.getWidth() / 2);
+ kx = k;
+ ky = -0.8 - (1 - Math.abs(k));
+ }
+ // destroying bricks
+ Set ixs = new HashSet(indexs);
+ indexs.clear();
+ indexs.addAll(ixs);
+ indexs.sort(Comparator.naturalOrder());
+ for (int k = 0; indexs.size() > 0; k++) {
+ SimpleObject b = bricks.get(indexs.get(0) - k);
+ b.setVisible(false);
+ bricks.remove(b);
+ indexs.remove(0);
+ ArtRico.score += 1;
+ }
+ if (bricks.size() == 0) {
+ gameWon = true;
+ }
- // move ball
- setTranslateX(getTranslateX() + speedX*kx);
- setTranslateY(getTranslateY() + speedY*ky);
- setFlagsInFalse(); // reset flags
- }
+ // move ball
+ setTranslateX(getTranslateX() + speed * kx);
+ setTranslateY(getTranslateY() + speed * ky);
+ setFlagsInFalse(); // reset flags
+ }
- public void setBricks(ArrayList<SimpleObject> bricks) {
- this.bricks.clear();
- this.bricks.addAll(bricks);
- }
+ public void setBricks(ArrayList<SimpleObject> bricks) {
+ this.bricks.clear();
+ this.bricks.addAll(bricks);
+ }
- public void setPlayer(Player player) {
- this.player = player;
- }
+ public void setPlayer(Player player) {
+ this.player = player;
+ }
- public boolean isGameOver() {
- return gameOver;
- }
+ public boolean isGameOver() {
+ return gameOver;
+ }
- public boolean isGameWon() {
- return gameWon;
- }
+ public boolean isGameWon() {
+ return gameWon;
+ }
- public void setFlagsInFalse() {
- lc = false;
- rc = false;
- tc = false;
- bc = false;
- pc = false;
- }
+ public void setFlagsInFalse() {
+ lc = false;
+ rc = false;
+ tc = false;
+ bc = false;
+ pc = false;
+ tcow = false;
+ bcow = false;
+ }
}
diff --git a/src/sample/Bot.java b/src/sample/Bot.java
index b2b7d44..fcd2ddc 100644
--- a/src/sample/Bot.java
+++ b/src/sample/Bot.java
@@ -1,34 +1,51 @@
package sample;
public class Bot {
- Player player;
- Ball ball;
- int time;
- public Bot(Player player, Ball ball) {
- this.player = player;
- this.ball = ball;
- time = 0;
- }
- public void Execute() {
- if(time < 1850){
- if (player.getTranslateX() + player.getWidth()/2 < ball.getTranslateX() + ball.getWidth()) {
- player.moveRight();
- }
- else {
- player.moveLeft();
- }
- }
- if(time > 1850) {
- if (player.getTranslateX() + player.getWidth()/2 < ball.getTranslateX()) {
- player.moveRight();
- }
- else {
- player.moveLeft();
- }
- }
- if (time == 4000) {
- time = 0;
- }
- time++;
- }
+ final int timeOfMoving = 2000;
+ Player player;
+ Ball ball;
+ int time;
+ boolean on;
+
+ public Bot(Player player, Ball ball) {
+ this.player = player;
+ this.ball = ball;
+ time = 0;
+ on = false;
+ }
+
+ public void Execute() {
+ if (time < timeOfMoving) {
+ if (player.getTranslateX() + player.getWidth() / 2
+ < ball.getTranslateX() + ball.getWidth()) {
+ player.moveRight();
+ } else {
+ player.moveLeft();
+ }
+ }
+ if (time > timeOfMoving) {
+ if (player.getTranslateX() + player.getWidth() / 2
+ < ball.getTranslateX()) {
+ player.moveRight();
+ } else {
+ player.moveLeft();
+ }
+ }
+ if (time == timeOfMoving * 2) {
+ time = 0;
+ }
+ time++;
+ }
+
+ public boolean isOn() {
+ return on;
+ }
+
+ public void setOn() {
+ on = true;
+ }
+
+ public void setOff() {
+ on = false;
+ }
}
diff --git a/src/sample/Player.java b/src/sample/Player.java
index b7e5c8d..e061e0c 100644
--- a/src/sample/Player.java
+++ b/src/sample/Player.java
@@ -3,31 +3,32 @@
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
-public class Player extends SimpleObject{
- Rectangle rect;
- double speed;
- double leftBorder;
- double rightBorder;
+public class Player extends SimpleObject {
+ Rectangle rect;
+ double speed;
+ double leftBorder;
+ double rightBorder;
- public Player(double x, double y, double w, double h, double sw, Color fillColor,Color strokeColor,
- double lb, double rb, double speed) {
- super(x, y, w, h, sw, fillColor, strokeColor);
- leftBorder = lb;
- rightBorder = rb;
- this.speed = speed;
- }
+ public Player(double x, double y, double w, double h, double sw,
+ Color fillColor, Color strokeColor,
+ double lb, double rb, double speed) {
+ super(x, y, w, h, sw, fillColor, strokeColor);
+ leftBorder = lb;
+ rightBorder = rb;
+ this.speed = speed;
+ }
- public void moveLeft() {
- if(this.getTranslateX() <= leftBorder) {
- return;
- }
- setTranslateX(this.getTranslateX() - speed);
- }
+ public void moveLeft() {
+ if (this.getTranslateX() <= leftBorder) {
+ return;
+ }
+ setTranslateX(this.getTranslateX() - speed);
+ }
- public void moveRight() {
- if(this.getTranslateX() + this.getWidth() >= rightBorder) {
- return;
- }
- setTranslateX(this.getTranslateX() + speed);
- }
+ public void moveRight() {
+ if (this.getTranslateX() + this.getWidth() >= rightBorder) {
+ return;
+ }
+ setTranslateX(this.getTranslateX() + speed);
+ }
}
diff --git a/src/sample/SimpleObject.java b/src/sample/SimpleObject.java
index 0eab22e..037b7a8 100644
--- a/src/sample/SimpleObject.java
+++ b/src/sample/SimpleObject.java
@@ -5,14 +5,15 @@
import javafx.scene.shape.Rectangle;
public class SimpleObject extends Pane {
- Rectangle rect;
+ Rectangle rect;
- public SimpleObject(double x, double y, double w, double h, double sw, Color fillColor,Color strokeColor) {
- rect = new Rectangle(w, h, fillColor);
- this.setTranslateX(x);
- this.setTranslateY(y);
- this.getChildren().add(rect);
- rect.setStrokeWidth(sw);
- rect.setStroke(strokeColor);
- }
+ public SimpleObject(double x, double y, double w, double h, double sw,
+ Color fillColor, Color strokeColor) {
+ rect = new Rectangle(w, h, fillColor);
+ this.setTranslateX(x);
+ this.setTranslateY(y);
+ this.getChildren().add(rect);
+ rect.setStrokeWidth(sw);
+ rect.setStroke(strokeColor);
+ }
}
\ No newline at end of file