Space Invader Game in JavaFX
Space Invader Game in JavaFX
To
0
A Project Report on
To
We wish to express our deep sense of gratitude to our guide Mr Rajiv shrma, Lecturer,
Dept. of computer science engineering, of Pacific Polytechnic College who through their excellent
guidance have enabled us to accomplish this work. He has been great source of inspiration to us,
all through. We are very grateful to him for guiding us how to conduct research and how to clearly
& effectively present the work done.
I extend my sincere thanks to our Director Mr. Mukesh Shrimali and Mr Rajiv Sharma,
H.O.D (C.S.E), Pacific Polytechnic College, for providing the adequate means and support to
pursue this work.
Finally, we would like to add few heartfelt words for the people who were the part of the
project in various ways, especially our friends and classmates who gave us unending support right
from the beginning. Without their support, persistence and love we would not be where we are
today.
LUCKY BAIRAGI
The project titled Arcade Game Software: Space Invader is game software in which player play as hero
space ship through which he/she destroy other enemy ships. This game is developed in Java language
and using GUI library JavaFX which is very helpful in making GUI related application in Java.
This game is focused on a battle ship which is equipped with bullets to destroy enemies. Every time an
enemy ship get destroyed player get a point for that.
“Arcade Game Software: Space Invader” is a platform independent software which means you can run
it on any device like Windows machine, linux machine and Mac. This software is developed in JDK 14
which means it require JRE8-JRE14.
This software is very easy to use, just play it with two arrows keys to move ship and space bar to shoot
bullets.
It shows real time score on screen and when you clash with other ships it pop-up with “Game Over”
screen. Longer you last ,faster the game become.
CERTFICATE
ACKNOWLEDGEMENT
ABSTRACT
1 Introduction 1-2
1.1.1 Synopsis 1
2 Software Requirements 3
3.1 Overview 4
3.4.1 Class 6
3.4.2 Objects 6
3.4.4 Methods 6
3.5.1 Classes 7
3.5.2 Inheritance 7
3.5.3 Polymorphism 7
3.5.4 Abstraction 7
3.5.5 Encapsulation 7
4 JavaFx 8-9
4.1 Overview 8
8.2.4 Controls 29
CONCLUSION
Chapter 1 - INTRODUCTION
This report describes process involved in making a 2D Arcade game, Space War, This game is for all devices
which support latest version of Java. This chapter discusses the game overview including synopsis, target
audience, description, then focus on game design and how game is implemented .
The primary objective of this game is to kill enemies and achieve high score, He can achieve high score by
killing more number of enemy ships. Each time player kills enemy, speed of ships increase.
1.1.1 Synopsis
HeroShip is protagonist of our game, whose mission is to spread peace in Universe. He can’t achieve peace
in Universe without killing all enemies, who are doing evil things in universe. After killing 20 enemies
HeroShip get new bullets.
Arcade games were really popular in 90s Video Games. People used to play a lot such games like Snake,
Space Invaders and Pac-man. Even today people like to play such games for entertainment, we upgraded
Space Invaders so that people can enjoy it more.
This game starts with HeroShip with a gun as the weapon which fire bullets which can kill enemy ships.
When he enters into enemy territory, all ships start attacking him. To kill those alien ships he can fire
bullets through his ship and on every kill he get 1 point which allow him to upgrade his bullets. When his
score get above 20 he get new bullets to kill more number of enemies. To control peace in universe, he
needs to achieve highest score and assert dominance.
1
1.2 Game Objectives
• To create a Arcade game which will have all functionalities of Arcade games.
Video games are not just any computer software which are made to benefit user’s daily life. Games are
rather made for user’s entertainment purpose, so more than anything we need to pay attention to what
the user wants from the game, how to make it more entertaining, just making any game will not do. That
is why it’s more challenging because I always have to carefully consider if I’m making developing it
correctly to entertain users. I also have to invest a lot of time on the proper game designing to make it
visually accepted, And to ass that game requires a lot of scripts. The scripts are like pieces of a puzzle
which you need to put all of them together to make it work. Thus I think game is a perfect project to prove
myself as a CSE student.
2
Chapter 2 – SOFTWARE REQUIREMENT
Arcade Game Software: Space War is developed in Java using JavaFX library, It can run on any platform
which support latest version of java.
3
Chapter 3 – JAVA INTRODUCTION
This chapter gives basic introduction of java. We will discuss about Features of Java, History of Java, Basic
Syntax of Java, Object Orient Concepts and JavaFX library.
3.1 Overview
Java, the programming language, was introduced by Sun Microsystems. This work was initiated by James
Gosling and the final version of Java was released in the year 1995. However, initially Java was released
as a component of the core Sun Microsystem platform for Java called J2SE or Java 1.0. The latest release
of Java or J2SE is Java Standard Version 6.
The rising popularity of Java, as a programming platform and language has led to the development of
several tools and configurations, which are made keeping Java in mind. For instance, the J2ME and J2EE
are two such configurations. The latest versions of Java are called Java SE and Java EE or Java ME instead
of J2SE, J2EE and J2ME. The biggest advantage of using the Java platform is the fact that it allows you to
run your code at any machine. So, you just need to write your code once and expect it to run everywhere.
Java have many features which attract many programmers to choose this language for both software
development and web development.
• Object Oriented - In Java, everything is an object. Java can be effectively stretched out
and extended to unimaginable dimensions since it is focused around the Object model.
4
• Simple - Java is intended to be not difficult to learn. In the event that you comprehend
the essential idea of OOP, Java would not be difficult to ace.
• Secure - With Java’s security framework, it empowers to create frameworks, which are
free of viruses and tampering. Public-key encryption is used as the core authentication
strategy.
• Portability - The fact that Java code is machine and platform independent makes it
extremely compact. Compiler in Java is composed in ANSI C with a clean conveyability
limit, which is a POSIX subset.
• Robustness - Java tries to kill circumstances, which can lead to potential system failures,
by stressing chiefly on runtime checking and compile time checking.
• Interpreted Code - Java byte code is interpreted on the fly to local machine. The
advancement methodology is more quick and expository since the interfacing is an
incremental and lightweight process.
• High Performance - With the utilization of Just-In-Time compilers, Java enhances the
performance of the system.
• Dynamic - Java is thought to be more dynamic than C or C++ since it is intended to adjust
to an advancing environment. Java projects can convey broad measure of run-time data
that can be utilized to check for accesses and respond to the same on run-time.
5
3.3 History of Java
James Gosling started working on the Java programming language in June 1991 for utilization in one of his
numerous set-top box ventures. The programming language, at first, was called Oak. This name was kept
after an oak tree that remained outside Gosling’s office. This name was changed to the name Green and
later renamed as Java, from a list of words, randomly picked from the dictionary. Sun discharged the first
open usage as Java 1.0 in 1995. It guaranteed Write Once, Run Anywhere (WORA), giving no-expense run-
times on prominent stages. On 13 November 2006, Sun discharged much of Java as free and open source
under the terms of the GNU General Public License (GPL). On 8 May 2007, Sun completed the procedure,
making the greater part of Java’s center code free and open-source, beside a little parcel of code to which
Sun did not hold the copyright.
A basic Java program can be broken down into several constructs and elements. Typically, it can be
characterized as a collection of objects, which communicate with each other by calling each other’s
routines. The basic definitions of objects and classes are given below:
• Class - A class can be described as a blueprint that portrays the practices/expresses all the
behaviors and states of its objects.
Object - Objects are characterized by two components namely, methods and attributes or
variables. For instance, if you consider the example of a puppy, then it has the following attributes
or states: name, color and breed. In addition, it also has the following behaviours, which include
woofing, wagging and consuming. Any object is nothing but an instance of a class.
• Instance Variables - Each object has its set of variables. An object’s state is made by the qualities
alloted to these variables during program execution.
• Methods - A method is the definition of a method. Moreover, a class can contain numerous
methods. It is in these methods that behaviours like where the rationales are composed,
information is controlled and all the activities are executed.
6
3.5 Object Orient in Java
Object means a real-world entity such as a pen, chair, table, computer, watch, etc. Object-Oriented
Programming is a methodology or paradigm to design a program using classes and objects. It simplifies
software development and maintenance by providing some concepts like Classes, Inheritance,
Polymorphism, Abstraction, Encapsulation.
3.5.1 Classes
A class can also be defined as a blueprint from which you can create an individual object. Class doesn't
consume any space.
3.5.2 Inheritance
When one object acquires all the properties and behaviors of a parent object, it is known as inheritance.
It provides code reusability. It is used to achieve runtime polymorphism.
3.5.3 Polymorphism
If one task is performed in different ways, it is known as polymorphism. For example: to convince the
customer differently, to draw something, for example, shape, triangle, rectangle, etc.
Another example can be to speak something; for example, a cat speaks meow, dog barks woof, etc.
3.5.4 Abstraction
Hiding internal details and showing functionality is known as abstraction. For example phone call, we
don't know the internal processing.
3.5.5 Encapsulation
Binding (or wrapping) code and data together into a single unit are known as encapsulation. For
example, a capsule, it is wrapped with different medicines.A java class is the example of encapsulation.
Java bean is the fully encapsulated class because all the data members are private here.
7
Chapter 4 - JAVAFX
In this chapter we will learn about JavaFx library, features of JavaFx and how we can use it inside our
project.
4.1 Overview
JavaFX is a collection of Java packages that lets you add fancy graphical user interfaces to your Java
applications. With JavaFX, you can create traditional windows-style user interfaces that include familiar
controls such as labels, buttons, text boxes, check boxes, drop-down lists, and so on. But you can also
adorn these user interfaces with fancy effects such as light sources, perspective, and animation. Hence
the FX in JavaFX.
• Java APIs - JavaFX is a Java library that consists of classes and interfaces that are written in native
Java code. The APIs are designed to be a friendly alternative to Java Virtual Machine (Java VM)
languages, such as JRuby and Scala.
• FXML and Scene Builder - FXML is an XML-based declarative markup language for constructing a
JavaFX application user interface. A designer can code in FXML or use JavaFX Scene Builder to
interactively design the graphical user interface (GUI). Scene Builder generates FXML markup that
can be ported to an IDE where a developer can add the business logic.
• WebView - A web component that uses WebKitHTML technology to make it possible to embed
web pages within a JavaFX application. JavaScript running in WebView can call Java APIs, and Java
APIs can call JavaScript running in WebView.
• Swing interoperability - Existing Swing applications can be updated with new JavaFX features, such
as rich graphics media playback and embedded Web content.
• Built-in UI Controls and CSS - JavaFX provides all the major UI controls required to develop a full-
featured application. Components can be skinned with standard Web technologies such as CSS
8
• Canvas API - The Canvas API enables drawing directly within an area of the JavaFX scene that
consists of one graphical element (node).
• Multi-touch support - JavaFX provides support for multitouch operations, based on the
capabilities of the underlying platform.
• Hardware-accelerated graphics pipeline - JavaFX graphics are based on the graphics rendering
pipeline (Prism). JavaFX offers smooth graphics that render quickly through Prism when it is used
with a supported graphics card or graphics processing unit (GPU). If a system does not feature
one of the recommended GPUs supported by JavaFX, then Prism defaults to the Java 2D software
stack.
• High-performance media engine - The media pipeline supports the playback of web multimedia
content. It provides a stable, low-latency media framework that is based on the GStreamer
multimedia framework.
• Self-contained application deployment model - Self-contained application packages have all of the
application resources and a private copy of the Java and JavaFX runtimes. They are distributed as
native install-able packages and provide the same installation and launch experience as native
applications for that operating system.
JavaFX was originally developed by Chris Oliver at SeeBeyond and it was called F3 (Form Follows Function).
F3 was a Java scripting language for easily developing GUI applications. It offered declarative syntax, static
typing, type inference, data binding, animation, 2D graphics, and Swing components. SeeBeyond was
bought by Sun Microsystems and F3 was renamed JavaFX in 2007. Oracle acquired Sun Microsystems in
2010. Oracle then open sourced JavaFX in 2013.
The first version of JavaFX was released in the fourth quarter of 2008. The current release for JavaFX is
version 8.0. The version number jumped from 2.2 to 8.0. From Java 8, the version numbers of Java SE and
JavaFX are same.
9
Chapter 5 – Feasibility Study
This chapter describes all the feasibilities that come as questions to both the developers and other users
during the development of software. The chapter contains financial feasibility, technical feasibility,
economic feasibility and legal feasibility.
For the PC players, it is completely a free to play game, It will not even consume any internet data.
Additionally if we add some premium weapons or player skins in future updates the player need to pay to
acquire them which is a choice for the players and is not must.
The points mentioned above indicate that the project is financially feasible.
The tools and technology that were used in the making Space Ware are:
These mentioned above technologies are completely free for students. There were no other additional
tools required to make this game.
IntelIij IDEA Community edition provides all features to write code, maintenance library and deployment
of project in Jar file. We added Maven in IDE to maintain all resources and libraries.
The simplicity of the projects along with the facts mentioned above proves that this software is also
technically feasible.
10
5.3 Economic Feasibility
• Development machine. Any regular laptop/PC with a Minimum Ram of 4GB and a decent GPU can
be used for the development of this game.
• Technical tools and software. As mentioned previously, the tools needed to develop this software
are available to developers at no change.
Although for now I have used free assets to design the game, if I want to release the game for monetizing
purposes this project will also require graphic designers for the game to get proper recognition. As these
are largely free of cost, the only expenses lie in the payment of the programming individual(s), graphic
designers and the machine. Neither of these is scarce in availability nor are they extremely expensive.
The game assets that were used to make this game are completely free with either CC0 or CC3 licenses so
I can use them as long as I properly credit them which I already did by uploading the project to github.
And as mentioned in the previous sections this game uses freely available software and tools which are
intended for the use of game developers everywhere. There cannot be any conflict regarding any illegal
use of any software, because I have already followed the rules of the licenses to make this game.
11
Chapter 6 – System Design
12
6.2 Class Diagram (UML Diagram)
These class diagrams will explain classes in simpler way and will show relation between each class.
Hero class is derived from ImageView class from javafx.scene.image.ImageView. Object of Hero class is
created inside GameScene to create our hero battle ship in our game.
It contains getHeroX() method which return current X position of Hero object and setHeroX(double x)
which set X position of Hero object.
ImageView
Hero
-path:File
-image:Image
+getHeroX():double
+setHeroX(double x)
HeroBullets class is derived from Rectangle class from javafx.scene.shape.Rectangle. Object of Hero class
is passed through constructor of HeroBullets class. HeroBullets class is used to fire bullet whenever player
press spacebar.
It contains move() method to move bullets upward, collide(Enemies enemies) method take object of
Enemies and return true whenever enemy ship collide with bullet and shipCase(int x) method which
change skin of ship of our hero.
Rectangle
HeroBullets
<<constructor>>HeroBullets(Hero hero)
+move()
+collide(Enemies enemies):boolean
+ship(int x)
13
6.2.3 Enemies Class
Enemies class extends ImageView class. Object of Enemies class is created inside GameScene class. It is
used to create enemy ship in our game. Enemies are created randomly using Random Class of java.util.
Constructor of this class takes double posX, double posY, double height and doube width as parameter
and this parameters are used to plot Enemy ship on game stage.
It contains move() method to move enemy ships, show() method return Enemies object to show how
many enemy ship are there on stage, getPosY() return Y position of enemy ship in double data type,
collide(Hero hero) method return true if enemy ship collide with hero ship.
ImageView
Enemies
-images:Image[]
-random:Random
PauseScreen class extends StackPane class from javafx.scene.layout.StackPane. Whenever user press P
button on keyboard object of PauseScreen class get created and pause game.
StackPane
PauseScreen
-rectangle:Rectangle
-head:Text
14
6.2.5 Space Class
Space class extends Circle class from javafx.scene.shape.Circle. This class create environment of space on
our game stage. Object of this is class is created inside GameScene class.
Circle
Space
- random:Random
- RADIUS:int
+move(double SPEED)
GameScene class contain main method, this class is main stage of our game in which object of all other
classes is created. It contains object of Hero, Enemies, Space , StackPane, HeroBullets, VFXSound,
PauseScreen and StartMenu class. This class contain many methods like
1) gameReset():- This method reset whole game when player click on New Game button.
2) gameScene():- This method control whole game from showing current score to checking collision
between hero ship and enemy ship and between enemy ship and bullet.
3) gameStartMenu():- This method is called just after game is opened, this method create object of
StartMenu class.
4) start():- This method overrides the start method from Application class, this method create window for
our game, control user inputs and call gameScene() method.
6) controls(KeyCode keyCode, Hero hero):- This method take inputs from user through keyboard and
control hero ship.
15
7) setSHIPCASE(int x):- change hero ship.
GameScene
-stackpane:StackPane
-pane:Pane
-SCORE:int
-gameScene:GameScene(this)
-hero:Hero
-START:boolean
-SHIPCASE:int
-vfxSound:VFXSound
-vfxCheck:boolean
-bgMusicCheck:boolean
-enemies:Enemies
+setSHIPCASE(int x)
+setStart(boolean flag)
+getBgMusicCheck():boolean
+getVfxCheck():boolean
+setVfxCheck(boolean x)
+setBgMusicCheck(boolean x)
+gameReset();
SelectShip class extends AnchorPane. This class create menu to select skin for our hero ship. User click on
new skin and our hero ship get changed into that ship.
This class doesn’t have any method, It just take parameter through constructor and create Select Ship
menu whenever user click on “Select Ship” button from Option Menu window.
AnchorPane
SelectShip
-ship1, ship2. ship3, ship4, ship5,
ship6:ImageView
16
6.2.8 StartMenu Class
StartMenu class extends AnchorPane. This class is responsible for create start menu when game starts. It
contains “New Button” and “Options” button.
Its constructor take object of StackPane, GameScene, Pane, Hero, and VFXSound class as parameter.
AnchorPane
StartMenu
-logo:Text
-newGame:Text
-option:Text
<<constructor>>StartMenu(StackPane stackPane,
GameScene gameScene, Pane pane, Hero hero,
VFXSound vfxSound)
OptionMenu class extends AnchorPane. It is used to show options like “Select Ship”, “VFXSound
enable/disable”, “Background Music enable/disable”.
Its constructor take object of StackPane, AnchorPane ,GameScene, Pane, Hero, and VFXSound class as
parameter.
AnchorPane
OptionMenu
-selectShip:Text
-vfxBox:CheckBox
-backgroundSound:CheckBox
VfxCheck:boolean
BgMusicCheck:boolean
<<constructor>>OptionMenu(StackPane
stackPane, Hero hero, Pane pane, AnchorPane
anchorPane, GameScene gameScene, VFXSound
vfxSound)
17
6.2.10 GameOverScreen Class
This class extends VBox class from javafx.scene.layout.Vbox. This class is used to create overlay when hero
ship get killed. User can again start new game by clicking “New Game” button from GameOverScreen.
VBox
GameOverScreen
-head:Text
-scoreToShow:Text
VFXSound class is responsible for VFX sound and background sound in our game. This class contain many
methods to control background music and VFX sound.
2) bombSound():- Play bomb sound when enemy ship get killed using bullet.
VFXSound
-background:AudioClip
-fireClip:AudioClip
-bombClip:AudioClip
<<constructor>>VFXSound()
+fireSound()
+bombSound()
+muteAll()
+unmuteAll()
+muteBgMusic()
+unmuteBgMusic()
18
6.3 Relationship Among Classes
GameScene
Circle ImageVie
-stackpane:StackPane w
-pane:Pane
-SCORE:int
-gameScene:GameScene(this)
-hero:Hero
-START:boolean
-SHIPCASE:int Hero
Space -vfxSound:VFXSound
-vfxCheck:boolean -path:File
-random:Random
-bgMusicCheck:boolean -image:Image
-RADIUS:int
-enemies:Enemies
+move(double SPEED) +getHeroX():double
+setSHIPCASE(int x)
+setHeroX(double x)
+setStart(boolean flag)
+getBgMusicCheck():boolean
+getVfxCheck():boolean
+setVfxCheck(boolean x)
+setBgMusicCheck(boolean x)
+gameReset();
ImageView Rectangle
Enemies HeroBullets
-images:Image[] <<constructor>>HeroBullets(Hero hero)
-random:Random +move()
+collide(Enemies enemies):boolean
<<constructor>>Enemies(double posX, +ship(int x)
double posY, double height, double width)
+move(double SPEED)
+show():Enemies(this)
+getPosY():double
+collide(Hero hero):boolean
AnchorPan
AnchorPane e
StartMenu
OptionMenu
-logo:Text
-selectShip:Text -newGame:Text
-vfxBox:CheckBox -option:Text
-backgroundSound:CheckBox
VfxCheck:boolean <<constructor>>StartMenu(StackPane
BgMusicCheck:boolean stackPane, GameScene gameScene, Pane
pane, Hero hero, VFXSound vfxSound)
<<constructor>>OptionMenu(StackPane
stackPane, Hero hero, Pane pane,
AnchorPane anchorPane, GameScene
gameScene, VFXSound vfxSound)
19
AnchorPane OptionMen
u
SelectShip
-ship1, ship2. ship3, ship4, ship5,
ship6:ImageView
VFXSound
GameScene -background:AudioClip
-fireClip:AudioClip
-bombClip:AudioClip
<<constructor>>VFXSound()
StackPane +fireSound()
+bombSound()
+muteAll()
+unmuteAll()
+muteBgMusic()
+unmuteBgMusic()
PauseScreen
-rectangle:Rectangle
-head:Text
VBox
GameOverScreen
-head:Text
-scoreToShow:Text
20
Chapter 7 – USER INTERFACE
User Interface (UI) is the visual part of an application or device that determines how a user interacts with
it and how information is displayed on the screen.
This User Interface chapter will provide us detailed view of our game.
View of our game is Top View is Top View which is used in most of the classical arcade games.
Because of this view we can see Hero Ship as well as Enemy ship clearly.
The HUD shows vital information about the game and allows for additional actions.
Hero Ship is our main protagonist which is controlled by the player and kill enemies using bullets.
Player can move Hero Ship using arrow keys and can fire using space bar. He can change skin of Hero
Ship using “Select Ship” option from “Option Menu”.
21
7.2.2 Score Board
Score board shows the real time score of player. Whenever player kill enemy, his score get incremented
by one.
Game Stage is main window of our game from where player can play game. Here player can move Hero
Ship and can fire bullets, Enemies appear here and player can kill enemies by firing bullets.
22
7.2.4 Start Menu
Start Menu is the first window appears when we open our game. We can start our game from here, we
can go into option menu by clicking on “Option” button from this menu.
Option Menu provide options to player to change skin of Hero Ship, to turn on and off VFX sounds like
firing sound, explosion sound when enemy ship get killed and background music using checkbox.
23
7.2.6 Game Over Screen
Game Over Screen appears when Hero Ship get killed. We can start new game from Game Over Screen.
Select Ship Screen is for changing skin Hero Ship. Player can change appearance of Hero Ship just by
clicking on the desired ship.
24
7.2.8 Pause Screen
Pause Screen is showed when game is paused. Player can pause game by pressing P on keyboard and
can resume game by again pressing P on keyboard.
25
Chapter 8 – PROGRAMMING AND IMPLEMENTING GAME
This Chapter will talk about some programming stuffs and how we implemented this game.
We used single package in our game and its called “game” which is inside “src” folder. “src” folder is
automatically created by IntellIj IDEA IDE.
Inside “game” package we have all java files we need to develop our game.
“images” folder contain all images we need in our game, example HeroShip image to create our
protagonist.
“Sounds” folder contain our background music and VFX sound like fire sound, explosion sound.
Space War/
images
heroShip.png
ship1.png
ship2.png
ship3.png
ship4.png
ship5.png
ship6.png
enemyShip1.png
enemyShip2.png
enemyShip3.png
enemyShip4.png
enemyShip5.png
sounds
backgroundMusic.wav
bomb.wav
fire.wav
26
src
java
main
game
Enemies.java
GameLauncher.java
GameOverScreen.java
GameScene.java
Hero.java
HeroBullets.java
OptionMenu.java
PauseScreen.java
SelectShip.java
Space.java
StartMenu.java
VFXSound.java
target
classes
generate-sources
maven-archiver
maven-status
GameScene-1.0-SNAPSHOT.jar
pom.xml
GameScene.iml
SpaceInvader.iml
27
8.2 Functionality
Here we will talk about how we created elements of our game with code.
}
public static void main(String[] args){
launch(args)
}
}
We used Circle class of JavaFX library to create elements of space and set background color of StackPane
to black to show dark space.
package game;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import java.util.Random;
28
8.2.3 Creating Hero Ship
We created hero ship using an png image. We added image using ImageViewer class from JavaFX library.
package game;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import java.io.File;
this.setFitHeight(height);
this.setFitWidth(width);
this.setTranslateX(480);
this.setTranslateY(650);
}
public double getHeroX(){
return this.getTranslateX();
}
}
8.2.4 Controls
//Usual Code
scene.setOnKeyPressed(e ->{
if(!GAME_OVER && collide && START){
if (e.getCode() == KeyCode.P)
checkPause();
else
controls(e.getCode(), hero);
}
});
}
public void controls(KeyCode keyCode, Hero hero){ //This method control everything.
if (keyCode == KeyCode.RIGHT)
hero.setTranslateX(hero.getHeroX() + 10); //Move Hero Ship to the Right Side
if (keyCode == KeyCode.LEFT)
hero.setTranslateX(hero.getHeroX() - 10); //Move Hero Ship to the Left Side
if (keyCode == KeyCode.SPACE) {
bullets = new HeroBullets(hero);
bullets.shipCase(SHIPCASE); //Check Skin of Hero Ship
bulletsList.add(bullets); //Fire bulltes
pane.getChildren().add(bullets);
vfxSound.fireSound(); //Play sound of gun when bullet is fired
}
}
}
29
8.2.5 Creating Enemy Ship
We created Enemy ships in same way as we created hero ship. We just added random position of ships
so that Enemy ships can appear randomly on screen.
package game;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import java.io.File;
import java.net.MalformedURLException;
import java.util.Random;
Enemies(double posX, double posY, double height, double width) throws MalformedURLExcep-
tion {
this.setImage(images[random.nextInt(5)]);
this.setFitWidth(width);
this.setFitHeight(height);
this.setTranslateX(posX);
this.setTranslateY(posY);
}
public void move(double SPEED){ //Move the ship according to given speed.
this.setTranslateY(this.getTranslateY()+SPEED);
}
30
8.2.6 Creating Bullets
We used Rectangle class from JavaFX library. Whenever player press Space bar key from keyboard, new
object of HeroBullets class.
package game;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
31
8.2.7 Creating Start Menu
package game;
this.setTranslateX((SCENE_WIDTH/2)-150);
this.setTranslateY((SCENE_HEIGHT/2)-150);
newGame.setOnMouseClicked(e ->{
//trigger all statements under braces when ‘New Game’ Button is pressed.
});
option.setOnMouseClicked(e->{
});
this.getChildren().addAll(logo,newGame, option);
stackPane.getChildren().add(this);
}
}
32
8.2.9 Creating Option Menu
package game;
33
8.2.10 Creating Select Ship Menu
package game;
import java.io.File;
import java.net.MalformedURLException;
this.setTranslateX((SCENE_WIDTH/2)-150);
this.setTranslateY((SCENE_HEIGHT/2)-150);
this.getChildren().add(gridPane);
}
}
34
8.2.11 VFX and Background Sound
We used wav format for background music and for VFX sound.
package game;
import javafx.scene.media.AudioClip;
import java.io.File;
}
public void unmuteBgMusic(){ //Unmute Background Music.
background.play();
}
}
35
Chapter 9 – FUTURE PLANS
This Chapter will talk about future plans for our game project. We can make it more interesting in future
by adding many features.
Even after completing our project we still have some errors and bugs left in our game which are minor
but still may cause issue for some players.
• We get run time error sometime when player fire bullets. Sometime it doesn’t even play gun firing
sound when player fire bullets.
• Random position of enemy ship cause overlap of ships, which we can fix by checking positon of
other enemy ships.
• All elements of space get removed when player press ‘New Game’ after game over.
• Hero get killed even when enemy ship is little far from hero ship, we can fix this issue by changing
intersection values of enemy ship and hero ship.
• Right now we don’t have back button on every menu which make difficult for player to return to
main menu, we can fix this issue by adding back button on every menu screen.
We can add more bullets in our future update so that player can enjoy our game more.
Right now we have feature of upgradation of bullet when player complete score of 20. We can add more
bullets when score get incremented by 20.
In future update, Player can even select his own bullet from option menu when new bullet get unlocked.
We will add new levels in our game. New levels with new enemies, new boss, new background sound,
new hero ships and new weapons which player can change from option menu.
36
9.4 Adding Public Score Board
We can add public score board in our game which will show high score of whoever play this game in
world.
For this we will add login functionality in this through which player can register his name in game and
whenever he will get high score, this high score will be uploaded on public score board with his name or
email id.
37
CONCLUSION
It was an exciting project to work on and there is a lot I learnt from it, above and beyond its original scope.
I was able to study and train myself on development in an environment that was almost completely
foreign to me, which I believe I have been reasonably successful with.
Although I was not able to complete some of the proposed functionality, the functionality related to the
core purpose of the application is working as desired. I believe there is still a lot of potential for this
application, and will continue development in the future.