Created
August 20, 2021 15:05
-
-
Save Da9el00/30515f6ff0c4718fc09f2b573532f535 to your computer and use it in GitHub Desktop.
JavaFX and Scene Builder - Brick Breaker Game part 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample; | |
import javafx.animation.Animation; | |
import javafx.animation.KeyFrame; | |
import javafx.animation.Timeline; | |
import javafx.event.ActionEvent; | |
import javafx.event.EventHandler; | |
import javafx.fxml.FXML; | |
import javafx.fxml.Initializable; | |
import javafx.geometry.Bounds; | |
import javafx.scene.Node; | |
import javafx.scene.control.Button; | |
import javafx.scene.layout.AnchorPane; | |
import javafx.scene.paint.Color; | |
import javafx.scene.robot.Robot; | |
import javafx.scene.shape.Circle; | |
import javafx.scene.shape.Rectangle; | |
import javafx.util.Duration; | |
import java.net.URL; | |
import java.util.ArrayList; | |
import java.util.ResourceBundle; | |
public class Controller implements Initializable { | |
@FXML | |
private AnchorPane scene; | |
@FXML | |
private Circle circle; | |
@FXML | |
private Rectangle paddle; | |
@FXML | |
private Rectangle bottomZone; | |
@FXML | |
private Button startButton; | |
Robot robot = new Robot(); | |
private ArrayList<Rectangle> bricks = new ArrayList<>(); | |
double deltaX = -1; | |
double deltaY = -3; | |
//1 Frame evey 10 millis, which means 100 FPS | |
Timeline timeline = new Timeline(new KeyFrame(Duration.millis(10), new EventHandler<ActionEvent>() { | |
@Override | |
public void handle(ActionEvent actionEvent) { | |
movePaddle(); | |
checkCollisionPaddle(paddle); | |
circle.setLayoutX(circle.getLayoutX() + deltaX); | |
circle.setLayoutY(circle.getLayoutY() + deltaY); | |
if(!bricks.isEmpty()){ | |
bricks.removeIf(brick -> checkCollisionBrick(brick)); | |
} else { | |
timeline.stop(); | |
} | |
checkCollisionScene(scene); | |
checkCollisionBottomZone(); | |
} | |
})); | |
@Override | |
public void initialize(URL url, ResourceBundle resourceBundle) { | |
timeline.setCycleCount(Animation.INDEFINITE); | |
} | |
@FXML | |
void startGameButtonAction(ActionEvent event) { | |
startButton.setVisible(false); | |
startGame(); | |
} | |
public void startGame(){ | |
createBricks(); | |
timeline.play(); | |
} | |
public void checkCollisionScene(Node node){ | |
Bounds bounds = node.getBoundsInLocal(); | |
boolean rightBorder = circle.getLayoutX() >= (bounds.getMaxX() - circle.getRadius()); | |
boolean leftBorder = circle.getLayoutX() <= (bounds.getMinX() + circle.getRadius()); | |
boolean bottomBorder = circle.getLayoutY() >= (bounds.getMaxY() - circle.getRadius()); | |
boolean topBorder = circle.getLayoutY() <= (bounds.getMinY() + circle.getRadius()); | |
if (rightBorder || leftBorder) { | |
deltaX *= -1; | |
} | |
if (bottomBorder || topBorder) { | |
deltaY *= -1; | |
} | |
} | |
public boolean checkCollisionBrick(Rectangle brick){ | |
if(circle.getBoundsInParent().intersects(brick.getBoundsInParent())){ | |
boolean rightBorder = circle.getLayoutX() >= ((brick.getX() + brick.getWidth()) - circle.getRadius()); | |
boolean leftBorder = circle.getLayoutX() <= (brick.getX() + circle.getRadius()); | |
boolean bottomBorder = circle.getLayoutY() >= ((brick.getY() + brick.getHeight()) - circle.getRadius()); | |
boolean topBorder = circle.getLayoutY() <= (brick.getY() + circle.getRadius()); | |
if (rightBorder || leftBorder) { | |
deltaX *= -1; | |
} | |
if (bottomBorder || topBorder) { | |
deltaY *= -1; | |
} | |
scene.getChildren().remove(brick); | |
return true; | |
} | |
return false; | |
} | |
public void createBricks(){ | |
double width = 560; | |
double height = 200; | |
int spaceCheck = 1; | |
for (double i = height; i > 0 ; i = i - 50) { | |
for (double j = width; j > 0 ; j = j - 25) { | |
if(spaceCheck % 2 == 0){ | |
Rectangle rectangle = new Rectangle(j,i,30,30); | |
rectangle.setFill(Color.RED); | |
scene.getChildren().add(rectangle); | |
bricks.add(rectangle); | |
} | |
spaceCheck++; | |
} | |
} | |
} | |
public void movePaddle(){ | |
Bounds bounds = scene.localToScreen(scene.getBoundsInLocal()); | |
double sceneXPos = bounds.getMinX(); | |
double xPos = robot.getMouseX(); | |
double paddleWidth = paddle.getWidth(); | |
if(xPos >= sceneXPos + (paddleWidth/2) && xPos <= (sceneXPos + scene.getWidth()) - (paddleWidth/2)){ | |
paddle.setLayoutX(xPos - sceneXPos - (paddleWidth/2)); | |
} else if (xPos < sceneXPos + (paddleWidth/2)){ | |
paddle.setLayoutX(0); | |
} else if (xPos > (sceneXPos + scene.getWidth()) - (paddleWidth/2)){ | |
paddle.setLayoutX(scene.getWidth() - paddleWidth); | |
} | |
} | |
public void checkCollisionPaddle(Rectangle paddle){ | |
if(circle.getBoundsInParent().intersects(paddle.getBoundsInParent())){ | |
boolean rightBorder = circle.getLayoutX() >= ((paddle.getLayoutX() + paddle.getWidth()) - circle.getRadius()); | |
boolean leftBorder = circle.getLayoutX() <= (paddle.getLayoutX() + circle.getRadius()); | |
boolean bottomBorder = circle.getLayoutY() >= ((paddle.getLayoutY() + paddle.getHeight()) - circle.getRadius()); | |
boolean topBorder = circle.getLayoutY() <= (paddle.getLayoutY() + circle.getRadius()); | |
if (rightBorder || leftBorder) { | |
deltaX *= -1; | |
} | |
if (bottomBorder || topBorder) { | |
deltaY *= -1; | |
} | |
} | |
} | |
public void checkCollisionBottomZone(){ | |
if(circle.getBoundsInParent().intersects(bottomZone.getBoundsInParent())){ | |
timeline.stop(); | |
bricks.forEach(brick -> scene.getChildren().remove(brick)); | |
bricks.clear(); | |
startButton.setVisible(true); | |
deltaX = -1; | |
deltaY = -3; | |
circle.setLayoutX(300); | |
circle.setLayoutY(300); | |
System.out.println("Game over!"); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample; | |
import javafx.application.Application; | |
import javafx.fxml.FXMLLoader; | |
import javafx.scene.Parent; | |
import javafx.scene.Scene; | |
import javafx.stage.Stage; | |
public class Main extends Application { | |
@Override | |
public void start(Stage primaryStage) throws Exception{ | |
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml")); | |
primaryStage.setTitle("Hello World"); | |
primaryStage.setScene(new Scene(root)); | |
primaryStage.show(); | |
} | |
public static void main(String[] args) { | |
launch(args); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<?import javafx.scene.control.Button?> | |
<?import javafx.scene.layout.AnchorPane?> | |
<?import javafx.scene.shape.Circle?> | |
<?import javafx.scene.shape.Rectangle?> | |
<?import javafx.scene.text.Font?> | |
<AnchorPane fx:id="scene" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller"> | |
<children> | |
<Circle fx:id="circle" fill="DODGERBLUE" layoutX="300.0" layoutY="300.0" radius="5.0" stroke="BLACK" strokeType="INSIDE" /> | |
<Rectangle fx:id="paddle" arcHeight="5.0" arcWidth="5.0" fill="DODGERBLUE" height="10.0" layoutX="256.0" layoutY="344.0" stroke="BLACK" strokeType="INSIDE" width="88.0" /> | |
<Rectangle fx:id="bottomZone" arcHeight="2.0" arcWidth="5.0" fill="TRANSPARENT" height="10.0" layoutY="395.0" stroke="BLACK" strokeType="INSIDE" strokeWidth="0.0" width="600.0" /> | |
<Button fx:id="startButton" layoutX="188.0" layoutY="145.0" mnemonicParsing="false" onAction="#startGameButtonAction" prefHeight="111.0" prefWidth="225.0" text="START"> | |
<font> | |
<Font size="44.0" /> | |
</font> | |
</Button> | |
</children> | |
</AnchorPane> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment