Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

If you have never seen the original Asteroids game, you can see a sample of the

ID: 3587726 • Letter: I

Question

If you have never seen the original Asteroids game, you can see a sample of the (Arcade) Asteroids game in Youtube. https://www.youtube.com/watch?v=WYSupJ5r2zo. However, your game will be slightly different, and it is not necessary to be familiar with the original Asteroids game to do any of the assignments during the semester. The initial version of your game will be text-based. As the semester progresses we will add graphics, animation, and sound. The goal of this first assignment is to develop a good initial class hierarchy and control structure, and to implement it in Java. This version uses keyboard input commands to control and display the contents of a “game world” containing a set of objects in the game. In future assignments, many of the keyboard commands will be replaced by interactive GUI operations, but for now we will simply simulate the game in “text mode” with user input coming from the keyboard and “output” being lines of text on the screen. Program Structure Because you will be working on the same project all semester, it is extremely important to organize it correctly from the beginning. Pay careful attention to the class structure described below and make sure your program follows this structure accurately. The primary class encapsulates the notion of a Game. A game in turn contains several components: (1) a GameWorld which holds a collection of game objects and other state variables, and (2) a play() method to accept and execute user commands. Later, we will learn that a component such as GameWorld that holds the program’s data is often called a model. The top-level Game class also encapsulates the flow of control in the game (such a class is therefore sometimes called a controller). The controller enforces rules such as what actions a player may take and what happens as a result. This class accepts input in the form of keyboard commands from the human player and invokes appropriate methods in the game world to perform the requested commands – that is, to manipulate data in the game model. In this first version of the program the top-level game class will also be responsible for displaying information about the state of the game. In future assignments, we will learn about a separate kind of component called a View which will assume that responsibility. When you create your CN1 project, you should name the main class as Starter. Then you should modify the start() method of the Starter class so that it would construct an instance of the Game class. The other methods in Starter (i.e., init(), stop(), destroy()) should not be altered or deleted. The Game class must extend from the build-in Form class (which lives in com.codename1.ui package). The Game constructor instantiates a GameWorld, calls a GameWorld method init() to set the initial state of the game, and then starts the game by calling a Game method play(). The play() method then accepts keyboard commands from the player and invokes appropriate methods in GameWorld to manipulate and display the data and game state values in the game model. Since CN1 does not support getting keyboard input from command prompt (i.e., the standard input stream, System.in, supported in Java is not available in CN1) the commands will be entered via a text field added to the form (the Game class). Refer to “Appendix – CN1 Notes” for the code that accepts keyboard commands through the text field located on the form. The following shows the pseudo-code implied by the above description. It is important for things that we will do later that your program follows this organization: Game World Objects For now, assume that the game world size is fixed and covers 1024(width) x 768(height) area (although we are going to change this later). The origin of the “world” (location (0,0)) is the lower left hand corner. The game world contains a collection which aggregates objects of abstract type GameObject. There are two kinds of abstract game objects: “fixed objects” with fixed locations (which are fixed in place) and “moveable objects” with changeable locations (which can move or be moved about the world). For this first version of the game there is just a single kind of fixed object: a space station; and there are three kinds of moveable objects: ships, missiles (which are objects fired by ships), and asteroids. Later we will see that there are other kinds of game objects as well. The various game objects have attributes (fields) and behaviors (“functions” or “methods”) as defined below. These definitions are requirements which must be properly implemented in your program. • All game objects have a location, defined by floating point values X and Y. (you can use float or double to represent it) non-negative values X and Y which initially should be in the range 0.0 to 1024.0 and 0.0 to 768.0, respectively. The point (X,Y) is the center of the object. Hence, initial locations of all game objects should always be set to values such that the objects’ centers are contained in the world. All game objects provide the ability for external code to obtain their location. By default, game objects provide the ability to have their location changed, unless it is explicitly stated that a certain type of game object has a location which cannot be changed once it is created. Except for ships, missiles initial locations of all the game objects should be assigned randomly when created. • All game objects have a color, defined by a int value (use static rgb() method of CN1’s built-in ColorUtil class to generate colors). All objects of the same class have the same color (chosen by you), assigned when the object is created (e.g, ships could be green, asteroids could be red). All game objects provide the ability for external code to obtain their color. By default, game objects provide the ability to have their color changed, unless it is explicitly stated that a certain type of game object has a color which cannot be changed once it is created. • Moveable objects have attributes speed and direction (heading), which are used to define how they move through the world when told to do so. Speed is an integer in the range 0-10, initialized to a random value in that range unless otherwise stated. Direction is an integer in the range 0..359, initialized to a random value in that range unless specified otherwise, indicating a compass heading. (Thus, a direction of zero specifies heading “north” or “up the screen”; a direction of 90 specifies heading “east” or toward the right side of the screen, etc.) • Moveable objects provide an interface that allows other objects to tell them to move. Telling a moveable object to move() causes the object to update its location based on its current speed and direction (heading). See below for details on updating a movable object’s position when its move() method is invoked. • Some moveable objects are steerable, meaning that they provide an interface that allows other objects to tell them to change their direction and/or speed. • Ships are steerable objects whose speed and direction (heading) can be controlled by the player. There is only one ship in the game at any one time, and its initial location should be the center of the world (e.g, 512,384), with a speed of zero and a heading of zero (north). Ships have an attribute called missile count, which is an integer representing the number of missiles the ship has available to be fired. Each ship starts with the maximum number of missiles, which is 10. Each time a ship fires a missile its missile count is decremented by one; when missile count reaches zero it can no longer fire any missiles. However, ships that fly to a space station automatically have their missile count reloaded to the original maximum number. • Missiles are moveable objects that are fired from ships (that is, they always start at a location matching the current ship). When a missile is fired, it has a direction matching its ship’s heading (that is, missiles are fired out the front of the ship), and a speed that is some fixed amount greater than the speed of its ship (you may decide the amount). Missiles also have an attribute called fuel level, a positive integer representing the number of time units left before the missile becomes dead and disappears. A missile’s fuel level gets decremented as time passes, and when it reaches zero the missile is removed from the game. All missiles have an initial fuel level of 10. Note that while missiles are moveable, they are not steerable; that is, there is no way to change a missile’s speed or heading once it is fired. The program must enforce this constraint. • Asteroids are movable objects that tumble through space at a fixed speed and direction. Asteroids also have a fixed attribute size, an integer giving the dimensions of the asteroid. The size, speed, and direction of an asteroid are to be randomly generated when the asteroid is created. If a missile collides with an asteroid, the asteroid (and the missile) are destroyed and removed from the game, and the user scores some points (you may decide how many). If a ship collides with an asteroid, the ship and the asteroid are destroyed and the player loses one “life”. A complete game consists of playing until three ships have been destroyed. • Fixed objects have unique integer identification (id) numbers; every fixed object (of any subtype) has an integer id which is different from that of every other fixed object. (Note that this is an appropriate place to use a static int in a Java class; such a value can be used to keep track of the next available id number each time an instance of the class is constructed.) The program must enforce the rule that it is not possible to change either the ID number or the location of a fixed object once it has been created. • Space stations are fixed objects that have an attribute called their blink rate; each space station blinks on and off at this rate (blink rate specifies the period; a blink rate of 2, for example, means the light on the space station is on for two seconds and then off for two seconds). Blink rate is initialized to a random value between zero and ten. Space stations also house an unlimited supply of missiles; any ship arriving at a space station automatically gets a full resupply of missiles (up to the maximum number of missiles a ship can hold)

