├── .mvn
└── wrapper
│ ├── maven-wrapper.jar
│ └── maven-wrapper.properties
├── target
└── classes
│ ├── module-info.class
│ └── com
│ └── example
│ └── spaceshooterproject
│ ├── Enemy.class
│ ├── rocket.jpg
│ ├── Bullet.class
│ ├── Player.class
│ ├── PowerUp.class
│ ├── BossEnemy.class
│ ├── GameObject.class
│ ├── EnemyBullet.class
│ ├── SpaceShooter.class
│ ├── HelloController.class
│ ├── HelloApplication.class
│ └── hello-view.fxml
├── src
└── main
│ ├── resources
│ └── com
│ │ └── example
│ │ └── spaceshooterproject
│ │ ├── rocket.jpg
│ │ └── hello-view.fxml
│ └── java
│ ├── module-info.java
│ └── com
│ └── example
│ └── spaceshooterproject
│ ├── HelloController.java
│ ├── HelloApplication.java
│ ├── GameObject.java
│ ├── EnemyBullet.java
│ ├── PowerUp.java
│ ├── Enemy.java
│ ├── Bullet.java
│ ├── BossEnemy.java
│ ├── Player.java
│ └── SpaceShooter.java
├── .idea
├── vcs.xml
├── .gitignore
├── encodings.xml
└── misc.xml
├── vercel.json
├── .gitignore
├── .github
└── workflows
│ ├── maven-publish.yml
│ └── maven.yml
├── README.md
├── pom.xml
├── mvnw.cmd
└── mvnw
/.mvn/wrapper/maven-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hoangsonww/javafx-space-shooter/HEAD/.mvn/wrapper/maven-wrapper.jar
--------------------------------------------------------------------------------
/target/classes/module-info.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hoangsonww/javafx-space-shooter/HEAD/target/classes/module-info.class
--------------------------------------------------------------------------------
/target/classes/com/example/spaceshooterproject/Enemy.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hoangsonww/javafx-space-shooter/HEAD/target/classes/com/example/spaceshooterproject/Enemy.class
--------------------------------------------------------------------------------
/target/classes/com/example/spaceshooterproject/rocket.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hoangsonww/javafx-space-shooter/HEAD/target/classes/com/example/spaceshooterproject/rocket.jpg
--------------------------------------------------------------------------------
/target/classes/com/example/spaceshooterproject/Bullet.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hoangsonww/javafx-space-shooter/HEAD/target/classes/com/example/spaceshooterproject/Bullet.class
--------------------------------------------------------------------------------
/target/classes/com/example/spaceshooterproject/Player.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hoangsonww/javafx-space-shooter/HEAD/target/classes/com/example/spaceshooterproject/Player.class
--------------------------------------------------------------------------------
/target/classes/com/example/spaceshooterproject/PowerUp.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hoangsonww/javafx-space-shooter/HEAD/target/classes/com/example/spaceshooterproject/PowerUp.class
--------------------------------------------------------------------------------
/src/main/resources/com/example/spaceshooterproject/rocket.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hoangsonww/javafx-space-shooter/HEAD/src/main/resources/com/example/spaceshooterproject/rocket.jpg
--------------------------------------------------------------------------------
/target/classes/com/example/spaceshooterproject/BossEnemy.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hoangsonww/javafx-space-shooter/HEAD/target/classes/com/example/spaceshooterproject/BossEnemy.class
--------------------------------------------------------------------------------
/target/classes/com/example/spaceshooterproject/GameObject.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hoangsonww/javafx-space-shooter/HEAD/target/classes/com/example/spaceshooterproject/GameObject.class
--------------------------------------------------------------------------------
/target/classes/com/example/spaceshooterproject/EnemyBullet.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hoangsonww/javafx-space-shooter/HEAD/target/classes/com/example/spaceshooterproject/EnemyBullet.class
--------------------------------------------------------------------------------
/target/classes/com/example/spaceshooterproject/SpaceShooter.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hoangsonww/javafx-space-shooter/HEAD/target/classes/com/example/spaceshooterproject/SpaceShooter.class
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/target/classes/com/example/spaceshooterproject/HelloController.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hoangsonww/javafx-space-shooter/HEAD/target/classes/com/example/spaceshooterproject/HelloController.class
--------------------------------------------------------------------------------
/target/classes/com/example/spaceshooterproject/HelloApplication.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hoangsonww/javafx-space-shooter/HEAD/target/classes/com/example/spaceshooterproject/HelloApplication.class
--------------------------------------------------------------------------------
/.idea/.gitignore:
--------------------------------------------------------------------------------
1 | # Default ignored files
2 | /shelf/
3 | /workspace.xml
4 | # Editor-based HTTP Client requests
5 | /httpRequests/
6 | # Datasource local storage ignored files
7 | /dataSources/
8 | /dataSources.local.xml
9 |
--------------------------------------------------------------------------------
/src/main/java/module-info.java:
--------------------------------------------------------------------------------
1 | module com.example.spaceshooterproject {
2 | requires javafx.controls;
3 | requires javafx.fxml;
4 |
5 |
6 | opens com.example.spaceshooterproject to javafx.fxml;
7 | exports com.example.spaceshooterproject;
8 | }
--------------------------------------------------------------------------------
/.mvn/wrapper/maven-wrapper.properties:
--------------------------------------------------------------------------------
1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.8.5/apache-maven-3.8.5-bin.zip
2 | wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/vercel.json:
--------------------------------------------------------------------------------
1 | {
2 | "functions": {
3 | "api/*.js": {
4 | "memory": 128,
5 | "maxDuration": 10
6 | }
7 | },
8 | "redirects": [
9 | {
10 | "source": "/",
11 | "destination": "https://github.com/anuraghazra/github-readme-stats"
12 | }
13 | ]
14 | }
15 |
--------------------------------------------------------------------------------
/src/main/java/com/example/spaceshooterproject/HelloController.java:
--------------------------------------------------------------------------------
1 | package com.example.spaceshooterproject;
2 |
3 | import javafx.fxml.FXML;
4 | import javafx.scene.control.Label;
5 |
6 | public class HelloController {
7 | @FXML
8 | private Label welcomeText;
9 |
10 | @FXML
11 | protected void onHelloButtonClick() {
12 | welcomeText.setText("Welcome to JavaFX Application!");
13 | }
14 | }
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | !.mvn/wrapper/maven-wrapper.jar
2 | !**/src/main/**/target/
3 | !**/src/test/**/target/
4 |
5 | ### IntelliJ IDEA ###
6 | .idea/modules.xml
7 | .idea/jarRepositories.xml
8 | .idea/compiler.xml
9 | .idea/libraries/
10 | *.iws
11 | *.iml
12 | *.ipr
13 |
14 | ### Eclipse ###
15 | .apt_generated
16 | .classpath
17 | .factorypath
18 | .project
19 | .settings
20 | .springBeans
21 | .sts4-cache
22 |
23 | ### NetBeans ###
24 | /nbproject/private/
25 | /nbbuild/
26 | /dist/
27 | /nbdist/
28 | /.nb-gradle/
29 | build/
30 | !**/src/main/**/build/
31 | !**/src/test/**/build/
32 |
33 | ### VS Code ###
34 | .vscode/
35 |
36 | ### Mac OS ###
37 | .DS_Store
--------------------------------------------------------------------------------
/src/main/resources/com/example/spaceshooterproject/hello-view.fxml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/target/classes/com/example/spaceshooterproject/hello-view.fxml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/src/main/java/com/example/spaceshooterproject/HelloApplication.java:
--------------------------------------------------------------------------------
1 | package com.example.spaceshooterproject;
2 |
3 | import javafx.application.Application;
4 | import javafx.fxml.FXMLLoader;
5 | import javafx.scene.Scene;
6 | import javafx.stage.Stage;
7 |
8 | import java.io.IOException;
9 |
10 | public class HelloApplication extends Application {
11 | @Override
12 | public void start(Stage stage) throws IOException {
13 | FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
14 | Scene scene = new Scene(fxmlLoader.load(), 320, 240);
15 | stage.setTitle("Hello!");
16 | stage.setScene(scene);
17 | stage.show();
18 | }
19 |
20 | public static void main(String[] args) {
21 | launch();
22 | }
23 | }
--------------------------------------------------------------------------------
/.github/workflows/maven-publish.yml:
--------------------------------------------------------------------------------
1 | # This workflow will build a package using Maven and then publish it to GitHub packages when a release is created
2 | # For more information see: https://github.com/actions/setup-java/blob/main/docs/advanced-usage.md#apache-maven-with-a-settings-path
3 |
4 | name: Maven Package
5 |
6 | on:
7 | release:
8 | types: [created]
9 |
10 | jobs:
11 | build:
12 |
13 | runs-on: ubuntu-latest
14 | permissions:
15 | contents: read
16 | packages: write
17 |
18 | steps:
19 | - uses: actions/checkout@v3
20 | - name: Set up JDK 11
21 | uses: actions/setup-java@v3
22 | with:
23 | java-version: '11'
24 | distribution: 'temurin'
25 | server-id: github # Value of the distributionManagement/repository/id field of the pom.xml
26 | settings-path: ${{ github.workspace }} # location for the settings.xml file
27 |
28 | - name: Build with Maven
29 | run: mvn -B package --file pom.xml
30 |
31 | - name: Publish to GitHub Packages Apache Maven
32 | run: mvn deploy -s $GITHUB_WORKSPACE/settings.xml
33 | env:
34 | GITHUB_TOKEN: ${{ github.token }}
35 |
--------------------------------------------------------------------------------
/src/main/java/com/example/spaceshooterproject/GameObject.java:
--------------------------------------------------------------------------------
1 | package com.example.spaceshooterproject;
2 |
3 | import javafx.scene.canvas.GraphicsContext;
4 | import javafx.geometry.Bounds;
5 | import javafx.scene.shape.Rectangle;
6 |
7 |
8 | public abstract class GameObject {
9 |
10 | protected double x;
11 | protected double y;
12 | protected double width;
13 | protected double height;
14 |
15 | public GameObject(double x, double y, double width, double height) {
16 | this.x = x;
17 | this.y = y;
18 | this.width = width;
19 | this.height = height;
20 | }
21 |
22 | public abstract void update();
23 |
24 | public abstract void render(GraphicsContext gc);
25 |
26 | public abstract boolean isDead();
27 |
28 | public double getX() {
29 | return x;
30 | }
31 |
32 | public double getY() {
33 | return y;
34 | }
35 |
36 | public Bounds getBounds() {
37 | return new Rectangle(x - getWidth() / 2, y - getHeight() / 2, getWidth(), getHeight()).getBoundsInLocal();
38 | }
39 |
40 | public abstract double getWidth();
41 |
42 | public abstract double getHeight();
43 | }
44 |
--------------------------------------------------------------------------------
/src/main/java/com/example/spaceshooterproject/EnemyBullet.java:
--------------------------------------------------------------------------------
1 | package com.example.spaceshooterproject;
2 |
3 | import javafx.scene.canvas.GraphicsContext;
4 | import javafx.scene.paint.Color;
5 |
6 | public class EnemyBullet extends GameObject {
7 |
8 | public static final int WIDTH = 4;
9 | public static final int HEIGHT = 20;
10 | private static final double SPEED = 3;
11 |
12 | public EnemyBullet(double x, double y) {
13 | super(x, y, WIDTH, HEIGHT);
14 | }
15 |
16 | @Override
17 | public void update() {
18 | y += SPEED;
19 | }
20 |
21 | @Override
22 | public void render(GraphicsContext gc) {
23 | gc.setFill(Color.RED);
24 | gc.fillRect(x - WIDTH / 2, y - HEIGHT / 2, WIDTH, HEIGHT);
25 | }
26 |
27 | @Override
28 | public double getWidth() {
29 | return WIDTH;
30 | }
31 |
32 | @Override
33 | public double getHeight() {
34 | return HEIGHT;
35 | }
36 |
37 | private boolean dead = false;
38 |
39 | public void setDead(boolean dead) {
40 | this.dead = dead;
41 | }
42 |
43 | @Override
44 | public boolean isDead() {
45 | return dead;
46 | }
47 | }
--------------------------------------------------------------------------------
/src/main/java/com/example/spaceshooterproject/PowerUp.java:
--------------------------------------------------------------------------------
1 | package com.example.spaceshooterproject;
2 |
3 | import javafx.scene.canvas.GraphicsContext;
4 | import javafx.scene.paint.Color;
5 |
6 | public class PowerUp extends GameObject {
7 |
8 | public static final int WIDTH = 20;
9 | public static final int HEIGHT = 20;
10 |
11 | private static final double SPEED = 2;
12 |
13 | private boolean isDead = false;
14 |
15 | public PowerUp(double x, double y) {
16 | super(x, y, WIDTH, HEIGHT);
17 | }
18 |
19 | @Override
20 | public void update() {
21 | y += SPEED;
22 | }
23 |
24 | @Override
25 | public void render(GraphicsContext gc) {
26 | gc.setFill(Color.GREEN);
27 | gc.fillRect(x - WIDTH / 2, y - HEIGHT / 2, WIDTH, HEIGHT);
28 | }
29 |
30 | @Override
31 | public double getWidth() {
32 | return WIDTH;
33 | }
34 |
35 | @Override
36 | public double getHeight() {
37 | return HEIGHT;
38 | }
39 |
40 | @Override
41 | public boolean isDead() {
42 | return this.isDead;
43 | }
44 |
45 | public void setDead(boolean b) {
46 | this.isDead = b;
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/src/main/java/com/example/spaceshooterproject/Enemy.java:
--------------------------------------------------------------------------------
1 | package com.example.spaceshooterproject;
2 |
3 | import javafx.scene.canvas.GraphicsContext;
4 | import javafx.scene.paint.Color;
5 | import javafx.scene.shape.Rectangle;
6 |
7 | public class Enemy extends GameObject {
8 |
9 | protected static final int WIDTH = 30;
10 | protected static final int HEIGHT = 30;
11 | public static double SPEED = 2;
12 |
13 | public Enemy(double x, double y) {
14 | super(x, y, WIDTH, HEIGHT);
15 | }
16 |
17 | @Override
18 | public void update() {
19 | y += SPEED;
20 | }
21 |
22 | @Override
23 | public void render(GraphicsContext gc) {
24 | gc.setFill(Color.RED);
25 | gc.fillRect(x - WIDTH / 2, y - HEIGHT / 2, WIDTH, HEIGHT);
26 | }
27 |
28 | @Override
29 | public double getWidth() {
30 | return WIDTH;
31 | }
32 |
33 | @Override
34 | public double getHeight() {
35 | return HEIGHT;
36 | }
37 |
38 | private boolean dead = false;
39 |
40 | public void setDead(boolean dead) {
41 | this.dead = dead;
42 | }
43 |
44 | @Override
45 | public boolean isDead() {
46 | return dead;
47 | }
48 | }
--------------------------------------------------------------------------------
/src/main/java/com/example/spaceshooterproject/Bullet.java:
--------------------------------------------------------------------------------
1 | package com.example.spaceshooterproject;
2 |
3 | import javafx.scene.canvas.GraphicsContext;
4 | import javafx.scene.paint.Color;
5 |
6 | public class Bullet extends GameObject {
7 |
8 | public static final int WIDTH = 4;
9 | public static final int HEIGHT = 20;
10 | private static final double SPEED = 7;
11 |
12 | public Bullet(double x, double y) {
13 | super(x, y, WIDTH, HEIGHT); // Pass the WIDTH and HEIGHT constants
14 | }
15 |
16 | @Override
17 | public void update() {
18 | y -= SPEED;
19 | }
20 |
21 | @Override
22 | public void render(GraphicsContext gc) {
23 | gc.setFill(Color.YELLOW);
24 | gc.fillRect(x - WIDTH / 2, y - HEIGHT / 2, WIDTH, HEIGHT);
25 | }
26 |
27 | // In Bullet class
28 | @Override
29 | public double getWidth() {
30 | return WIDTH;
31 | }
32 |
33 | @Override
34 | public double getHeight() {
35 | return HEIGHT;
36 | }
37 |
38 | private boolean dead = false;
39 |
40 | public void setDead(boolean dead) {
41 | this.dead = dead;
42 | }
43 |
44 | @Override
45 | public boolean isDead() {
46 | return dead;
47 | }
48 | }
--------------------------------------------------------------------------------
/.github/workflows/maven.yml:
--------------------------------------------------------------------------------
1 | # This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
2 | # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-maven
3 |
4 | # This workflow uses actions that are not certified by GitHub.
5 | # They are provided by a third-party and are governed by
6 | # separate terms of service, privacy policy, and support
7 | # documentation.
8 |
9 | name: Java CI with Maven
10 |
11 | on:
12 | push:
13 | branches: [ "main" ]
14 | pull_request:
15 | branches: [ "main" ]
16 |
17 | jobs:
18 | build:
19 |
20 | runs-on: ubuntu-latest
21 |
22 | steps:
23 | - uses: actions/checkout@v3
24 | - name: Set up JDK 17
25 | uses: actions/setup-java@v3
26 | with:
27 | java-version: '17'
28 | distribution: 'temurin'
29 | cache: maven
30 | - name: Build with Maven
31 | run: mvn -B package --file pom.xml
32 |
33 | # Optional: Uploads the full dependency graph to GitHub to improve the quality of Dependabot alerts this repository can receive
34 | - name: Update dependency graph
35 | uses: advanced-security/maven-dependency-submission-action@571e99aab1055c2e71a1e2309b9691de18d6b7d6
36 |
--------------------------------------------------------------------------------
/src/main/java/com/example/spaceshooterproject/BossEnemy.java:
--------------------------------------------------------------------------------
1 | package com.example.spaceshooterproject;
2 |
3 | import javafx.scene.canvas.GraphicsContext;
4 | import javafx.scene.paint.Color;
5 |
6 | import java.util.List;
7 |
8 | public class BossEnemy extends Enemy {
9 |
10 | private int health = 5;
11 |
12 | private int WIDTH;
13 |
14 | private int HEIGHT;
15 |
16 | private int numHits = 5;
17 |
18 | public BossEnemy(double x, double y) {
19 | super(x, y);
20 | SPEED = 1.0;
21 | WIDTH = 50;
22 | HEIGHT = 50;
23 | health = 5;
24 | }
25 |
26 |
27 | @Override
28 | public void update() {
29 | // BossEnemy stays at the top of the screen
30 | if (y < 40) {
31 | y += SPEED;
32 | }
33 | }
34 |
35 | public void takeDamage() {
36 | health--;
37 | if (health <= 0) {
38 | setDead(true);
39 | }
40 | }
41 |
42 | public void shoot(List newObjects) {
43 | if (Math.random() < 0.015) {
44 | newObjects.add(new EnemyBullet(x, y + HEIGHT / 2));
45 | }
46 | }
47 |
48 | public void decreaseHealth() {
49 | health--;
50 | }
51 |
52 | public boolean isDead() {
53 | return health <= 0;
54 | }
55 |
56 | @Override
57 | public void render(GraphicsContext gc) {
58 | gc.setFill(Color.BLUE);
59 | gc.fillRect(x - WIDTH / 2, y - HEIGHT / 2, WIDTH * 2, HEIGHT * 2);
60 | }
61 |
62 | public void hit() {
63 | health--;
64 | if (health <= 0) {
65 | setDead(true);
66 | }
67 | }
68 |
69 | }
70 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Space Shooter Game in JavaFX
2 |
3 | ## Overview
4 | Space Shooter is a thrilling arcade-style game developed using JavaFX, where players navigate a spaceship through a galaxy filled with enemies and obstacles. The objective is to survive as long as possible while destroying enemy ships and dodging incoming fire to achieve the highest score.
5 |
6 | ## Features
7 | - **Dynamic Enemies:** Encounter various types of enemy ships with different behaviors and attack patterns.
8 | - **Power-ups:** Collect power-ups to enhance your spaceship with better weapons and shields.
9 | - **Score System:** Earn points by destroying enemies and advancing through levels.
10 | - **Difficulty Levels:** The game offers multiple difficulty levels to challenge both beginners and seasoned players.
11 | - **Customizable Controls:** Players can customize their control scheme to suit their playstyle.
12 |
13 | ## Prerequisites
14 | To run this game, you'll need to have the following installed:
15 | - Java Development Kit (JDK) version 8 or higher
16 | - JavaFX SDK (compatible with your JDK version)
17 |
18 | Ensure that your JAVA_HOME environment variable is set to your JDK installation path.
19 |
20 | ## Installation and Running the Game
21 |
22 | ### Step 1: Clone the Repository
23 |
24 | ### Step 2: Compile the Source Code
25 | ```sh
26 | javac -cp "path/to/javafx-sdk/lib/*" src/*.java -d bin
27 | ```
28 |
29 | ### Step 3: Run the Game
30 | ```sh
31 | java --module-path "path/to/javafx-sdk/lib" --add-modules javafx.controls,javafx.fxml -cp bin Main
32 | ```
33 |
34 | Replace `"path/to/javafx-sdk/lib"` with the actual path to your JavaFX SDK libraries.
35 |
36 | ## Dependencies
37 | - **JavaFX:** Used for creating the graphical interface and animations.
38 | - **JDK:** The Java Development Kit is required for compiling and running the Java application.
39 |
40 | ## Contributing
41 | Contributions to the Space Shooter game are welcome! Whether it involves fixing bugs, adding new features, or improving documentation, your help is appreciated. Please follow these steps to contribute:
42 |
43 | 1. Fork the repository.
44 | 2. Create a new branch (`git checkout -b feature/AmazingFeature`).
45 | 3. Commit your changes (`git commit -am 'Add some AmazingFeature'`).
46 | 4. Push to the branch (`git push origin feature/AmazingFeature`).
47 | 5. Open a Pull Request.
48 |
49 | ## License
50 | This project is licensed under the MIT License - see the LICENSE.md file for details.
51 |
52 | ## Acknowledgments
53 | - Special thanks to [JavaFX](https://openjfx.io/) for providing the graphical framework.
54 | - Appreciation for all contributors and players who have supported this project.
55 |
56 | ---
57 |
--------------------------------------------------------------------------------
/src/main/java/com/example/spaceshooterproject/Player.java:
--------------------------------------------------------------------------------
1 | package com.example.spaceshooterproject;
2 |
3 | import javafx.scene.canvas.GraphicsContext;
4 | import javafx.scene.paint.Color;
5 | import javafx.scene.shape.Rectangle;
6 | import java.util.List;
7 |
8 |
9 | public class Player extends GameObject {
10 |
11 | private static final int WIDTH = 20;
12 | private static final int HEIGHT = 20;
13 | private static final double SPEED = 5;
14 | private boolean moveLeft;
15 | private boolean moveRight;
16 | private boolean moveForward;
17 | private boolean moveBackward;
18 | private int health = 20;
19 |
20 | // In Player class
21 | @Override
22 | public double getWidth() {
23 | return WIDTH;
24 | }
25 |
26 | @Override
27 | public double getHeight() {
28 | return HEIGHT;
29 | }
30 |
31 | public Player(double x, double y) {
32 | super(x, y, 30, 30);
33 | }
34 |
35 | public int getHealth() {
36 | return health;
37 | }
38 |
39 | public void setHealth(int health) {
40 | this.health = health;
41 | }
42 |
43 | @Override
44 | public void update() {
45 | if (moveLeft && x - SPEED > 0) {
46 | x -= SPEED;
47 | }
48 |
49 | if (moveRight && x + width + SPEED < SpaceShooter.WIDTH) {
50 | x += SPEED;
51 | }
52 |
53 | if (moveForward && y - SPEED > 0) {
54 | y -= SPEED;
55 | }
56 |
57 | if (moveBackward && y + height + SPEED < SpaceShooter.HEIGHT) {
58 | y += SPEED;
59 | }
60 | }
61 |
62 |
63 | @Override
64 | public void render(GraphicsContext gc) {
65 | gc.setFill(Color.BLUE);
66 | gc.fillRect(x - WIDTH / 2, y - HEIGHT / 2, WIDTH, HEIGHT);
67 | }
68 |
69 |
70 | public void setMoveLeft(boolean moveLeft) {
71 | this.moveLeft = moveLeft;
72 | }
73 |
74 | public void setMoveRight(boolean moveRight) {
75 | this.moveRight = moveRight;
76 | }
77 |
78 | public void setMoveForward(boolean moveForward) {
79 | this.moveForward = moveForward;
80 | }
81 |
82 | public void setMoveBackward(boolean moveBackward) {
83 | this.moveBackward = moveBackward;
84 | }
85 |
86 | public void shoot(List newObjects) {
87 | Bullet bullet = new Bullet(x, y - HEIGHT / 2 - Bullet.HEIGHT);
88 | newObjects.add(bullet);
89 | }
90 |
91 | private boolean dead = false;
92 |
93 | public void setDead(boolean dead) {
94 | this.dead = dead;
95 | }
96 |
97 | @Override
98 | public boolean isDead() {
99 | return dead;
100 | }
101 |
102 | }
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.example
8 | SpaceShooterProject
9 | 1.0-SNAPSHOT
10 | Space Shooter Project
11 |
12 |
13 | UTF-8
14 | 5.9.2
15 |
16 |
17 |
18 |
19 | org.openjfx
20 | javafx-controls
21 | 20.0.1
22 |
23 |
24 | org.openjfx
25 | javafx-fxml
26 | 20.0.1
27 |
28 |
29 |
30 | org.junit.jupiter
31 | junit-jupiter-api
32 | ${junit.version}
33 | test
34 |
35 |
36 | org.junit.jupiter
37 | junit-jupiter-engine
38 | ${junit.version}
39 | test
40 |
41 |
42 |
43 |
44 |
45 |
46 | org.apache.maven.plugins
47 | maven-compiler-plugin
48 | 3.11.0
49 |
50 | 20
51 | 20
52 |
53 |
54 |
55 | org.openjfx
56 | javafx-maven-plugin
57 | 0.0.8
58 |
59 |
60 |
61 | default-cli
62 |
63 |
64 | com.example.spaceshooterproject/com.example.spaceshooterproject.HelloApplication
65 |
66 | app
67 | app
68 | app
69 | true
70 | true
71 | true
72 |
73 |
74 |
75 |
76 |
77 |
78 |
--------------------------------------------------------------------------------
/mvnw.cmd:
--------------------------------------------------------------------------------
1 | @REM ----------------------------------------------------------------------------
2 | @REM Licensed to the Apache Software Foundation (ASF) under one
3 | @REM or more contributor license agreements. See the NOTICE file
4 | @REM distributed with this work for additional information
5 | @REM regarding copyright ownership. The ASF licenses this file
6 | @REM to you under the Apache License, Version 2.0 (the
7 | @REM "License"); you may not use this file except in compliance
8 | @REM with the License. You may obtain a copy of the License at
9 | @REM
10 | @REM https://www.apache.org/licenses/LICENSE-2.0
11 | @REM
12 | @REM Unless required by applicable law or agreed to in writing,
13 | @REM software distributed under the License is distributed on an
14 | @REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 | @REM KIND, either express or implied. See the License for the
16 | @REM specific language governing permissions and limitations
17 | @REM under the License.
18 | @REM ----------------------------------------------------------------------------
19 |
20 | @REM ----------------------------------------------------------------------------
21 | @REM Maven Start Up Batch script
22 | @REM
23 | @REM Required ENV vars:
24 | @REM JAVA_HOME - location of a JDK home dir
25 | @REM
26 | @REM Optional ENV vars
27 | @REM M2_HOME - location of maven2's installed home dir
28 | @REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands
29 | @REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending
30 | @REM MAVEN_OPTS - parameters passed to the Java VM when running Maven
31 | @REM e.g. to debug Maven itself, use
32 | @REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
33 | @REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files
34 | @REM ----------------------------------------------------------------------------
35 |
36 | @REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on'
37 | @echo off
38 | @REM set title of command window
39 | title %0
40 | @REM enable echoing by setting MAVEN_BATCH_ECHO to 'on'
41 | @if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO%
42 |
43 | @REM set %HOME% to equivalent of $HOME
44 | if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%")
45 |
46 | @REM Execute a user defined script before this one
47 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre
48 | @REM check for pre script, once with legacy .bat ending and once with .cmd ending
49 | if exist "%USERPROFILE%\mavenrc_pre.bat" call "%USERPROFILE%\mavenrc_pre.bat" %*
50 | if exist "%USERPROFILE%\mavenrc_pre.cmd" call "%USERPROFILE%\mavenrc_pre.cmd" %*
51 | :skipRcPre
52 |
53 | @setlocal
54 |
55 | set ERROR_CODE=0
56 |
57 | @REM To isolate internal variables from possible post scripts, we use another setlocal
58 | @setlocal
59 |
60 | @REM ==== START VALIDATION ====
61 | if not "%JAVA_HOME%" == "" goto OkJHome
62 |
63 | echo.
64 | echo Error: JAVA_HOME not found in your environment. >&2
65 | echo Please set the JAVA_HOME variable in your environment to match the >&2
66 | echo location of your Java installation. >&2
67 | echo.
68 | goto error
69 |
70 | :OkJHome
71 | if exist "%JAVA_HOME%\bin\java.exe" goto init
72 |
73 | echo.
74 | echo Error: JAVA_HOME is set to an invalid directory. >&2
75 | echo JAVA_HOME = "%JAVA_HOME%" >&2
76 | echo Please set the JAVA_HOME variable in your environment to match the >&2
77 | echo location of your Java installation. >&2
78 | echo.
79 | goto error
80 |
81 | @REM ==== END VALIDATION ====
82 |
83 | :init
84 |
85 | @REM Find the project base dir, i.e. the directory that contains the folder ".mvn".
86 | @REM Fallback to current working directory if not found.
87 |
88 | set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR%
89 | IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir
90 |
91 | set EXEC_DIR=%CD%
92 | set WDIR=%EXEC_DIR%
93 | :findBaseDir
94 | IF EXIST "%WDIR%"\.mvn goto baseDirFound
95 | cd ..
96 | IF "%WDIR%"=="%CD%" goto baseDirNotFound
97 | set WDIR=%CD%
98 | goto findBaseDir
99 |
100 | :baseDirFound
101 | set MAVEN_PROJECTBASEDIR=%WDIR%
102 | cd "%EXEC_DIR%"
103 | goto endDetectBaseDir
104 |
105 | :baseDirNotFound
106 | set MAVEN_PROJECTBASEDIR=%EXEC_DIR%
107 | cd "%EXEC_DIR%"
108 |
109 | :endDetectBaseDir
110 |
111 | IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig
112 |
113 | @setlocal EnableExtensions EnableDelayedExpansion
114 | for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a
115 | @endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS%
116 |
117 | :endReadAdditionalConfig
118 |
119 | SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe"
120 | set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar"
121 | set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain
122 |
123 | set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar"
124 |
125 | FOR /F "usebackq tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO (
126 | IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B
127 | )
128 |
129 | @REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central
130 | @REM This allows using the maven wrapper in projects that prohibit checking in binary data.
131 | if exist %WRAPPER_JAR% (
132 | if "%MVNW_VERBOSE%" == "true" (
133 | echo Found %WRAPPER_JAR%
134 | )
135 | ) else (
136 | if not "%MVNW_REPOURL%" == "" (
137 | SET DOWNLOAD_URL="%MVNW_REPOURL%/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar"
138 | )
139 | if "%MVNW_VERBOSE%" == "true" (
140 | echo Couldn't find %WRAPPER_JAR%, downloading it ...
141 | echo Downloading from: %DOWNLOAD_URL%
142 | )
143 |
144 | powershell -Command "&{"^
145 | "$webclient = new-object System.Net.WebClient;"^
146 | "if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^
147 | "$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^
148 | "}"^
149 | "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^
150 | "}"
151 | if "%MVNW_VERBOSE%" == "true" (
152 | echo Finished downloading %WRAPPER_JAR%
153 | )
154 | )
155 | @REM End of extension
156 |
157 | @REM Provide a "standardized" way to retrieve the CLI args that will
158 | @REM work with both Windows and non-Windows executions.
159 | set MAVEN_CMD_LINE_ARGS=%*
160 |
161 | %MAVEN_JAVA_EXE% ^
162 | %JVM_CONFIG_MAVEN_PROPS% ^
163 | %MAVEN_OPTS% ^
164 | %MAVEN_DEBUG_OPTS% ^
165 | -classpath %WRAPPER_JAR% ^
166 | "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" ^
167 | %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %*
168 | if ERRORLEVEL 1 goto error
169 | goto end
170 |
171 | :error
172 | set ERROR_CODE=1
173 |
174 | :end
175 | @endlocal & set ERROR_CODE=%ERROR_CODE%
176 |
177 | if not "%MAVEN_SKIP_RC%"=="" goto skipRcPost
178 | @REM check for post script, once with legacy .bat ending and once with .cmd ending
179 | if exist "%USERPROFILE%\mavenrc_post.bat" call "%USERPROFILE%\mavenrc_post.bat"
180 | if exist "%USERPROFILE%\mavenrc_post.cmd" call "%USERPROFILE%\mavenrc_post.cmd"
181 | :skipRcPost
182 |
183 | @REM pause the script if MAVEN_BATCH_PAUSE is set to 'on'
184 | if "%MAVEN_BATCH_PAUSE%"=="on" pause
185 |
186 | if "%MAVEN_TERMINATE_CMD%"=="on" exit %ERROR_CODE%
187 |
188 | cmd /C exit /B %ERROR_CODE%
189 |
--------------------------------------------------------------------------------
/mvnw:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | # ----------------------------------------------------------------------------
3 | # Licensed to the Apache Software Foundation (ASF) under one
4 | # or more contributor license agreements. See the NOTICE file
5 | # distributed with this work for additional information
6 | # regarding copyright ownership. The ASF licenses this file
7 | # to you under the Apache License, Version 2.0 (the
8 | # "License"); you may not use this file except in compliance
9 | # with the License. You may obtain a copy of the License at
10 | #
11 | # https://www.apache.org/licenses/LICENSE-2.0
12 | #
13 | # Unless required by applicable law or agreed to in writing,
14 | # software distributed under the License is distributed on an
15 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 | # KIND, either express or implied. See the License for the
17 | # specific language governing permissions and limitations
18 | # under the License.
19 | # ----------------------------------------------------------------------------
20 |
21 | # ----------------------------------------------------------------------------
22 | # Maven Start Up Batch script
23 | #
24 | # Required ENV vars:
25 | # ------------------
26 | # JAVA_HOME - location of a JDK home dir
27 | #
28 | # Optional ENV vars
29 | # -----------------
30 | # M2_HOME - location of maven2's installed home dir
31 | # MAVEN_OPTS - parameters passed to the Java VM when running Maven
32 | # e.g. to debug Maven itself, use
33 | # set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
34 | # MAVEN_SKIP_RC - flag to disable loading of mavenrc files
35 | # ----------------------------------------------------------------------------
36 |
37 | if [ -z "$MAVEN_SKIP_RC" ] ; then
38 |
39 | if [ -f /usr/local/etc/mavenrc ] ; then
40 | . /usr/local/etc/mavenrc
41 | fi
42 |
43 | if [ -f /etc/mavenrc ] ; then
44 | . /etc/mavenrc
45 | fi
46 |
47 | if [ -f "$HOME/.mavenrc" ] ; then
48 | . "$HOME/.mavenrc"
49 | fi
50 |
51 | fi
52 |
53 | # OS specific support. $var _must_ be set to either true or false.
54 | cygwin=false;
55 | darwin=false;
56 | mingw=false
57 | case "`uname`" in
58 | CYGWIN*) cygwin=true ;;
59 | MINGW*) mingw=true;;
60 | Darwin*) darwin=true
61 | # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home
62 | # See https://developer.apple.com/library/mac/qa/qa1170/_index.html
63 | if [ -z "$JAVA_HOME" ]; then
64 | if [ -x "/usr/libexec/java_home" ]; then
65 | export JAVA_HOME="`/usr/libexec/java_home`"
66 | else
67 | export JAVA_HOME="/Library/Java/Home"
68 | fi
69 | fi
70 | ;;
71 | esac
72 |
73 | if [ -z "$JAVA_HOME" ] ; then
74 | if [ -r /etc/gentoo-release ] ; then
75 | JAVA_HOME=`java-config --jre-home`
76 | fi
77 | fi
78 |
79 | if [ -z "$M2_HOME" ] ; then
80 | ## resolve links - $0 may be a link to maven's home
81 | PRG="$0"
82 |
83 | # need this for relative symlinks
84 | while [ -h "$PRG" ] ; do
85 | ls=`ls -ld "$PRG"`
86 | link=`expr "$ls" : '.*-> \(.*\)$'`
87 | if expr "$link" : '/.*' > /dev/null; then
88 | PRG="$link"
89 | else
90 | PRG="`dirname "$PRG"`/$link"
91 | fi
92 | done
93 |
94 | saveddir=`pwd`
95 |
96 | M2_HOME=`dirname "$PRG"`/..
97 |
98 | # make it fully qualified
99 | M2_HOME=`cd "$M2_HOME" && pwd`
100 |
101 | cd "$saveddir"
102 | # echo Using m2 at $M2_HOME
103 | fi
104 |
105 | # For Cygwin, ensure paths are in UNIX format before anything is touched
106 | if $cygwin ; then
107 | [ -n "$M2_HOME" ] &&
108 | M2_HOME=`cygpath --unix "$M2_HOME"`
109 | [ -n "$JAVA_HOME" ] &&
110 | JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
111 | [ -n "$CLASSPATH" ] &&
112 | CLASSPATH=`cygpath --path --unix "$CLASSPATH"`
113 | fi
114 |
115 | # For Mingw, ensure paths are in UNIX format before anything is touched
116 | if $mingw ; then
117 | [ -n "$M2_HOME" ] &&
118 | M2_HOME="`(cd "$M2_HOME"; pwd)`"
119 | [ -n "$JAVA_HOME" ] &&
120 | JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`"
121 | fi
122 |
123 | if [ -z "$JAVA_HOME" ]; then
124 | javaExecutable="`which javac`"
125 | if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then
126 | # readlink(1) is not available as standard on Solaris 10.
127 | readLink=`which readlink`
128 | if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then
129 | if $darwin ; then
130 | javaHome="`dirname \"$javaExecutable\"`"
131 | javaExecutable="`cd \"$javaHome\" && pwd -P`/javac"
132 | else
133 | javaExecutable="`readlink -f \"$javaExecutable\"`"
134 | fi
135 | javaHome="`dirname \"$javaExecutable\"`"
136 | javaHome=`expr "$javaHome" : '\(.*\)/bin'`
137 | JAVA_HOME="$javaHome"
138 | export JAVA_HOME
139 | fi
140 | fi
141 | fi
142 |
143 | if [ -z "$JAVACMD" ] ; then
144 | if [ -n "$JAVA_HOME" ] ; then
145 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
146 | # IBM's JDK on AIX uses strange locations for the executables
147 | JAVACMD="$JAVA_HOME/jre/sh/java"
148 | else
149 | JAVACMD="$JAVA_HOME/bin/java"
150 | fi
151 | else
152 | JAVACMD="`\\unset -f command; \\command -v java`"
153 | fi
154 | fi
155 |
156 | if [ ! -x "$JAVACMD" ] ; then
157 | echo "Error: JAVA_HOME is not defined correctly." >&2
158 | echo " We cannot execute $JAVACMD" >&2
159 | exit 1
160 | fi
161 |
162 | if [ -z "$JAVA_HOME" ] ; then
163 | echo "Warning: JAVA_HOME environment variable is not set."
164 | fi
165 |
166 | CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher
167 |
168 | # traverses directory structure from process work directory to filesystem root
169 | # first directory with .mvn subdirectory is considered project base directory
170 | find_maven_basedir() {
171 |
172 | if [ -z "$1" ]
173 | then
174 | echo "Path not specified to find_maven_basedir"
175 | return 1
176 | fi
177 |
178 | basedir="$1"
179 | wdir="$1"
180 | while [ "$wdir" != '/' ] ; do
181 | if [ -d "$wdir"/.mvn ] ; then
182 | basedir=$wdir
183 | break
184 | fi
185 | # workaround for JBEAP-8937 (on Solaris 10/Sparc)
186 | if [ -d "${wdir}" ]; then
187 | wdir=`cd "$wdir/.."; pwd`
188 | fi
189 | # end of workaround
190 | done
191 | echo "${basedir}"
192 | }
193 |
194 | # concatenates all lines of a file
195 | concat_lines() {
196 | if [ -f "$1" ]; then
197 | echo "$(tr -s '\n' ' ' < "$1")"
198 | fi
199 | }
200 |
201 | BASE_DIR=`find_maven_basedir "$(pwd)"`
202 | if [ -z "$BASE_DIR" ]; then
203 | exit 1;
204 | fi
205 |
206 | ##########################################################################################
207 | # Extension to allow automatically downloading the maven-wrapper.jar from Maven-central
208 | # This allows using the maven wrapper in projects that prohibit checking in binary data.
209 | ##########################################################################################
210 | if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then
211 | if [ "$MVNW_VERBOSE" = true ]; then
212 | echo "Found .mvn/wrapper/maven-wrapper.jar"
213 | fi
214 | else
215 | if [ "$MVNW_VERBOSE" = true ]; then
216 | echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..."
217 | fi
218 | if [ -n "$MVNW_REPOURL" ]; then
219 | jarUrl="$MVNW_REPOURL/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar"
220 | else
221 | jarUrl="https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar"
222 | fi
223 | while IFS="=" read key value; do
224 | case "$key" in (wrapperUrl) jarUrl="$value"; break ;;
225 | esac
226 | done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties"
227 | if [ "$MVNW_VERBOSE" = true ]; then
228 | echo "Downloading from: $jarUrl"
229 | fi
230 | wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar"
231 | if $cygwin; then
232 | wrapperJarPath=`cygpath --path --windows "$wrapperJarPath"`
233 | fi
234 |
235 | if command -v wget > /dev/null; then
236 | if [ "$MVNW_VERBOSE" = true ]; then
237 | echo "Found wget ... using wget"
238 | fi
239 | if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then
240 | wget "$jarUrl" -O "$wrapperJarPath" || rm -f "$wrapperJarPath"
241 | else
242 | wget --http-user=$MVNW_USERNAME --http-password=$MVNW_PASSWORD "$jarUrl" -O "$wrapperJarPath" || rm -f "$wrapperJarPath"
243 | fi
244 | elif command -v curl > /dev/null; then
245 | if [ "$MVNW_VERBOSE" = true ]; then
246 | echo "Found curl ... using curl"
247 | fi
248 | if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then
249 | curl -o "$wrapperJarPath" "$jarUrl" -f
250 | else
251 | curl --user $MVNW_USERNAME:$MVNW_PASSWORD -o "$wrapperJarPath" "$jarUrl" -f
252 | fi
253 |
254 | else
255 | if [ "$MVNW_VERBOSE" = true ]; then
256 | echo "Falling back to using Java to download"
257 | fi
258 | javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java"
259 | # For Cygwin, switch paths to Windows format before running javac
260 | if $cygwin; then
261 | javaClass=`cygpath --path --windows "$javaClass"`
262 | fi
263 | if [ -e "$javaClass" ]; then
264 | if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then
265 | if [ "$MVNW_VERBOSE" = true ]; then
266 | echo " - Compiling MavenWrapperDownloader.java ..."
267 | fi
268 | # Compiling the Java class
269 | ("$JAVA_HOME/bin/javac" "$javaClass")
270 | fi
271 | if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then
272 | # Running the downloader
273 | if [ "$MVNW_VERBOSE" = true ]; then
274 | echo " - Running MavenWrapperDownloader.java ..."
275 | fi
276 | ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR")
277 | fi
278 | fi
279 | fi
280 | fi
281 | ##########################################################################################
282 | # End of extension
283 | ##########################################################################################
284 |
285 | export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"}
286 | if [ "$MVNW_VERBOSE" = true ]; then
287 | echo $MAVEN_PROJECTBASEDIR
288 | fi
289 | MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS"
290 |
291 | # For Cygwin, switch paths to Windows format before running java
292 | if $cygwin; then
293 | [ -n "$M2_HOME" ] &&
294 | M2_HOME=`cygpath --path --windows "$M2_HOME"`
295 | [ -n "$JAVA_HOME" ] &&
296 | JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"`
297 | [ -n "$CLASSPATH" ] &&
298 | CLASSPATH=`cygpath --path --windows "$CLASSPATH"`
299 | [ -n "$MAVEN_PROJECTBASEDIR" ] &&
300 | MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"`
301 | fi
302 |
303 | # Provide a "standardized" way to retrieve the CLI args that will
304 | # work with both Windows and non-Windows executions.
305 | MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@"
306 | export MAVEN_CMD_LINE_ARGS
307 |
308 | WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain
309 |
310 | exec "$JAVACMD" \
311 | $MAVEN_OPTS \
312 | $MAVEN_DEBUG_OPTS \
313 | -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \
314 | "-Dmaven.home=${M2_HOME}" \
315 | "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \
316 | ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@"
317 |
--------------------------------------------------------------------------------
/src/main/java/com/example/spaceshooterproject/SpaceShooter.java:
--------------------------------------------------------------------------------
1 | package com.example.spaceshooterproject;
2 |
3 | import javafx.animation.AnimationTimer;
4 | import javafx.animation.PauseTransition;
5 | import javafx.application.Application;
6 | import javafx.scene.Scene;
7 | import javafx.scene.canvas.Canvas;
8 | import javafx.scene.canvas.GraphicsContext;
9 | import javafx.scene.control.Button;
10 | import javafx.scene.control.Label;
11 | import javafx.scene.image.Image;
12 | import javafx.scene.layout.Pane;
13 | import javafx.scene.layout.VBox;
14 | import javafx.scene.paint.Color;
15 | import javafx.scene.text.Font;
16 | import javafx.scene.text.FontWeight;
17 | import javafx.scene.text.Text;
18 | import javafx.stage.Stage;
19 | import javafx.util.Duration;
20 | import javafx.scene.control.Alert;
21 | import javafx.scene.control.Alert.AlertType;
22 | import javafx.scene.effect.Glow;
23 |
24 | import java.util.ArrayList;
25 | import java.util.Iterator;
26 | import java.util.List;
27 | import java.util.Random;
28 |
29 | public class SpaceShooter extends Application {
30 |
31 | public static final int WIDTH = 300;
32 |
33 | public static final int HEIGHT = 600;
34 |
35 | public static int numLives = 3;
36 |
37 | private int score = 0;
38 |
39 | private boolean bossExists = false;
40 |
41 | private boolean reset = false;
42 |
43 | // Add a new label to display the score
44 | private final Label scoreLabel = new Label("Score: " + score);
45 |
46 | private final Label lifeLabel = new Label("Lives: " + numLives);
47 |
48 | private final List gameObjects = new ArrayList<>();
49 |
50 | private final List newObjects = new ArrayList<>();
51 |
52 | private Player player = new Player(WIDTH / 2, HEIGHT - 40);
53 |
54 | private Pane root = new Pane();
55 |
56 | private Scene scene = new Scene(root, WIDTH, HEIGHT, Color.BLACK);
57 |
58 | private boolean levelUpMessageDisplayed = false;
59 |
60 | private boolean levelUpShown = false;
61 |
62 | private Stage primaryStage;
63 |
64 | public static void main(String[] args) {
65 | launch(args);
66 | }
67 |
68 |
69 | @Override
70 | public void start(Stage primaryStage) {
71 | this.primaryStage = primaryStage;
72 | primaryStage.setScene(scene);
73 | primaryStage.setTitle("Space Shooter");
74 | primaryStage.setResizable(false);
75 | Canvas canvas = new Canvas(WIDTH, HEIGHT);
76 | scoreLabel.setTranslateX(10);
77 | scoreLabel.setTranslateY(10);
78 | scoreLabel.setTextFill(Color.BLACK);
79 | scoreLabel.setFont(Font.font("Arial", FontWeight.BOLD, 18));
80 | root.getChildren().addAll(canvas, scoreLabel, lifeLabel);
81 | lifeLabel.setTranslateX(10);
82 | lifeLabel.setTranslateY(40);
83 | lifeLabel.setTextFill(Color.BLACK);
84 | lifeLabel.setFont(Font.font("Arial", FontWeight.BOLD, 18));
85 | GraphicsContext gc = canvas.getGraphicsContext2D();
86 | gameObjects.add(player);
87 | Pane menuPane = createMenu();
88 | Scene menuScene = new Scene(menuPane, WIDTH, HEIGHT);
89 | primaryStage.setScene(menuScene);
90 | primaryStage.setTitle("Space Shooter");
91 | primaryStage.setResizable(false);
92 | initEventHandlers(scene);
93 |
94 | AnimationTimer gameLoop = new AnimationTimer() {
95 | private long lastEnemySpawned = 0;
96 |
97 | private long lastPowerUpSpawned = 0;
98 |
99 | @Override
100 | public void handle(long now) {
101 | if (reset) {
102 | this.start();
103 | reset = false;
104 | }
105 | gc.clearRect(0, 0, WIDTH, HEIGHT);
106 |
107 | if (now - lastEnemySpawned > 1_000_000_000) {
108 | spawnEnemy();
109 | lastEnemySpawned = now;
110 | }
111 |
112 | if (now - lastPowerUpSpawned > 10_000_000_000L) {
113 | spawnPowerUp();
114 | lastPowerUpSpawned = now;
115 | }
116 |
117 | if (score >= 200 && score % 200 == 0) {
118 | boolean bossExists = false;
119 | for (GameObject obj : gameObjects) {
120 | if (obj instanceof BossEnemy) {
121 | bossExists = true;
122 | break;
123 | }
124 | }
125 | if (!bossExists) {
126 | spawnBossEnemy();
127 | }
128 | }
129 |
130 | checkCollisions();
131 |
132 | checkEnemiesReachingBottom();
133 | gameObjects.addAll(newObjects);
134 | newObjects.clear();
135 |
136 | for (GameObject obj : gameObjects) {
137 | obj.update();
138 | obj.render(gc);
139 | }
140 |
141 | Iterator iterator = gameObjects.iterator();
142 | while (iterator.hasNext()) {
143 | GameObject obj = iterator.next();
144 | if (obj.isDead()) {
145 | iterator.remove();
146 | }
147 | }
148 | }
149 |
150 |
151 | };
152 |
153 | gameLoop.start();
154 |
155 | primaryStage.show();
156 | }
157 |
158 | private void spawnEnemy() {
159 | Random random = new Random();
160 | int x = random.nextInt(WIDTH - 50) + 25;
161 |
162 | boolean bossExists = false;
163 |
164 | for (GameObject obj : gameObjects) {
165 | if (obj instanceof BossEnemy) {
166 | bossExists = true;
167 | break;
168 | }
169 | }
170 |
171 | if (!bossExists && score % 200 == 0 && score > 0) {
172 | BossEnemy boss = new BossEnemy(x, -50);
173 | gameObjects.add(boss);
174 | } else {
175 | Enemy enemy = new Enemy(x, -40);
176 | gameObjects.add(enemy);
177 | }
178 |
179 | if (!bossExists && score % 200 == 0 && score > 0) {
180 | BossEnemy boss = new BossEnemy(x, -50);
181 | gameObjects.add(boss);
182 | showTempMessage("A boss is ahead, watch out!", 75, HEIGHT / 2 - 100, 5);
183 | bossExists = true;
184 | }
185 | else {
186 | Enemy enemy = new Enemy(x, -40);
187 | gameObjects.add(enemy);
188 | }
189 |
190 | }
191 |
192 |
193 |
194 | private void checkCollisions() {
195 | List bullets = new ArrayList<>();
196 | List enemies = new ArrayList<>();
197 | List powerUps = new ArrayList<>();
198 |
199 | for (GameObject obj : gameObjects) {
200 | if (obj instanceof Bullet) {
201 | bullets.add((Bullet) obj);
202 | } else if (obj instanceof Enemy) {
203 | enemies.add((Enemy) obj);
204 | } else if (obj instanceof PowerUp) {
205 | powerUps.add((PowerUp) obj);
206 | }
207 | }
208 |
209 | for (Bullet bullet : bullets) {
210 | for (Enemy enemy : enemies) {
211 | if (bullet.getBounds().intersects(enemy.getBounds())) {
212 | bullet.setDead(true);
213 | if (enemy instanceof BossEnemy) {
214 | ((BossEnemy) enemy).takeDamage();
215 | score += 20;
216 | }
217 | else {
218 | enemy.setDead(true);
219 | score += 10;
220 | }
221 | scoreLabel.setText("Score: " + score);
222 |
223 | if (score % 100 == 0) {
224 | Enemy.SPEED += 2;
225 | }
226 | }
227 | }
228 |
229 | // Check collisions between bullets and power-ups
230 | for (PowerUp powerUp : powerUps) {
231 | if (bullet.getBounds().intersects(powerUp.getBounds())) {
232 | bullet.setDead(true);
233 | powerUp.setDead(true);
234 | score += 50; // Deduct 5 points when a bullet hits a power-up
235 | scoreLabel.setText("Score: " + score);
236 | }
237 | }
238 | }
239 |
240 | if (score % 100 == 0 && score > 0 && !levelUpShown) {
241 | showTempMessage("Level Up!", 110, HEIGHT / 2, 2);
242 | levelUpShown = true;
243 | } else if (score % 100 != 0) {
244 | levelUpShown = false;
245 | }
246 |
247 | checkScore();
248 |
249 | }
250 |
251 | private void checkEnemiesReachingBottom() {
252 | List enemies = new ArrayList<>();
253 | for (GameObject obj: gameObjects) {
254 | if (obj instanceof Enemy) {
255 | enemies.add((Enemy) obj);
256 | }
257 | }
258 |
259 | for (Enemy enemy : enemies) {
260 | if (enemy.getY() + enemy.getHeight() / 2 >= HEIGHT) {
261 | enemy.setDead(true);
262 | enemy.SPEED = enemy.SPEED + 0.4;
263 | numLives--;
264 | score -= 10;
265 | lifeLabel.setText("Lives: " + numLives);
266 | if (numLives < 0) {
267 | enemy.SPEED = 2;
268 | resetGame();
269 | }
270 | }
271 | }
272 | }
273 |
274 | private void resetGame() {
275 | gameObjects.clear();
276 | numLives = 3;
277 | score = 0;
278 | lifeLabel.setText("Lives: " + numLives);
279 | scoreLabel.setText("Score: " + score);
280 | gameObjects.add(player);
281 | reset = true;
282 | Text lostMessage = new Text("You lost! The game has been reset.");
283 | lostMessage.setFont(Font.font("Arial", FontWeight.BOLD, 18));
284 | lostMessage.setFill(Color.RED);
285 | lostMessage.setX((WIDTH - lostMessage.getLayoutBounds().getWidth()) / 2);
286 | lostMessage.setY(HEIGHT / 2);
287 | root.getChildren().add(lostMessage);
288 |
289 | PauseTransition pause = new PauseTransition(Duration.seconds(2));
290 | pause.setOnFinished(event -> {
291 | root.getChildren().remove(lostMessage);
292 | initEventHandlers(scene);
293 | });
294 | pause.play();
295 | }
296 |
297 | private void initEventHandlers(Scene scene) {
298 | scene.setOnKeyPressed(event -> {
299 | switch (event.getCode()) {
300 | case A:
301 | case LEFT:
302 | player.setMoveLeft(true);
303 | break;
304 | case D:
305 | case RIGHT:
306 | player.setMoveRight(true);
307 | break;
308 | case S:
309 | player.setMoveBackward(true);
310 | break;
311 | case W:
312 | player.setMoveForward(true);
313 | break;
314 | case SPACE:
315 | player.shoot(newObjects);
316 | break;
317 | }
318 | });
319 |
320 | scene.setOnKeyReleased(event -> {
321 | switch (event.getCode()) {
322 | case A:
323 | case LEFT:
324 | player.setMoveLeft(false);
325 | break;
326 | case D:
327 | case RIGHT:
328 | player.setMoveRight(false);
329 | break;
330 | case S:
331 | player.setMoveBackward(false);
332 | break;
333 | case W:
334 | player.setMoveForward(false);
335 | break;
336 | }
337 | });
338 |
339 | }
340 |
341 | private void spawnPowerUp() {
342 | Random random = new Random();
343 | int x = random.nextInt(WIDTH - PowerUp.WIDTH) + PowerUp.WIDTH / 2;
344 | PowerUp powerUp = new PowerUp(x, -PowerUp.HEIGHT / 2);
345 | gameObjects.add(powerUp);
346 | }
347 |
348 | private void spawnBossEnemy() {
349 | if (gameObjects.stream().noneMatch(obj -> obj instanceof BossEnemy)) {
350 | BossEnemy bossEnemy = new BossEnemy(WIDTH / 2, -40);
351 | gameObjects.add(bossEnemy);
352 | }
353 | }
354 |
355 |
356 |
357 | private void checkScore() {
358 | if (this.score >= 100) {
359 | Text lostMessage = new Text("Now I dare you to pass 1000 :)");
360 | lostMessage.setFont(Font.font("Arial", FontWeight.BOLD, 12));
361 | lostMessage.setFill(Color.RED);
362 | lostMessage.setX((WIDTH - lostMessage.getLayoutBounds().getWidth()) / 2);
363 | lostMessage.setY(HEIGHT / 2 - 100);
364 | root.getChildren().add(lostMessage);
365 | PauseTransition pause = new PauseTransition(Duration.seconds(2));
366 | pause.setOnFinished(event -> {
367 | root.getChildren().remove(lostMessage);
368 | });
369 | pause.play();
370 | }
371 |
372 | }
373 |
374 | private Pane createMenu() {
375 | Pane menuPane = new Pane();
376 | menuPane.setStyle("-fx-background-color: black;");
377 |
378 | Text welcomeText = new Text(" Welcome to \nSpace Shooter!");
379 | welcomeText.setFont(Font.font("Arial", FontWeight.BOLD, 30));
380 | welcomeText.setFill(Color.WHITE);
381 | welcomeText.setX((WIDTH - welcomeText.getLayoutBounds().getWidth()) / 2);
382 | welcomeText.setY(100); // Move welcome message higher on the screen
383 |
384 | Button startButton = createButton("START", 200);
385 | startButton.setOnAction(event -> startGame());
386 |
387 | Button instructionsButton = createButton("Show Instructions", 300);
388 | instructionsButton.setOnAction(event -> showInstructions());
389 |
390 | Button quitButton = createButton("QUIT", 400);
391 | quitButton.setOnAction(event -> System.exit(0));
392 |
393 | VBox buttonsContainer = new VBox(20); // Add buttons container to center-align the buttons
394 | buttonsContainer.setLayoutX((WIDTH - startButton.getPrefWidth()) / 2 - 100);
395 | buttonsContainer.setLayoutY(200);
396 | buttonsContainer.getChildren().addAll(startButton, instructionsButton, quitButton);
397 |
398 | menuPane.getChildren().addAll(welcomeText, buttonsContainer);
399 |
400 | return menuPane;
401 | }
402 |
403 | private Button createButton(String text, double y) {
404 | Button button = new Button(text);
405 | button.setLayoutX((WIDTH - button.getPrefWidth()) / 2);
406 | button.setLayoutY(y);
407 | button.setTextFill(Color.WHITE);
408 | button.setStyle("-fx-background-color: rgba(0, 0, 0, 0.5); -fx-font-size: 20; -fx-font-weight: bold; -fx-padding: 10 20;");
409 | button.setOnMouseEntered(event -> {
410 | button.setTextFill(Color.YELLOW);
411 | button.setEffect(new Glow());
412 | });
413 | button.setOnMouseExited(event -> {
414 | button.setTextFill(Color.WHITE);
415 | button.setEffect(null);
416 | });
417 | return button;
418 | }
419 |
420 | private void showInstructions() {
421 | Alert instructionsAlert = new Alert(AlertType.INFORMATION);
422 | instructionsAlert.setTitle("Instructions");
423 | instructionsAlert.setHeaderText("Space Shooter Instructions");
424 | instructionsAlert.setContentText("Use the A, W, S, and D keys or the arrow keys to move your spaceship.\n" +
425 | "Press SPACE to shoot bullets and destroy the enemies.\n" +
426 | "If an enemy reaches the bottom of the screen, you lose a life.\n" +
427 | "The game resets if you lose all lives.");
428 | instructionsAlert.showAndWait();
429 | }
430 |
431 | private void showTempMessage(String message, double x, double y, double duration) {
432 | Text tempMessage = new Text(message);
433 | tempMessage.setFont(Font.font("Arial", FontWeight.BOLD, 18));
434 | tempMessage.setFill(Color.RED);
435 | tempMessage.setX(x);
436 | tempMessage.setY(y);
437 | root.getChildren().add(tempMessage);
438 |
439 | PauseTransition pause = new PauseTransition(Duration.seconds(duration));
440 | pause.setOnFinished(event -> root.getChildren().remove(tempMessage));
441 | pause.play();
442 | }
443 |
444 |
445 | private void startGame() {
446 | primaryStage.setScene(scene);
447 | }
448 |
449 | }
450 |
--------------------------------------------------------------------------------