code styled
diff --git a/src/sample/ArtRico.java b/src/sample/ArtRico.java
index d7081e8..fbed54d 100644
--- a/src/sample/ArtRico.java
+++ b/src/sample/ArtRico.java
@@ -17,20 +17,45 @@
import java.util.HashSet;
public class ArtRico extends Application {
+ final int sceneWidth = 1280;
+ final int sceneHeight = 640;
+ final int wallWidth = 10;
+ final int strokeWidth = 2;
+ final int topHeight = 30;
+ final int bottomHeight = 10;
+ final int playerHeight = 20;
+ final int playerPosY = 570;
+ final int playerWidthEasy = 280;
+ final int playerWidthMedium = 200;
+ final int playerWidthHard = 140;
+ final int playerSpeedEasy = 10;
+ final int playerSpeedMedium = 10;
+ final int playerSpeedHard = 10;
+ final int ballSpeedEasy = 5;
+ final int ballSpeedMedium = 5;
+ final int ballSpeedHard = 10;
+ final int ballSize = 20;
+ final int bricksN = 15;
+ final int bricksM = 8;
+ final int bricksHeight = 40;
+ final int bricksWidth = 80;
+ 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; // массив нажатых клавиш
+ 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;
@@ -47,13 +72,14 @@
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: ");
}
@@ -82,37 +108,59 @@
private Parent createContent(int mode) {
pause = true;
gameRoot = new Pane();
- gameRoot.setPrefSize(1280,640);
- Rectangle bg = new Rectangle(1280,640,Color.GREY);
- leftWall = new SimpleObject(0,0,10,640,2,Color.ORANGE,Color.BEIGE);
- rightWall = new SimpleObject(1270,0,10,640,2,Color.ORANGE,Color.BEIGE);
- rightWall = new SimpleObject(1270,0,10,640,2,Color.ORANGE,Color.BEIGE);
- bottom = new SimpleObject(0,640,1280,20,2,Color.ORANGE,Color.BEIGE);
- top = new SimpleObject(0,0,1280,30,2,Color.ORANGE,Color.BEIGE);
+ 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));
- if(mode == 0){
- player = new Player(500,570,280,20,2,Color.ORANGE,Color.BEIGE,10,1270,10);
- ball = new Ball(635,550,20,20,2,Color.ORANGE,Color.BEIGE,10,1270,30,640,5,5);
- }
- if(mode == 1){
- player = new Player(540,570,200,20,2,Color.ORANGE,Color.BEIGE,10,1270,10);
- ball = new Ball(635,550,20,20,2,Color.ORANGE,Color.BEIGE,10,1270,30,640,5,5);
- }
- if(mode == 2){
- player = new Player(570,570,140,20,2,Color.ORANGE,Color.BEIGE,10,1270,10);
- ball = new Ball(635,550,20,20,2,Color.ORANGE,Color.BEIGE,10,1270,30,640,10,10);
- }
+
+ 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);
+ 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);
+ 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);
+ break;
+ }
+
+ }
ball.setPlayer(player);
ball.setFlagsInFalse();
bot = new Bot(player, ball);
- gameRoot.getChildren().addAll(bg,player,ball,leftWall,rightWall,bottom,top,scoreLabel);
+ gameRoot.getChildren().addAll(bg, player, ball, leftWall, rightWall, bottom, top, scoreLabel);
bricks = new ArrayList<>();
- createBricks(15,8,80,40);
+ createBricks(bricksN, bricksM, bricksWidth, bricksHeight);
return gameRoot;
}
@@ -123,21 +171,22 @@
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);
+ 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);
- createBricks(15,8,80,40);
+ createBricks(bricksN, bricksM, bricksWidth, bricksHeight);
score = 0;
- player.setTranslateX(570);
- player.setTranslateY(570);
- ball.setTranslateX(635);
- ball.setTranslateY(550);
+ 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();
}
@@ -179,12 +228,13 @@
}
});
}
+
private void showMessage (String msg) {
pause = true;
Button button = new Button(msg + score);
button.setLayoutX(510);
button.setLayoutY(240);
- button.setPrefSize(260,80);
+ button.setPrefSize(buttonWidth, buttonHeight);
button.setOnAction(e -> {
pause = false;
button.setVisible(false);
@@ -195,22 +245,23 @@
restart();
gameRoot.getChildren().add(button);
}
+
private void showMenu(Stage primaryStage){
menuRoot = new Pane();
- menuRoot.setPrefSize(1280,640);
- Rectangle bg = new Rectangle(1280,640,Color.GREY);
+ 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.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(260,80);
+ button[i].setLayoutY(i * 100 + 140);
+ button[i].setPrefSize(buttonWidth, buttonHeight);
menuRoot.getChildren().add(button[i]);
}
button[0].setText("Easy");
@@ -238,6 +289,7 @@
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 39117c1..b5c3373 100644
--- a/src/sample/Ball.java
+++ b/src/sample/Ball.java
@@ -14,15 +14,17 @@
ArrayList<Integer> indexs;
Rectangle rect;
double speedX, kx;
- double speedY, ky; //
- double leftBorder; // коорд левой стены X
- double rightBorder; // коорд правой стены X
- double topBorder; // коорд. потолка Y
- double bottomBorder; // коорд. дна Y
- boolean gameOver; // флаг поражения
- boolean gameWon; // флаг победы
- boolean lc, rc, tc, bc, pc; // флаги столкновений
- 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) {
+ double speedY, ky;
+ double leftBorder;
+ double rightBorder;
+ double topBorder;
+ double bottomBorder;
+ boolean gameOver;
+ boolean gameWon;
+ boolean lc, rc, tc, bc, pc;
+
+ 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;
@@ -37,6 +39,7 @@
ky = 1;
kx = 0;
}
+
public void checkCollision(){
if (this.getTranslateX() + speedX*kx <= leftBorder) {
lc = true;
@@ -51,72 +54,86 @@
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()) {
+ 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() && this.getTranslateX() + this.getWidth() >= player.getTranslateX()) {
+ 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()) {
+ 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()) {
+ 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);
}
}
}
}
- // коллизия с кирпичом верхом
+ // 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()) {
+ 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);
}
}
}
}
- // коллизия с кирпичом правым боком
+ // 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()) {
+ 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()) {
+ 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);
}
@@ -124,9 +141,9 @@
}
}
}
+
public void move() {
- checkCollision(); // проверка коллизий, установка флагов
- // изменение скорости в зависимости от флагов
+ checkCollision();
if (tc ^ bc) {
ky = -ky;
}
@@ -135,11 +152,12 @@
}
if (pc) {
double k;
- k = (this.getTranslateX()+this.getWidth()/2-player.getTranslateX()-player.getWidth()/2) / (player.getWidth()/2);
+ 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);
@@ -155,33 +173,29 @@
gameWon = true;
}
- // перемещение мяча
+ // move ball
setTranslateX(getTranslateX() + speedX*kx);
setTranslateY(getTranslateY() + speedY*ky);
- setFlagsInFalse(); // сброс флагов
+ setFlagsInFalse(); // reset flags
}
- public void setSpeedX(double speedX) {
- this.speedX = speedX;
- }
- public void setSpeedY(double speedY) {
- this.speedY = speedY;
- }
+
public void setBricks(ArrayList<SimpleObject> bricks) {
- /*for(SimpleObject b : this.bricks) {
- b.setVisible(false);
- }*/
this.bricks.clear();
this.bricks.addAll(bricks);
}
+
public void setPlayer(Player player) {
this.player = player;
}
+
public boolean isGameOver() {
return gameOver;
}
+
public boolean isGameWon() {
return gameWon;
}
+
public void setFlagsInFalse() {
lc = false;
rc = false;
diff --git a/src/sample/Bot.java b/src/sample/Bot.java
index 6848b1b..b2b7d44 100644
--- a/src/sample/Bot.java
+++ b/src/sample/Bot.java
@@ -30,6 +30,5 @@
time = 0;
}
time++;
-
}
}
diff --git a/src/sample/Player.java b/src/sample/Player.java
index 9379382..b7e5c8d 100644
--- a/src/sample/Player.java
+++ b/src/sample/Player.java
@@ -8,18 +8,22 @@
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) {
+
+ 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 moveRight() {
if(this.getTranslateX() + this.getWidth() >= rightBorder) {
return;
diff --git a/src/sample/SimpleObject.java b/src/sample/SimpleObject.java
index 7c32753..0eab22e 100644
--- a/src/sample/SimpleObject.java
+++ b/src/sample/SimpleObject.java
@@ -8,7 +8,7 @@
Rectangle rect;
public SimpleObject(double x, double y, double w, double h, double sw, Color fillColor,Color strokeColor) {
- rect = new Rectangle(w,h,fillColor);
+ rect = new Rectangle(w, h, fillColor);
this.setTranslateX(x);
this.setTranslateY(y);
this.getChildren().add(rect);