In: Computer Science
I
have a JavaFx game that you have a rectangle and you move it with the arrow keys to shoot circles that I call enemies. I want to add some collision to the rectangle so that when it hits the circles the game ends and I also want to add a counter so that each time I shoot an enemy it adds 2 points and when you die it prints out how many points you get.
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.geometry.Point2D;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import java.util.ArrayList;
import java.util.List;
public class MainClass extends Application {
// Create Constructors
public Pane root;
public List<GameObject> bullets = new
ArrayList<>();
public List<GameObject> enemies = new
ArrayList<>();
public GameObject player;
public Parent createContent() {
root = new Pane();
root.setPrefSize(500, 500);
root.setBackground(new Background(new BackgroundFill(Color.BLACK,
null, null),null, null));
player = new Player();
player.setVelocity(new Point2D(1, 0));
addGameObject(player, 300, 300);
// Start Animation Timer
AnimationTimer timer = new AnimationTimer() {
@Override
public void handle(long now) {
onUpdate();
}
};
timer.start();
return root;
}
// Add the bullets and enemies
public void addBullet(GameObject bullet, double x, double y)
{
bullets.add(bullet);
addGameObject(bullet, x, y);
}
public void addEnemy(GameObject enemy, double x, double y)
{
enemies.add(enemy);
addGameObject(enemy, x, y);
}
public void addGameObject(GameObject object, double x, double y)
{
object.getView().setTranslateX(x);
object.getView().setTranslateY(y);
root.getChildren().add(object.getView());
}
// Provide collions for the bullets and enimes
public void onUpdate() {
for (GameObject bullet : bullets) {
for (GameObject enemy : enemies) {
if (bullet.isColliding(enemy)) {
bullet.setAlive(false);
enemy.setAlive(false);
root.getChildren().removeAll(bullet.getView(),
enemy.getView());
}
}
}
bullets.removeIf(GameObject::isDead);
enemies.removeIf(GameObject::isDead);
bullets.forEach(GameObject::update);
enemies.forEach(GameObject::update);
player.update();
// Generate the enemies
if (Math.random() < 0.03) {
addEnemy(new Enemy(), Math.random() * root.getPrefWidth(),
Math.random() * root.getPrefHeight());
}
}
// Inherit the player,bullets, and enemies from the GameObject
class
public class Player extends GameObject {
Player() {
super(new Rectangle(40, 20, Color.BLUE));
}
}
public class Enemy extends GameObject {
Enemy() {
super(new Circle(15, 15, 15, Color.RED));
}
}
public class Bullet extends GameObject {
Bullet() {
super(new Circle(5, 5, 5, Color.BROWN));
}
}
// Shoot the bullets
@Override
public void start(Stage stage) throws Exception {
stage.setScene(new Scene(createContent()));
stage.getScene().setOnKeyPressed(e -> {
if (e.getCode() == KeyCode.LEFT) {
player.rotateLeft();
} else if (e.getCode() == KeyCode.RIGHT) {
player.rotateRight();
} else if (e.getCode() == KeyCode.SPACE) {
Bullet bullet = new Bullet();
bullet.setVelocity(player.getVelocity().normalize().multiply(5));
addBullet(bullet, player.getView().getTranslateX(),
player.getView().getTranslateY());
}
});
stage.show();
}
public static void main(String[] args) {
launch(args);
}
GameObject
import javafx.geometry.Point2D;
import javafx.scene.Node;
public class GameObject {
public Node view;
public Point2D velocity = new Point2D(0, 0);
public boolean alive = true;
public GameObject(Node view) {
this.view = view;
}
public void update() {
view.setTranslateX(view.getTranslateX() + velocity.getX());
view.setTranslateY(view.getTranslateY() + velocity.getY());
}
public void setVelocity(Point2D velocity) {
this.velocity = velocity;
}
public Point2D getVelocity() {
return velocity;
}
public Node getView() {
return view;
}
public boolean isAlive() {
return alive;
}
public boolean isDead() {
return !alive;
}
public void setAlive(boolean alive) {
this.alive = alive;
}
public double getRotate() {
return view.getRotate();
}
public void rotateRight() {
view.setRotate(view.getRotate() + 10);
setVelocity(new Point2D(Math.cos(Math.toRadians(getRotate())),
Math.sin(Math.toRadians(getRotate()))));
}
public void rotateLeft() {
view.setRotate(view.getRotate() - 10);
setVelocity(new Point2D(Math.cos(Math.toRadians(getRotate())),
Math.sin(Math.toRadians(getRotate()))));
}
public boolean isColliding(GameObject other) {
return
getView().getBoundsInParent().intersects(other.getView().getBoundsInParent());
}
}
// GameObject.java
import javafx.geometry.Point2D;
import javafx.scene.Node;
public class GameObject {
public Node view;
public Point2D velocity = new Point2D(0, 0);
public boolean alive = true;
public GameObject(Node view) {
this.view = view;
}
public void update() {
view.setTranslateX(view.getTranslateX() + velocity.getX());
view.setTranslateY(view.getTranslateY() + velocity.getY());
}
public void setVelocity(Point2D velocity) {
this.velocity = velocity;
}
public Point2D getVelocity() {
return velocity;
}
public Node getView() {
return view;
}
public boolean isAlive() {
return alive;
}
public boolean isDead() {
return !alive;
}
public void setAlive(boolean alive) {
this.alive = alive;
}
public double getRotate() {
return view.getRotate();
}
public void rotateRight() {
view.setRotate(view.getRotate() +
10);
setVelocity(new
Point2D(Math.cos(Math.toRadians(getRotate())),
Math.sin(Math.toRadians(getRotate()))));
}
public void rotateLeft() {
view.setRotate(view.getRotate() -
10);
setVelocity(new
Point2D(Math.cos(Math.toRadians(getRotate())),
Math.sin(Math.toRadians(getRotate()))));
}
public boolean isColliding(GameObject other)
{
return
getView().getBoundsInParent().intersects(other.getView().getBoundsInParent());
}
}
// MainClass.java
import java.util.ArrayList;
import java.util.List;
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.geometry.Point2D;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class MainClass extends Application {
// Create Constructors
public Pane root;
public List<GameObject> bullets = new
ArrayList<>();
public List<GameObject> enemies = new
ArrayList<>();
public int score = 0;
public GameObject player;
public Parent createContent() {
root = new Pane();
root.setPrefSize(500, 500);
root.setBackground(new
Background(new BackgroundFill(Color.BLACK, null, null), null,
null));
player = new Player();
player.setVelocity(new Point2D(1,
0));
addGameObject(player, 300,
300);
// Start Animation Timer
AnimationTimer timer = new
AnimationTimer() {
@Override
public void
handle(long now) {
if(player.isAlive())
onUpdate();
else
{
showScore();
this.stop();
}
}
};
timer.start();
return root;
}
public void showScore() {
GameObject g = new GameObject(new
Rectangle(root.getHeight(),root.getWidth()));
root.getChildren().add(g.getView());
Label l = new Label("Game
Over\nYour Score is: "+score);
l.setAlignment(Pos.CENTER);
l.setFont((new Font("Arial",
30)));
l.setTextFill(Color.WHITE);
root.getChildren().add(l);
}
// Add the bullets and enemies
public void addBullet(GameObject bullet, double x,
double y) {
bullets.add(bullet);
addGameObject(bullet, x, y);
}
public void addEnemy(GameObject enemy, double x,
double y) {
enemies.add(enemy);
addGameObject(enemy, x, y);
}
public void addGameObject(GameObject object, double
x, double y) {
object.getView().setTranslateX(x);
object.getView().setTranslateY(y);
root.getChildren().add(object.getView());
}
// Provide collions for the bullets and enimes
public void onUpdate() {
for (GameObject bullet : bullets)
{
for (GameObject
enemy : enemies) {
if (bullet.isColliding(enemy)) {
bullet.setAlive(false);
enemy.setAlive(false);
score += 2;
root.getChildren().removeAll(bullet.getView(),
enemy.getView());
}
}
}
for (GameObject enemy : enemies)
{
if
(player.isColliding(enemy)) {
player.setAlive(false);
root.getChildren().removeAll(player.getView());
}
}
bullets.removeIf(GameObject::isDead);
enemies.removeIf(GameObject::isDead);
bullets.forEach(GameObject::update);
enemies.forEach(GameObject::update);
player.update();
// Generate the enemies
if (Math.random() < 0.03)
{
addEnemy(new
Enemy(), Math.random() * root.getPrefWidth(), Math.random() *
root.getPrefHeight());
}
}
// Inherit the player,bullets, and enemies from the GameObject class
public class Player extends GameObject {
Player() {
super(new
Rectangle(40, 20, Color.BLUE));
}
}
public class Enemy extends GameObject {
Enemy() {
super(new
Circle(15, 15, 15, Color.RED));
}
}
public class Bullet extends GameObject {
Bullet() {
super(new
Circle(5, 5, 5, Color.BROWN));
}
}
// Shoot the bullets
@Override
public void start(Stage stage) throws Exception
{
stage.setScene(new
Scene(createContent()));
stage.getScene().setOnKeyPressed(e
-> {
if (e.getCode()
== KeyCode.LEFT) {
player.rotateLeft();
} else if
(e.getCode() == KeyCode.RIGHT) {
player.rotateRight();
} else if
(e.getCode() == KeyCode.SPACE) {
Bullet bullet = new Bullet();
bullet.setVelocity(player.getVelocity().normalize().multiply(5));
addBullet(bullet,
player.getView().getTranslateX(),
player.getView().getTranslateY());
}
});
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}