Game Engine - Pt.1
The basic structure of the applicaiton is built focusing on screen formatting and screen size and app termination.
RunNGunApp.java
package ceccs;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import java.util.ArrayList;
public class RunNGunApp extends Application {
Player player;
GameTimer gTimer;
StackPane outerStackPane;
Pane motionPane;
double physW,physH;
Rectangle backgroundRect;
@Override
public void start(Stage primaryStage) throws Exception {
physH = 600;
physW = 800;
motionPane = new Pane();
backgroundRect = new Rectangle(physW,physH, Color.BLACK);
outerStackPane = new StackPane(backgroundRect, motionPane);
Scene scn = new Scene(outerStackPane, physW, physH);
primaryStage.setScene(scn);
primaryStage.setResizable(false);
primaryStage.show();
player = new Player(this);
gTimer = new GameTimer(this);
//*motion test*//
player.vx = .1;
player.vy = .1;
gTimer.start();
}
public static void main(String[] args) {
launch(args);
}
}
GameTimer.java
package ceccs;
import javafx.animation.AnimationTimer;
public class GameTimer extends AnimationTimer {
RunNGunApp app;
public GameTimer(RunNGunApp app){
this.app = app;
}
@Override
public void start() {
super.start();
}
@Override
public void handle(long now) {
updatePhysics();
updateGraphics();
}
void updatePhysics(){
app.player.updatePhysics();
}
void updateGraphics(){
app.player.updateGraphics();
}
}
Player.java
package ceccs;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
public class Player extends Circle {
RunNGunApp app;
double x, y, vx, vy, ax, ay, size;
public Player(RunNGunApp app){
this.app = app;
size = 5;
setRadius(size);
setFill(Color.LIME);
app.motionPane.getChildren().add(this);
}
void updatePhysics(){
vx += ax;
x += vx;
vy += ay;
y += vy;
}
void updateGraphics(){
setLayoutX(x);
setLayoutY(y);
setRadius(size);
}
}