Skip to content

Commit

Permalink
Merge pull request kailielexx#2 from hoangsonww/main
Browse files Browse the repository at this point in the history
Added JavaFX GUI files for the game
  • Loading branch information
kailielexx authored Feb 1, 2024
2 parents b0872b8 + 3f0d91a commit fca787f
Show file tree
Hide file tree
Showing 21 changed files with 1,022 additions and 0 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/maven-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# This workflow will build a package using Maven and then publish it to GitHub packages when a release is created
# For more information see: https://github.com/actions/setup-java/blob/main/docs/advanced-usage.md#apache-maven-with-a-settings-path

name: Maven Package

on:
release:
types: [created]

jobs:
build:

runs-on: ubuntu-latest
permissions:
contents: read
packages: write

steps:
- uses: actions/checkout@v3
- name: Set up JDK 11
uses: actions/setup-java@v3
with:
java-version: '11'
distribution: 'temurin'
server-id: github # Value of the distributionManagement/repository/id field of the pom.xml
settings-path: ${{ github.workspace }} # location for the settings.xml file

- name: Build with Maven
run: mvn -B package --file pom.xml

- name: Publish to GitHub Packages Apache Maven
run: mvn deploy -s $GITHUB_WORKSPACE/settings.xml
env:
GITHUB_TOKEN: ${{ github.token }}
35 changes: 35 additions & 0 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-maven

# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

name: Java CI with Maven

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'
cache: maven
- name: Build with Maven
run: mvn -B package --file pom.xml

# Optional: Uploads the full dependency graph to GitHub to improve the quality of Dependabot alerts this repository can receive
- name: Update dependency graph
uses: advanced-security/maven-dependency-submission-action@571e99aab1055c2e71a1e2309b9691de18d6b7d6
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added .mvn/wrapper/maven-wrapper.jar
Binary file not shown.
2 changes: 2 additions & 0 deletions .mvn/wrapper/maven-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.8.5/apache-maven-3.8.5-bin.zip
wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar
69 changes: 69 additions & 0 deletions src/main/java/com/example/spaceshooterproject/BossEnemy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.example.spaceshooterproject;

import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;

import java.util.List;

public class BossEnemy extends Enemy {

private int health = 5;

private int WIDTH;

private int HEIGHT;

private int numHits = 5;

public BossEnemy(double x, double y) {
super(x, y);
SPEED = 1.0;
WIDTH = 50;
HEIGHT = 50;
health = 5;
}


@Override
public void update() {
// BossEnemy stays at the top of the screen
if (y < 40) {
y += SPEED;
}
}

public void takeDamage() {
health--;
if (health <= 0) {
setDead(true);
}
}

public void shoot(List<GameObject> newObjects) {
if (Math.random() < 0.015) {
newObjects.add(new EnemyBullet(x, y + HEIGHT / 2));
}
}

public void decreaseHealth() {
health--;
}

public boolean isDead() {
return health <= 0;
}

@Override
public void render(GraphicsContext gc) {
gc.setFill(Color.BLUE);
gc.fillRect(x - WIDTH / 2, y - HEIGHT / 2, WIDTH * 2, HEIGHT * 2);
}

public void hit() {
health--;
if (health <= 0) {
setDead(true);
}
}

}
48 changes: 48 additions & 0 deletions src/main/java/com/example/spaceshooterproject/Bullet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.example.spaceshooterproject;

import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;

public class Bullet extends GameObject {

public static final int WIDTH = 4;
public static final int HEIGHT = 20;
private static final double SPEED = 7;

public Bullet(double x, double y) {
super(x, y, WIDTH, HEIGHT); // Pass the WIDTH and HEIGHT constants
}

@Override
public void update() {
y -= SPEED;
}

@Override
public void render(GraphicsContext gc) {
gc.setFill(Color.YELLOW);
gc.fillRect(x - WIDTH / 2, y - HEIGHT / 2, WIDTH, HEIGHT);
}

// In Bullet class
@Override
public double getWidth() {
return WIDTH;
}

@Override
public double getHeight() {
return HEIGHT;
}

private boolean dead = false;

public void setDead(boolean dead) {
this.dead = dead;
}

@Override
public boolean isDead() {
return dead;
}
}
48 changes: 48 additions & 0 deletions src/main/java/com/example/spaceshooterproject/Enemy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.example.spaceshooterproject;

import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;

public class Enemy extends GameObject {

protected static final int WIDTH = 30;
protected static final int HEIGHT = 30;
public static double SPEED = 2;

public Enemy(double x, double y) {
super(x, y, WIDTH, HEIGHT);
}

@Override
public void update() {
y += SPEED;
}

@Override
public void render(GraphicsContext gc) {
gc.setFill(Color.RED);
gc.fillRect(x - WIDTH / 2, y - HEIGHT / 2, WIDTH, HEIGHT);
}

@Override
public double getWidth() {
return WIDTH;
}

@Override
public double getHeight() {
return HEIGHT;
}

private boolean dead = false;

public void setDead(boolean dead) {
this.dead = dead;
}

@Override
public boolean isDead() {
return dead;
}
}
47 changes: 47 additions & 0 deletions src/main/java/com/example/spaceshooterproject/EnemyBullet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.example.spaceshooterproject;

import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;

public class EnemyBullet extends GameObject {

public static final int WIDTH = 4;
public static final int HEIGHT = 20;
private static final double SPEED = 3;

public EnemyBullet(double x, double y) {
super(x, y, WIDTH, HEIGHT);
}

@Override
public void update() {
y += SPEED;
}

@Override
public void render(GraphicsContext gc) {
gc.setFill(Color.RED);
gc.fillRect(x - WIDTH / 2, y - HEIGHT / 2, WIDTH, HEIGHT);
}

@Override
public double getWidth() {
return WIDTH;
}

@Override
public double getHeight() {
return HEIGHT;
}

private boolean dead = false;

public void setDead(boolean dead) {
this.dead = dead;
}

@Override
public boolean isDead() {
return dead;
}
}
43 changes: 43 additions & 0 deletions src/main/java/com/example/spaceshooterproject/GameObject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.example.spaceshooterproject;

import javafx.scene.canvas.GraphicsContext;
import javafx.geometry.Bounds;
import javafx.scene.shape.Rectangle;


public abstract class GameObject {

protected double x;
protected double y;
protected double width;
protected double height;

public GameObject(double x, double y, double width, double height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}

public abstract void update();

public abstract void render(GraphicsContext gc);

public abstract boolean isDead();

public double getX() {
return x;
}

public double getY() {
return y;
}

public Bounds getBounds() {
return new Rectangle(x - getWidth() / 2, y - getHeight() / 2, getWidth(), getHeight()).getBoundsInLocal();
}

public abstract double getWidth();

public abstract double getHeight();
}
Loading

0 comments on commit fca787f

Please sign in to comment.