Skip to content

Instantly share code, notes, and snippets.

@Da9el00
Created August 12, 2021 08:55
Show Gist options
  • Save Da9el00/6f2e3353b22dd04014e714467cc21da1 to your computer and use it in GitHub Desktop.
Save Da9el00/6f2e3353b22dd04014e714467cc21da1 to your computer and use it in GitHub Desktop.
JavaFX and Scene Builder - Dodge the red rectangles!
package sample;
import javafx.animation.*;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.util.Duration;
import java.net.URL;
import java.util.ArrayList;
import java.util.Random;
import java.util.ResourceBundle;
public class Controller implements Initializable {
@FXML
private Rectangle shape1;
@FXML
private AnchorPane scene;
private Random random = new Random();
private TranslateTransition transition;
private ArrayList<Rectangle> dangerousRectangles = new ArrayList<>();
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
MovementController movementController = new MovementController(shape1, scene);
timeline.setCycleCount(Animation.INDEFINITE);
collisionTimer.start();
}
@FXML
void start(ActionEvent event) {
if(timeline.getStatus() == Animation.Status.RUNNING){
timeline.stop();
} else {
timeline.play();
}
}
Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(1), e ->{
Rectangle rectangle = createDangerShape();
scene.getChildren().add(rectangle);
dangerousRectangles.add(rectangle);
transition.play();
}));
AnimationTimer collisionTimer = new AnimationTimer() {
@Override
public void handle(long timestamp) {
for (Rectangle rectangle: dangerousRectangles) {
if(rectangle != null){
checkCollision(shape1,rectangle);
}
}
}
};
public void checkCollision(Rectangle shape1, Rectangle collisionShape){
if(shape1.getBoundsInParent().intersects(collisionShape.getBoundsInParent())){
timeline.stop();
System.out.println("Game over!");
}
}
public Rectangle createDangerShape(){
int recHeightX = random.nextInt(250);
int recHeight = 25 + random.nextInt(100);
int recWidth = 25 + random.nextInt(100);
Rectangle rectangle = new Rectangle(700,recHeightX,recHeight,recWidth);
rectangle.setFill(Color.RED);
transition = new TranslateTransition();
transition.setNode(rectangle);
transition.setDuration(Duration.seconds(5));
transition.setToX(-900);
transition.setOnFinished(e ->{
scene.getChildren().remove(rectangle);
dangerousRectangles.remove(rectangle);
});
return rectangle;
}
}
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);
}
}
package sample;
import javafx.animation.AnimationTimer;
import javafx.beans.binding.BooleanBinding;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.fxml.FXML;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.AnchorPane;
import javafx.scene.shape.Rectangle;
public class MovementController {
private BooleanProperty wPressed = new SimpleBooleanProperty();
private BooleanProperty aPressed = new SimpleBooleanProperty();
private BooleanProperty sPressed = new SimpleBooleanProperty();
private BooleanProperty dPressed = new SimpleBooleanProperty();
private BooleanBinding keyPressed = wPressed.or(aPressed).or(sPressed).or(dPressed);
private int movementVariable = 2;
@FXML
private Rectangle shape1;
@FXML
private AnchorPane scene;
public MovementController(Rectangle shape1, AnchorPane scene) {
this.shape1 = shape1;
this.scene = scene;
movementSetup();
keyPressed.addListener(((observableValue, aBoolean, t1) -> {
if(!aBoolean){
timer.start();
} else {
timer.stop();
}
}));
}
AnimationTimer timer = new AnimationTimer() {
@Override
public void handle(long timestamp) {
if(wPressed.get()) {
shape1.setLayoutY(shape1.getLayoutY() - movementVariable);
}
if(sPressed.get()){
shape1.setLayoutY(shape1.getLayoutY() + movementVariable);
}
if(aPressed.get()){
shape1.setLayoutX(shape1.getLayoutX() - movementVariable);
}
if(dPressed.get()){
shape1.setLayoutX(shape1.getLayoutX() + movementVariable);
}
}
};
public void movementSetup(){
scene.setOnKeyPressed(e -> {
if(e.getCode() == KeyCode.W) {
wPressed.set(true);
}
if(e.getCode() == KeyCode.A) {
aPressed.set(true);
}
if(e.getCode() == KeyCode.S) {
sPressed.set(true);
}
if(e.getCode() == KeyCode.D) {
dPressed.set(true);
}
});
scene.setOnKeyReleased(e ->{
if(e.getCode() == KeyCode.W) {
wPressed.set(false);
}
if(e.getCode() == KeyCode.A) {
aPressed.set(false);
}
if(e.getCode() == KeyCode.S) {
sPressed.set(false);
}
if(e.getCode() == KeyCode.D) {
dPressed.set(false);
}
});
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.shape.Rectangle?>
<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>
<Rectangle fx:id="shape1" arcHeight="5.0" arcWidth="5.0" fill="DODGERBLUE" height="40.0" layoutX="280.0" layoutY="200.0" stroke="BLACK" strokeType="INSIDE" width="40.0" />
<Button layoutX="278.0" layoutY="26.0" mnemonicParsing="false" onAction="#start" text="Start" />
</children>
</AnchorPane>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment