spaceinvaders
Class Game

java.lang.Object
  extended by spaceinvaders.Game
All Implemented Interfaces:
java.awt.event.MouseListener, java.awt.event.MouseMotionListener, java.lang.Runnable, java.util.EventListener

public class Game
extends java.lang.Object
implements java.lang.Runnable, java.awt.event.MouseListener, java.awt.event.MouseMotionListener

The Game object is the core of the application. It is a "Runnable" - the thread that runs it keeps the game going while another thread looks after the graphics. It is a MouseListener and MouseMotionListener - the mouse is used by the player to control the gun; the Game object picks up the mouse events. It is responsible for drawing the Gun and other details in the display apart from the invaders. It provides a number of utility functions for the invaders.


Constructor Summary
Game(int num)
          Constructor - loads sound files, performs other minor initializations.
 
Method Summary
 GamePanel getGamePanel()
          Used by invaders to get reference to display panel (e.g.
 void groundBurst(int damage)
          Record damage done to gun when invader hits ground
 int gunLocation()
          Returns gun's location (x-coordinate in game coordinates)
 void mouseClicked(java.awt.event.MouseEvent e)
          Record attempt to fire gun in this cycle of game
 void mouseDragged(java.awt.event.MouseEvent e)
          No-operation (Part of MouseMotionListener)
 void mouseEntered(java.awt.event.MouseEvent e)
          No-operation (Part of MouseListener)
 void mouseExited(java.awt.event.MouseEvent e)
          No-operation (Part of MouseListener)
 void mouseMoved(java.awt.event.MouseEvent e)
          Mouse moved, use x-coordinate to update gun position
 void mousePressed(java.awt.event.MouseEvent e)
          No-operation (Part of MouseListener)
 void mouseReleased(java.awt.event.MouseEvent e)
          No-operation (Part of MouseListener)
 void paint(java.awt.Graphics g)
          Paints gun etc, then tells each invader to paint itself
 void run()
          Define game behaviour: while gunners are alive check for movement/firing of gun allow invaders to attack
 int score()
          Returns current score for player.
 void setControlLink(ControlPanel cp)
          Set link to control panel.
 void setGamePanelLink(GamePanel gp)
          Set link to GamePanel.
 void start()
          Starts a new thread to run the game
 int status()
          Returns integer status score for player (score starts at 100 and is decreased every time an invader makes a successful attack)
 int X2X(int gameX)
          Converts game X-coordinate to screen X-coordinate (needed when an invader is trying to draw itself)
 int Y2Y(int gameY)
          Converts game Y-coordinate to screen Y-coordinate (needed when an invader is trying to draw itself)
 void zapGun()
          Record damage done by invader to gun through mechanisms like missle fire
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Game

public Game(int num)
Constructor - loads sound files, performs other minor initializations.

Parameters:
num - Defines number of invaders to use in game.
Method Detail

X2X

public int X2X(int gameX)
Converts game X-coordinate to screen X-coordinate (needed when an invader is trying to draw itself)

Parameters:
gameX - Game x- coordinate
Returns:
Screen x- coordinate

Y2Y

public int Y2Y(int gameY)
Converts game Y-coordinate to screen Y-coordinate (needed when an invader is trying to draw itself)

Parameters:
gameY - Game Y coordinate
Returns:
Screen y coordinate

setControlLink

public void setControlLink(ControlPanel cp)
Set link to control panel. When game in progress, some of display fields in control panel (like current score) will need to be updated.

Parameters:
cp - Reference to ControlPanel part of GUI

setGamePanelLink

public void setGamePanelLink(GamePanel gp)
Set link to GamePanel.

Parameters:
gp - GamePanel component in GUI.

score

public int score()
Returns current score for player.

Returns:
integer score

status

public int status()
Returns integer status score for player (score starts at 100 and is decreased every time an invader makes a successful attack)

Returns:
Current status

gunLocation

public int gunLocation()
Returns gun's location (x-coordinate in game coordinates)

Returns:
integer location

getGamePanel

public GamePanel getGamePanel()
Used by invaders to get reference to display panel (e.g. an invader that uses a GIF image would need a reference to the display panel so that it could use it as an ImageObserver when painting its GIF).

Returns:
Reference to display panel

paint

public void paint(java.awt.Graphics g)
Paints gun etc, then tells each invader to paint itself

Parameters:
g - Graphic object used for painting

start

public void start()
Starts a new thread to run the game


zapGun

public void zapGun()
Record damage done by invader to gun through mechanisms like missle fire


groundBurst

public void groundBurst(int damage)
Record damage done to gun when invader hits ground

Parameters:
damage - Damage done by this invader

run

public void run()
Define game behaviour: while gunners are alive check for movement/firing of gun allow invaders to attack

Specified by:
run in interface java.lang.Runnable

mouseClicked

public void mouseClicked(java.awt.event.MouseEvent e)
Record attempt to fire gun in this cycle of game

Specified by:
mouseClicked in interface java.awt.event.MouseListener
Parameters:
e - Mouse event marking click

mouseEntered

public void mouseEntered(java.awt.event.MouseEvent e)
No-operation (Part of MouseListener)

Specified by:
mouseEntered in interface java.awt.event.MouseListener
Parameters:
e - Mouse event

mouseExited

public void mouseExited(java.awt.event.MouseEvent e)
No-operation (Part of MouseListener)

Specified by:
mouseExited in interface java.awt.event.MouseListener
Parameters:
e - Mouse event

mousePressed

public void mousePressed(java.awt.event.MouseEvent e)
No-operation (Part of MouseListener)

Specified by:
mousePressed in interface java.awt.event.MouseListener
Parameters:
e - Mouse event

mouseReleased

public void mouseReleased(java.awt.event.MouseEvent e)
No-operation (Part of MouseListener)

Specified by:
mouseReleased in interface java.awt.event.MouseListener
Parameters:
e - Mouse event

mouseDragged

public void mouseDragged(java.awt.event.MouseEvent e)
No-operation (Part of MouseMotionListener)

Specified by:
mouseDragged in interface java.awt.event.MouseMotionListener
Parameters:
e - Mouse event

mouseMoved

public void mouseMoved(java.awt.event.MouseEvent e)
Mouse moved, use x-coordinate to update gun position

Specified by:
mouseMoved in interface java.awt.event.MouseMotionListener
Parameters:
e - Mouse event (with x,y coords in display coordinates)