Game Play
When your program begins, there are no objects in the world. The player can enter
commands (see below) to control various aspects of the game such as adding objects, moving
the ship, firing missiles, etc. The game keeps track of time using a clock, which is simply an
integer counter. The objective is to achieve the highest possible score in the minimum amount
of time – that is, to destroy as many asteroids as possible, flying to a space station to restock
missiles as necessary, without losing ships, all in minimum time.
Commands
Once the game world has been created and initialized, the Game constructor is to call a
method name play() to actually begin the game. play() accepts single-character commands from
the player via the text field located on the form (the Game class) as indicated in the “Appendix
– C1 Notes”.
Any undefined or illegal input should generate an appropriate error message on the
console and ignore the input. Single keystrokes invoke action -- the human hits “enter” after
typing each command. The allowable input commands and their meanings are defined below
(note that commands are case sensitive):
‘a’ – add a new asteroid to the world.
‘b’ – add a new blinking space station to the world.
‘s’ – add a new ship to the world.
‘i’ – increase the speed of the ship by a small amount.
‘d’ – decrease the speed of the ship by a small amount, as long as doing so doesn’t make
the speed go negative (the ship can’t go backwards).
‘l’ (ell) – turn left by a small amount (change the heading of the ship by a small amount in the
counter-clockwise direction). Note that determination of the actual new heading is not
the responsibility of the game class; it simply instructs the game world that a change is
to be made.
‘r’ – turn right (change the ship heading by a small amount in the clockwise direction).
‘f’ – fire a missile out the front of the ship. If the ship has no more missiles, an error message
should be printed; otherwise, add to the world a new missile with a location, speed, and
heading determined by the ship.
‘j’ – jump through hyperspace. This command causes the ship to instantly jump to the initial
default position in the middle of the screen, regardless of its current position. (This
makes it easy to regain control of the ship after it has left visible space; later we will see
a different mechanism for viewing the ship when it is outside screen space.)
‘n’ – load a new supply of missiles into the ship. This increases the ship’s missile supply to
the maximum. It is permissible to reload missiles before all missiles have been fired,
but it is not allowed for a ship to carry more than the maximum number of missiles.
Note that normally the ship would actually have to fly to a space station to reload; for
this version of the game we will assume the station has transporters that can transfer
new missiles to the ship regardless of its location.
‘k’ – a missile has struck and killed an asteroid; tell the game world to remove a missile and
an asteroid and to increment the player’s score by some amount of your choosing. You
may choose any missile and asteroid to be removed; later we’ll worry about missiles
needing to be “close to” their victims. You may assume that this command will not be
invoked unless there is at least one missile and one asteroid in the world.
‘c’ – the ship has crashed into an asteroid; tell the game world to remove the ship and an
asteroid and to decrement the count of lives left; if no lives are left then the game is
over. You may choose any asteroid to be removed; later we’ll worry about asteroids
needing to be “close to” the ship. You may assume that this command will not be
invoked unless there is at least one ship and one asteroid in the world.
‘x’ -- two asteroids have collided with and exterminated each other; tell the game world to
remove two asteroids from the game. You may choose any two asteroids to be
removed; later we’ll worry about the asteroids needing to be “close to” each other. You
may assume that this command will not be invoked unless there are at least two
asteroids in the world.
‘t’ – tell the game world that the “game clock” has ticked. Each tick of the game clock has
the following effects: (1) all moveable objects are told to update their positions
according to their current direction and speed; (2) every missile’s fuel level is reduced
by one and any missiles which are now out of fuel are removed from the game; (3) each
space station toggles its blinking light if the tick count modulo the station’s blink rate is
zero; and (4) the “elapsed game time” is incremented by one.
‘p’ – print a display (output lines of text) giving the current game state values, including: (1)
current score (number of points the player has earned), (2) number of missiles currently
in the ship, and (3) current elapsed time. Output should be well labeled in easy-to-read
format.
‘m’ – print a “map” showing the current world state (see below).
‘q’– quit, by calling the method System.exit(0) to terminate the program. Your program
should first confirm the user’s intent to quit before actually exiting.
Additional Details
• Each command must be encapsulated in a separate method in the Game class (later we will
move those methods to other locations). Most of the game class command actions also
invoke related actions in GameWorld. When the Game gets a command, it invokes one or
more methods in the GameWorld, rather than itself manipulating game world objects.
• All classes must be designed and implemented following the guidelines discussed in class:
o All data fields must be private,
o Accessors / mutators must be provided, but only where the design requires them,
o Game world objects may only be manipulated by calling methods in the game world. It is
never appropriate for the controller to directly manipulate game world data attributes.
• Moving objects need to determine their new location when their move() method is invoked,
at each time tick (‘t’ command). The new location can be computed as follows:
newLocation(x,y) = oldLocation(x,y) + (deltaX, deltaY), where
deltaX = cos()*speed,
deltaY = sin()*speed, and
= 90 – heading.
There is a diagram in course notes (look for the slide title “Computed Animated Location
(cont.)” in the “Introduction to Animation” chapter) showing the derivation of these
calculations for an arbitrary amount of time; in this assignment, we are assuming “time” is
fixed at one unit per “tick”, so “elapsed time” is 1.
• For this assignment, all output will be in text on the console; no “graphical” output is required.
The “map” (generated by the ‘m’ command) will simply be a set of lines describing the objects
currently in the world, similar to the following:
Ship: loc=130.0,565.5 color=[0,255,0] speed=8 dir=90 missiles=5
Missile: loc=180.3,100.0 color=[255,0,0] speed=10 dir=112 fuel=7
Missile: loc=50.9,540.2 color=[255,0,0] speed=5 dir=313 fuel=1
Asteroid: loc=440.2,20.0 color=[128,128,128] speed=6 dir=0 size=6
Asteroid: loc=660.0,50.0 color=[128,128,128] speed=1 dir=90 size=26
Station: loc=210.2,800.0 color=[255,255,0] rate=4
Note that the appropriate mechanism for implementing this output is to override the
toString() method in each concrete game object class so that it returns a String
describing itself. See the coding notes Appendix for additional details, including how to
display single decimal value. Please be conformed to the format specified in above example.
• Missiles are considered to have special radar that allows them to avoid hitting the ship, other
missiles, and space stations, so we will not worry about handling these collision conditions.
Likewise, we will assume that space stations automatically avoid asteroids.
• The program must handle any situation where the player enters an illegal command – for
example, a command to increase the ship speed when no ship has yet been added to the
world – by printing an appropriate condition-specific error message on the console (for
example, “Cannot execute ‘increase’ – no ship has been created”) and otherwise simply
waiting for a valid command.
• The program is not required to have any code that actually checks for collisions between
objects; that’s something we’ll be adding later. For now, the program simply relies on the
user to say when such events have occurred, using for example the ‘k’ and ‘c’ commands.
• You must follow standard Java coding conventions:
o class names always start with an upper case letter,
o variable names always start with a lower case letter,
o compound parts of compound names are capitalized (e.g., myExampleVariable),
o Java interface names should start with the letter “I” (e.g., ISteerable).
• Your program must be contained in a CN1 project called A1Prj. You must create your project
following the instructions given at “2 – Introduction to Mobile App Development and CN1”
lecture notes posted at SacCT (Steps for Eclipse: 1) File New Project Codename
One Project. 2) Give a project name “A1Prj” and uncheck “Java 8 project” 3) Hit “Next”. 4)
Give a main class name “Starter”, package name “com.mycompany.a1”, and select a “native”
theme, and “Hello World(Bare Bones)” template (for manual GUI building). 5) Hit “Finish”.).
Further, you must verify that your program works properly from the command prompt before
submitting it to SacCT: First make sure that the A1Prj.jar file is up-to-date. If not, under
eclipse, right click on the dist directory and say “Refresh”. Then get into the A1Prj directory
and type (all in one line): “java -cp distA1Prj.jar;JavaSE.jar
com.codename1.impl.javase.Simulator com.mycompany.a1.Starter” for Windows machines
(for the command line arguments of Mac OS/Linux machines please refer to the class notes).
Substantial penalties will be applied to submissions which do not work properly from the
command prompt.
• You are not required to use any particular data structure to store the game world objects, but
all your game world objects must be stored in a single structure. In addition, your program
must be able to handle changeable numbers of objects at runtime – so you can’t use a fixedsize
array, and you can’t use individual variables.
Consider either the Java ArrayList or Vector class for implementing this storage. Note that
Java Vectors (and ArrayLists) hold elements of type “Object”, but you will need to be able to
treat the Objects differently depending on the type of object. You can use the “instanceof”
operator to determine the type of a given Object, but be sure to use it in a polymorphicallysafe
way. For example, you can write a loop which runs through all the elements of a world
Vector and processes each “movable” object with code like:
for (int i=0; i<theWorldVector.size(); i++) {
if (theWorldVector.elementAt(i) instanceof IMovable) {
IMovable mObj = (IMovable)theWorldVector.elementAt(i);
mObj.move();
}
}

