Skip to content

Instantly share code, notes, and snippets.

@Da9el00
Created April 9, 2022 17:54
Show Gist options
  • Save Da9el00/e5344fa140d3317e34e57607b92d96c5 to your computer and use it in GitHub Desktop.
Save Da9el00/e5344fa140d3317e34e57607b92d96c5 to your computer and use it in GitHub Desktop.
JavaFX and Scene Builder - Flappy Bird
package com.example.flappybird;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class Application extends javafx.application.Application {
@Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(Application.class.getResource("view.fxml"));
Scene scene = new Scene(fxmlLoader.load());
scene.getRoot().requestFocus();
stage.setTitle("Flappy Bird!");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
package com.example.flappybird;
import javafx.animation.AnimationTimer;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.shape.Rectangle;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable {
AnimationTimer gameLoop;
@FXML
private AnchorPane plane;
@FXML
private Rectangle bird;
double yDelta = 0.02 ;
double time = 0;
int jumpHeight = 100;
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
load();
gameLoop = new AnimationTimer() {
@Override
public void handle(long l) {
update();
}
};
gameLoop.start();
}
@FXML
void pressed(KeyEvent event) {
if(event.getCode() == KeyCode.SPACE){
fly();
}
}
private void fly(){
if(bird.getLayoutY() + bird.getY() <= jumpHeight){
moveBirdY(-(bird.getLayoutY() + bird.getY()));
time = 0;
return;
}
moveBirdY(-jumpHeight);
time = 0;
}
//Called every game frame
private void update() {
time ++;
moveBirdY(yDelta * time);
if(isBirdDead()){
resetBird();
}
}
//Everything called once, at the game start
private void load(){
System.out.println("Game starting");
}
private void moveBirdY(double positionChange){
bird.setY(bird.getY() + positionChange);
}
private boolean isBirdDead(){
double birdY = bird.getLayoutY() + bird.getY();
return birdY >= plane.getHeight();
}
private void resetBird(){
bird.setY(0);
time = 0;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.shape.Rectangle?>
<AnchorPane fx:id="plane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" onKeyPressed="#pressed" prefHeight="600.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.flappybird.Controller">
<children>
<Rectangle fx:id="bird" arcHeight="5.0" arcWidth="5.0" fill="#fcba03" height="50.0" layoutX="175.0" layoutY="150.0" stroke="BLACK" strokeType="INSIDE" strokeWidth="0.0" width="50.0" />
</children>
</AnchorPane>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment