Skip to content

Instantly share code, notes, and snippets.

@arwankhoiruddin
Created March 13, 2020 01:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arwankhoiruddin/068dbde27eddd36cbfe3cbf4b1028839 to your computer and use it in GitHub Desktop.
Save arwankhoiruddin/068dbde27eddd36cbfe3cbf4b1028839 to your computer and use it in GitHub Desktop.
An example of my flappy balloon JavaFX Project
package core;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import libs.Configs;
import libs.CoreFunc;
import libs.GameText;
import java.util.ArrayList;
import java.util.Random;
public class GameCore implements CoreFunc {
// Main Game variables should be here
Background background = new Background();
Balloon balloon = new Balloon();
GameText text = new GameText(Color.YELLOW, Color.YELLOW);
int textX = 10;
int textY = 50;
int fontSize = 30;
int balloonX = 500;
int balloonY = 300;
int speed = 2;
int hole = 160;
int wallWidth = 50;
int wallHeight = (Configs.appHeight - hole) / 2;
Wall wall1 = new Wall();
int wallX = Configs.appWidth - wallWidth;
int wallY = 0;
Wall wall2 = new Wall();
int wall2X = Configs.appWidth - wallWidth;
int wall2Y = Configs.appHeight - wallHeight;
int wall1Height = wallHeight;
@Override
public void init(GraphicsContext gc) {
// initialize objects (initial position, initial size, etc)
background.resize(Configs.appWidth, Configs.appHeight);
background.render(gc, 0, 0);
wall1.resize(wallWidth, wallHeight);
wall1.render(gc, wallX, wallY);
wall2.resize(wallWidth, wallHeight);
wall2.render(gc, wall2X, wall2Y);
balloon.resize(80, 100);
balloon.render(gc, balloonX, balloonY);
System.out.println("Initial wall height: " + wallHeight);
text.setText(gc, "Score: ", fontSize, textX, textY);
}
@Override
public void animate(GraphicsContext gc, int time, ArrayList input) {
// any animations and keyboard controls should be here
if (wallX <= 0) {
wallX = Configs.appWidth - wallWidth;
wall2X = Configs.appWidth - wallWidth;
if (speed < 5)
speed++;
hole = 150 + new Random().nextInt(50);
wall1Height = 50 + new Random().nextInt(300);
wallHeight = (Configs.appHeight - hole) - wall1Height;
System.out.println("wall 2 height: " + wallHeight);
wall2Y = Configs.appHeight - wallHeight;
}
wall1.resize(wallWidth, wall1Height);
wall2.resize(wallWidth, wallHeight);
// randomize the hole size
background.render(gc, 0, 0);
wallX -= speed;
wall2X -= speed;
wall1.render(gc, wallX, wallY);
wall2.render(gc, wall2X, wall2Y);
balloon.render(gc, balloonX, balloonY);
if (input.contains("UP"))
balloonY-=2;
else if (input.contains("DOWN"))
balloonY+=2;
if (balloon.collide(wall1))
System.exit(0);
if (balloon.collide(wall2))
System.exit(0);
text.setText(gc, "Score: ", fontSize, textX, textY);
}
@Override
public void mouseClick(MouseEvent e) {
// mouse click event here
}
@Override
public void mouseMoved(MouseEvent e) {
// mouse move event here
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment