logo

Sky War Java Implementation

   

Added on  2022-11-27

7 Pages943 Words275 Views
Sky War Java Implementation
Name
Institution

Program Design
The SkyWar game is developed using 8 java classes, namely;
GameLogic: This class holds all methods that are responsible for implementing the game.
The class creates threads that move the Master Ship as well as the Enemy Ships. Each
type of ship is moved by it’s own thread, which handles the movements and determines
when a given ship is destroyed.
MasterShip: The class creates the master ship.
Square: This is the individual cell that makes up a grid for the game.
SkyWar: The class creates the GUI for the project. It makes use of Squares to create a
swing GUI, then calls the Game Logic.
EnemyShip: The enemy ship is a parent class to various types of enemy vessels, the class
displays the use of polymophism and inheritance. More types of Enemy ships can be
added to the setup, by simply defining the additional classes. The child classes include;
o BattleCruiser
o BattleShooter
o BattleStar
Figure 1.0 Class diagram of the developed application.
Main Features

The main features of the game are the graphical user interface, the game logic and the objects that
make up the enemy and the master ship. The detailed features are as follows;
Graphical User Interface: The games GUI is made using a JFrame, two panels and 16 JButtons
that forms the grid. An upper panel is used as the base for the buttons while a lower panel is used
for commands; in this case the exit command. The Buttons are arranged into a grid by use of the
GridLayout Manager. Each JButton is made from the class Square; this class places features such
as color change on the buttons to be able to change the color on the grid depending on the type of
object on a specific button. When the Master Ship is on a button, the button turns green.
Similarly, when the button is blank – meaning no ship is on the button – the button is painted
blue; representing the sky. For the enemy ships, each type of ship has its own distinct color
defined in their classes.
Game Movements: to facilitate movement of objects, java threads are used. Threads are created
using the Runnable interface for the Master Ship and for each type of enemy ship created. The
thread runs in an infinite loop until an interrupt occurs. Interrupts are induced when the game
over condition in attained; in this case when the master ship moves into a grid cell with more than
1 enemy ship. For stealth mode, the game is over when the master ship moves into a cell with
more than 2 enemy ships.
A thread is created to move the ship, this thread calls an inner class that implements the Runnable
interface, making it possible to create a thread from the class as follows;
Thread t1 = new Thread(new MoveMasterShip());
threads.add(t1);
t1.start();
The private class handling the movement of the master ship is;
private class MoveMasterShip implements Runnable {
@Override
public void run() {
while (true) {
ArrayList<Integer> posiblePositions =
getPossibleMoves(masterShipIndex);
Random rand = new Random();

End of preview

Want to access all the pages? Upload your documents or become a member.