Explanation / Answer

package asteroids;

import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.geom.Rectangle2D;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Random;
import processing.core.PApplet;

/**
*
* This class is the logic center of the game. It controls the object list
* handling all operations such as adding/destroying objects, monitoring collisions,
* etc.
*/
class Game {
private PApplet canvas;
private List spaceThings;
private List createables;
private ListIterator li;
private ListIterator createablesLi;
private SpaceThing target;
Random rand = new Random();
private int gameState;
private int frame;
private Ship ship;
private int level;
private int score;
private int asteroidsRemaining;
private int shipsRemaining;
private int bulletsActive;
private int powerUps;
// These constants make switches much nicer
final int SHIP = 1;
final int ASTEROID = 2;
final int BULLET = 3;
final int NUKE = 4;
final int MENU = 0;
final int PLAYING = 1;
final int GAMEOVER = 2;
/**
*
* @param papp Main PApplet of game.
*
*/
public Game(PApplet papp) {
canvas = papp;
}

public void initMenu() {
gameState = MENU;
}

/**
* Resets all object lists, scores, etc and inits level 1.
*/
public void newGame() {
spaceThings = new LinkedList();
createables = new LinkedList();
shipsRemaining = 3;
score = 0;
createThing(SHIP);
initLevel(1);
gameState = PLAYING;
}

public void gameOver() {
spaceThings = null;
createables = null;
gameState = GAMEOVER;
}


/**
* Creates a level with newLeveL + 2 ships
* @param newLevel
*/
public void initLevel(int newLevel) {
level = newLevel;
for(int i=0; i<level+2; i++) {
createThing(ASTEROID);
}
}

/**
* Creates Ships, Asteroids, Bullets, etc and adds them to the main
* object list.
* @param thing type of SpaceThing. Ship, Asteroid or Bullet
*/
public void createThing(int thing) {
li = spaceThings.listIterator();
switch(thing) {
case SHIP:
li.add(new Ship(canvas));
break;
case ASTEROID:
li.add(new Asteroid(canvas, 1+level));
break;
}
}

/**
* Removes thing from object list.
* @param thing
*/
public void destroyThing(SpaceThing thing) {
li = spaceThings.listIterator();
li.remove();
}

public void draw() {
canvas.background(0);
if (gameState == PLAYING) {
drawObjects();
drawHud();
if(asteroidsRemaining <= 0) {
initLevel(level+1);
}
if(shipsRemaining < 0) {
gameOver();
}
} else if (gameState == MENU) {
drawMenu();
} else if (gameState == GAMEOVER) {
drawGameOver();
}
}

/**
* This is drawn while gamestate is MENU. Controls, enter to play, etc.
*/
public void drawMenu() {
canvas.textAlign(PApplet.CENTER);
canvas.text("BAD ASTEROIDS", canvas.width/2, 40);

canvas.text("By Zac Stewart", canvas.width/2, 60);
canvas.text("zgstewart", canvas.width/2, 75);

canvas.text("Controls:", canvas.width/2, 105);
canvas.text("W - Accelerate", canvas.width/2, 125);
canvas.text("A - Rotate Left", canvas.width/2, 140);
canvas.text("D - Rotate Right", canvas.width/2, 155);
canvas.text("Space - Fire Primary", canvas.width/2, 170);

canvas.text("Use the mouse to target and fire nukes", canvas.width/2, 200);

canvas.text("Press enter to begin", canvas.width/2, 230);

}

/**
* :( Score, go back to main menu.
*/
public void drawGameOver() {
canvas.textAlign(PApplet.CENTER);
canvas.text("GAME OVER", canvas.width/2, 40);

canvas.text("Score: " + score, canvas.width/2, 60);

canvas.text("Insert Coins to Continue", canvas.width/2, 80);
canvas.text("Or Press Enter", canvas.width/2, 95);
}

/**
* Displays game info like score, ships remaining, etc
*/
public void drawHud() {
canvas.stroke(255);
canvas.fill(255);
canvas.textAlign(PApplet.LEFT);
canvas.text("Level: " + level, 10, 20);
canvas.text("Ships: " + shipsRemaining, 10, 40);
canvas.text("Nukes: " + ship.getNukes(), 10, 60);
canvas.textAlign(PApplet.RIGHT);
canvas.text("Score: " + score, canvas.width-10, 20);
}

/**
* Lots going on in here. Main control center for game. Sets the ship
* var to the current Ship, removes dead objects, adds items in each objects
* createable array to the object list and clears said array. Monitors
* collisions via getCollision() and then enacts the appropriate collision.
*/
public void drawObjects() {
frame += 1;
li = spaceThings.listIterator();
createablesLi = createables.listIterator();
asteroidsRemaining = 0;
powerUps = 0;
target = null;
while(li.hasNext()) {
Object x = li.next();
if(x instanceof Ship) {
ship = (Ship) x;
shipsRemaining += ship.incShips;
ship.incShips = 0;
} else if (x instanceof Asteroid) {
Point mousePos = canvas.getMousePosition();
if (mousePos != null) {
if (((Asteroid) x).getBounds().contains(mousePos.x, mousePos.y)) {
target = (SpaceThing) x;
}
}
asteroidsRemaining += 1;
} else if (x instanceof PowerUp) {
powerUps += 1;
}
if(x instanceof SpaceThing) {
SpaceThing s = (SpaceThing) x;
if(s.createable != null) {
for(SpaceThing createable : s.createable) {
if(!(createable instanceof Bullet) || bulletsActive < 3) {
if(createable instanceof Bullet) {
bulletsActive += 1;
}
createablesLi.add(createable);
}
}
s.createable = null;
}
if (s.remove) {
if (s instanceof Ship) {
shipsRemaining += ((Ship) s).incShips;
((Ship) s).incShips = 0;
bulletsActive = 0;
} else if (s instanceof Bullet) {
bulletsActive -= 1;
} else if (s instanceof Asteroid) {
score += 10;
}
li.remove();
} else {
s.draw();

SpaceThing collidingObject = getCollision(s);
if(collidingObject instanceof SpaceThing){
s.collide(collidingObject);
if(s.createable != null) {
for(SpaceThing createable : s.createable) {
createablesLi.add(createable);
}
s.createable = null;
}
}
}
}
}

// add powerups
if(frame % 100 == 0 && powerUps <= 0) {
int coinToss = rand.nextInt(2);
int type = (coinToss == 1 ? SHIP : NUKE);
li.add(new PowerUp(canvas, type));
}

insertCreateables();
}

/**
* This loops through the createables and adds them to the object list.
* if the ship's spawn place is blocked by an asteroid, it waits until
* next iteration.
*/
public void insertCreateables() {
li = spaceThings.listIterator();
createablesLi = createables.listIterator();
while(createablesLi.hasNext()) {
Object createable = createablesLi.next();
if(createable instanceof Ship) {
if(collisionAtPoint(canvas.width/2,canvas.height/2) == null) {
li.add(createable);
createablesLi.remove();
}
} else {
li.add(createable);
createablesLi.remove();
}
}
}


/**
* this is used to determine if there is an object at an given x, y pair.
* @param xPoint
* @param yPoint
* @return
*/
public SpaceThing collisionAtPoint(int xPoint, int yPoint) {
Rectangle2D ghostShip = new Rectangle2D.Float(canvas.width/2, canvas.height/2, 20, 20);
ListIterator it = spaceThings.listIterator();
while(it.hasNext()) {
Object x = it.next();
if(x instanceof Asteroid) {
Asteroid object = (Asteroid) x;
if(object.collides(ghostShip)) {
return object;
}
}
}
return null;
}


/**
* Nested ListIterator loops to check for collisions between object and
* all other items in the object list. Returns whatever SpaceThing is
* colliding with object.
* @param object
* @return SpaceThing
*/
public SpaceThing getCollision(SpaceThing object) {
ListIterator it = spaceThings.listIterator();
while(it.hasNext()) {
Object x = it.next();
if(object instanceof Asteroid) {
if(((Asteroid) object).collides(x)) {
return (SpaceThing) x;
}
}
if(object instanceof PowerUp && x instanceof Ship) {
if(((PowerUp) object).collides((SpaceThing) x)) {
return (SpaceThing) x;
}
}
}
return null;
}


/**
* Key monitor. Maps keys and space keys to corresponding ship
* methods or game methods depending on gameState.
* @param type
* @param e
*/
public void control(String type, KeyEvent e) {
// System.out.println(e);
switch(gameState) {
case PLAYING: {
if(type.equals("keyDown")) {
switch(e.getKeyCode()) {
case 87:
ship.setAccelerating(true);
break;
case 65:
ship.setRotatingLeft(true);
break;
case 68:
ship.setRotatingRight(true);
break;
case 32:
ship.firePrimary();
break;
default:
break;
}
} else if(type.equals("keyUp")) {
switch(e.getKeyCode()) {
case 87:
ship.setAccelerating(false);
break;
case 65:
ship.setRotatingLeft(false);
break;
case 68:
ship.setRotatingRight(false);
break;
default:
break;
}
}
break;
}
case MENU: {
if(type.equals("keyDown")) {
switch(e.getKeyCode()) {
case 10:
newGame();
}
}
break;
}
case GAMEOVER: {
if(type.equals("keyDown")) {
switch(e.getKeyCode()) {
case 10:
initMenu();
}
}
break;
}
}
}


/**
* Mouse click monitor. Targets and fires nukes. Protip: click an asteroid
* and it tracks the asteroid. Click space and it just blows up on that point.
*/
public void mouseControl() {
switch(gameState) {
case PLAYING: {
if(target != null) {
ship.fireNuke(target);
} else {
ship.fireNuke();
}
}
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote