Top Description Fields Constructors Methods
org.lwjgl.examples.spaceinvaders

public Class Game

extends Object
Class Inheritance
Imports
java.util.ArrayList, org.lwjgl.LWJGLException, .Sys, org.lwjgl.input.Keyboard, .Mouse, org.lwjgl.opengl.Display, .DisplayMode

The main hook of our game. This class with both act as a manager for the display and central mediator for the game logic. Display management will consist of a loop that cycles round all entities in the game asking them to move and then drawing them in the appropriate place. With the help of an inner class it will also allow the player to control the main ship. As a mediator it will be informed when entities within our game detect events (e.g. alient killed, played died) and will take appropriate game actions.

NOTE:
This game is a LWJGLized implementation of the Space Invaders game by Kevin Glass. The original implementation is renderer agnostic and supports other OpenGL implementations as well as Java2D. This version has been made specific for LWJGL, and has added input control as well as sound (which the original doesn't, at the time of writing). You can find the original article here:
http://www.cokeandcode.com

Authors
Kevin Glass, Brian Matzon

Field Summary

Modifier and TypeField and Description
private int
alienCount

The number of aliens left on the screen

private ArrayList<Entity>
entities

The list of all the entities that exist in our game

private boolean
fireHasBeenReleased

True if the fire key has been released

private long
firingInterval

The interval between our players shot (ms)

private int
fps

The recorded fps

private boolean
fullscreen

Whether we're running in fullscreen mode

public static boolean
gameRunning

True if the game is currently "running", i.e. the game loop is looping

private Sprite
gotYou

The sprite containing the "You lose!" message

private int
height

The height of the game display area

private static boolean
isApplication

Is this an application or applet

private long
lastFire

The time at which last fired a shot

private long
lastFpsTime

The time since the last record of fps

private long
lastLoopTime

The time at which the last rendering looped started from the point of view of the game logic

private boolean
logicRequiredThisLoop

True if game logic needs to be applied this loop, normally as a result of a game event

private Sprite
message

The message to display which waiting for a key press

private int
mouseX

Mouse movement on x axis

private float
moveSpeed

The speed at which the player's ship should move (pixels/sec)

private Sprite
pressAnyKey

The sprite containing the "Press Any Key" message

private ArrayList<Entity>
removeList

The list of entities that need to be removed from the game this loop

private ShipEntity
ship

The entity representing the player

private int
shotIndex

Last shot index

private ShotEntity[]
shots

List of shots

private int
SOUND_HIT

ID of hit effect

private int
SOUND_LOOSE

ID of loose sound

private int
SOUND_SHOT

ID of shot effect

private int
SOUND_START

ID of start sound

private int
SOUND_WIN

ID of win sound

private SoundManager
soundManager

SoundManager to make sound with

private TextureLoader
textureLoader

The loader responsible for converting images into OpenGL textures

private static long
private boolean
waitingForKeyPress

True if we're holding up game play until a key has been pressed

private int
width

The width of the game display area

private String
WINDOW_TITLE

The normal title of the window

private Sprite
youWin

The sprite containing the "You win!" message

Constructor Summary

AccessConstructor and Description
public
Game(boolean fullscreen)

Construct our game and set it running.

Method Summary

Modifier and TypeMethod and Description
public void
public void
frameRendering()

Notification that a frame is being rendered.

private void
gameLoop()

Run the main game loop.

public Sprite

Returns:

A sprite that can be drawn onto the current graphics context.
getSprite
(String
A reference to the image to load
ref
)

Create or get a sprite which displays the image that is pointed to in the classpath by "ref"

public static long

Returns:

The high resolution time in milliseconds
getTime
()

Get the high resolution time in milliseconds

private boolean
hasInput(int direction)

private void
initEntities()

Initialise the starting state of the entities (ship and aliens).

public void
initialize()

Intialise the common elements for the game

public static void
main(String[]
The arguments that are passed into our game
argv
)

The entry point into the game.

public void
notifyAlienKilled()

Notification that an alien has been killed

public void
notifyDeath()

Notification that the player has died.

public void
notifyWin()

Notification that the player has won since all the aliens are dead.

public void
removeEntity(Entity
The entity that should be removed
entity
)

Remove an entity from the game.

private boolean
setDisplayMode()

Sets the display mode for fullscreen mode

public static void
sleep(long
The amount of time in milliseconds to sleep for
duration
)

Sleep for a fixed number of milliseconds.

private void
startGame()

Start a fresh game, this should clear out any old data and create a new set.

public void
tryToFire()

Attempt to fire a shot from the player.

public void
updateLogic()

Notification from a game entity that the logic of the game should be run at the next opportunity (normally as a result of some game event)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

alienCountback to summary
private int alienCount

The number of aliens left on the screen

entitiesback to summary
private ArrayList<Entity> entities

The list of all the entities that exist in our game

fireHasBeenReleasedback to summary
private boolean fireHasBeenReleased

True if the fire key has been released

firingIntervalback to summary
private long firingInterval

The interval between our players shot (ms)

fpsback to summary
private int fps

The recorded fps

fullscreenback to summary
private boolean fullscreen

Whether we're running in fullscreen mode

gameRunningback to summary
public static boolean gameRunning

True if the game is currently "running", i.e. the game loop is looping

gotYouback to summary
private Sprite gotYou

The sprite containing the "You lose!" message

heightback to summary
private int height

The height of the game display area

isApplicationback to summary
private static boolean isApplication

Is this an application or applet

lastFireback to summary
private long lastFire

The time at which last fired a shot

lastFpsTimeback to summary
private long lastFpsTime

The time since the last record of fps

lastLoopTimeback to summary
private long lastLoopTime

The time at which the last rendering looped started from the point of view of the game logic

logicRequiredThisLoopback to summary
private boolean logicRequiredThisLoop

True if game logic needs to be applied this loop, normally as a result of a game event

messageback to summary
private Sprite message

The message to display which waiting for a key press

mouseXback to summary
private int mouseX

Mouse movement on x axis

moveSpeedback to summary
private float moveSpeed

The speed at which the player's ship should move (pixels/sec)

pressAnyKeyback to summary
private Sprite pressAnyKey

The sprite containing the "Press Any Key" message

removeListback to summary
private ArrayList<Entity> removeList

The list of entities that need to be removed from the game this loop

shipback to summary
private ShipEntity ship

The entity representing the player

shotIndexback to summary
private int shotIndex

Last shot index

shotsback to summary
private ShotEntity[] shots

List of shots

SOUND_HITback to summary
private int SOUND_HIT

ID of hit effect

SOUND_LOOSEback to summary
private int SOUND_LOOSE

ID of loose sound

SOUND_SHOTback to summary
private int SOUND_SHOT

ID of shot effect

SOUND_STARTback to summary
private int SOUND_START

ID of start sound

SOUND_WINback to summary
private int SOUND_WIN

ID of win sound

soundManagerback to summary
private SoundManager soundManager

SoundManager to make sound with

textureLoaderback to summary
private TextureLoader textureLoader

The loader responsible for converting images into OpenGL textures

timerTicksPerSecondback to summary
private static long timerTicksPerSecond
waitingForKeyPressback to summary
private boolean waitingForKeyPress

True if we're holding up game play until a key has been pressed

widthback to summary
private int width

The width of the game display area

WINDOW_TITLEback to summary
private String WINDOW_TITLE

The normal title of the window

youWinback to summary
private Sprite youWin

The sprite containing the "You win!" message

Constructor Detail

Gameback to summary
public Game(boolean fullscreen)

Construct our game and set it running.

Method Detail

executeback to summary
public void execute()
frameRenderingback to summary
public void frameRendering()

Notification that a frame is being rendered. Responsible for running game logic and rendering the scene.

gameLoopback to summary
private void gameLoop()

Run the main game loop. This method keeps rendering the scene and requesting that the callback update its screen.

getSpriteback to summary
public Sprite getSprite(String ref)

Create or get a sprite which displays the image that is pointed to in the classpath by "ref"

Parameters
ref:String

A reference to the image to load

Returns:Sprite

A sprite that can be drawn onto the current graphics context.

getTimeback to summary
public static long getTime()

Get the high resolution time in milliseconds

Returns:long

The high resolution time in milliseconds

hasInputback to summary
private boolean hasInput(int direction)
initEntitiesback to summary
private void initEntities()

Initialise the starting state of the entities (ship and aliens). Each entitiy will be added to the overall list of entities in the game.

initializeback to summary
public void initialize()

Intialise the common elements for the game

mainback to summary
public static void main(String[] argv)

The entry point into the game. We'll simply create an instance of class which will start the display and game loop.

Parameters
argv:String[]

The arguments that are passed into our game

notifyAlienKilledback to summary
public void notifyAlienKilled()

Notification that an alien has been killed

notifyDeathback to summary
public void notifyDeath()

Notification that the player has died.

notifyWinback to summary
public void notifyWin()

Notification that the player has won since all the aliens are dead.

removeEntityback to summary
public void removeEntity(Entity entity)

Remove an entity from the game. The entity removed will no longer move or be drawn.

Parameters
entity:Entity

The entity that should be removed

setDisplayModeback to summary
private boolean setDisplayMode()

Sets the display mode for fullscreen mode

sleepback to summary
public static void sleep(long duration)

Sleep for a fixed number of milliseconds.

Parameters
duration:long

The amount of time in milliseconds to sleep for

startGameback to summary
private void startGame()

Start a fresh game, this should clear out any old data and create a new set.

tryToFireback to summary
public void tryToFire()

Attempt to fire a shot from the player. Its called "try" since we must first check that the player can fire at this point, i.e. has he/she waited long enough between shots

updateLogicback to summary
public void updateLogic()

Notification from a game entity that the logic of the game should be run at the next opportunity (normally as a result of some game event)