package org.newdawn.spaceinvaders;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferStrategy;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* 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.
*
* @author Kevin Glass
*/
public class Game extends Canvas {
/** The stragey that allows us to use accelerate page flipping */
private BufferStrategy strategy;
/** True if the game is currently "running", i.e. the game loop is looping */
private boolean gameRunning = true;
/** The list of all the entities that exist in our game */
private ArrayList entities = new ArrayList();
/** The list of entities that need to be removed from the game this loop */
private ArrayList removeList = new ArrayList();
/** The entity representing the player */
private Entity ship;
/** The speed at which the player's ship should move (pixels/sec) */
private double moveSpeed = 300;
/** The time at which last fired a shot */
private long lastFire = 0;
/** The interval between our players shot (ms) */
private long firingInterval = 500;
/** The number of aliens left on the screen */
private int alienCount;
/** The message to display which waiting for a key press */
private String message = "";
/** True if we're holding up game play until a key has been pressed */
private boolean waitingForKeyPress = true;
/** True if the left cursor key is currently pressed */
private boolean leftPressed = false;
/** True if the right cursor key is currently pressed */
private boolean rightPressed = false;
/** True if we are firing */
private boolean firePressed = false;
/** True if game logic needs to be applied this loop, normally as a result of a game event */
private boolean logicRequiredThisLoop = false;
/** The last time at which we recorded the frame rate */
private long lastFpsTime;
/** The current number of frames recorded */
private int fps;
/** The normal title of the game window */
private String windowTitle = "Space Invaders 102";
/** The game window that we'll update with the frame count */
private JFrame container;
/**
* Construct our game and set it running.
*/
public Game() {
container = new JFrame("Space Invaders 102");
JPanel panel = (JPanel) container.getContentPane();
panel.setPreferredSize(new Dimension(800,600));
panel.setLayout(null);
setBounds(0,0,800,600);
panel.add(this);
setIgnoreRepaint(true);
container.pack();
container.setResizable(false);
container.setVisible(true);
container.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
addKeyListener(new KeyInputHandler());
requestFocus();
createBufferStrategy(2);
strategy = getBufferStrategy();
initEntities();
}
/**
* Start a fresh game, this should clear out any old data and
* create a new set.
*/
private void startGame() {
entities.clear();
initEntities();
leftPressed = false;
rightPressed = false;
firePressed = false;
}
/**
* Initialise the starting state of the entities (ship and aliens). Each
* entitiy will be added to the overall list of entities in the game.
*/
private void initEntities() {
ship = new ShipEntity(this,"sprites/ship.gif",370,550);
entities.add(ship);
alienCount = 0;
for (int row=0;row<5;row++) {
for (int x=0;x<12;x++) {
Entity alien = new AlienEntity(this,100+(x*50),(50)+row*30);
entities.add(alien);
alienCount++;
}
}
}
/**
* 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)
*/
public void updateLogic() {
logicRequiredThisLoop = true;
}
/**
* Remove an entity from the game. The entity removed will
* no longer move or be drawn.
*
* @param entity The entity that should be removed
*/
public void removeEntity(Entity entity) {
removeList.add(entity);
}
/**
* Notification that the player has died.
*/
public void notifyDeath() {
message = "Oh no! They got you, try again?";
waitingForKeyPress = true;
}
/**
* Notification that the player has won since all the aliens
* are dead.
*/
public void notifyWin() {
message = "Well done! You Win!";
waitingForKeyPress = true;
}
/**
* Notification that an alien has been killed
*/
public void notifyAlienKilled() {
alienCount--;
if (alienCount == 0) {
notifyWin();
}
for (int i=0;i<entities.size();i++) {
Entity entity = (Entity) entities.get(i);
if (entity instanceof AlienEntity) {
entity.setHorizontalMovement(entity.getHorizontalMovement() * 1.02);
}
}
}
/**
* 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
*/
public void tryToFire() {
if (System.currentTimeMillis() - lastFire < firingInterval) {
return;
}
lastFire = System.currentTimeMillis();
ShotEntity shot = new ShotEntity(this,"sprites/shot.gif",ship.getX()+10,ship.getY()-30);
entities.add(shot);
}
/**
* The main game loop. This loop is running during all game
* play as is responsible for the following activities:
* <p>
* - Working out the speed of the game loop to update moves
* - Moving the game entities
* - Drawing the screen contents (entities, text)
* - Updating game events
* - Checking Input
* <p>
*/
public void gameLoop() {
long lastLoopTime = SystemTimer.getTime();
while (gameRunning) {
long delta = SystemTimer.getTime() - lastLoopTime;
lastLoopTime = SystemTimer.getTime();
lastFpsTime += delta;
fps++;
if (lastFpsTime >= 1000) {
container.setTitle(windowTitle+" (FPS: "+fps+")");
lastFpsTime = 0;
fps = 0;
}
Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
g.setColor(Color.black);
g.fillRect(0,0,800,600);
if (!waitingForKeyPress) {
for (int i=0;i<entities.size();i++) {
Entity entity = (Entity) entities.get(i);
entity.move(delta);
}
}
for (int i=0;i<entities.size();i++) {
Entity entity = (Entity) entities.get(i);
entity.draw(g);
}
for (int p=0;p<entities.size();p++) {
for (int s=p+1;s<entities.size();s++) {
Entity me = (Entity) entities.get(p);
Entity him = (Entity) entities.get(s);
if (me.collidesWith(him)) {
me.collidedWith(him);
him.collidedWith(me);
}
}
}
entities.removeAll(removeList);
removeList.clear();
if (logicRequiredThisLoop) {
for (int i=0;i<entities.size();i++) {
Entity entity = (Entity) entities.get(i);
entity.doLogic();
}
logicRequiredThisLoop = false;
}
// if we're waiting for an "any key" press then draw the
if (waitingForKeyPress) {
g.setColor(Color.white);
g.drawString(message,(800-g.getFontMetrics().stringWidth(message))/2,250);
g.drawString("Press any key",(800-g.getFontMetrics().stringWidth("Press any key"))/2,300);
}
g.dispose();
strategy.show();
ship.setHorizontalMovement(0);
if ((leftPressed) && (!rightPressed)) {
ship.setHorizontalMovement(-moveSpeed);
} else if ((rightPressed) && (!leftPressed)) {
ship.setHorizontalMovement(moveSpeed);
}
if (firePressed) {
tryToFire();
}
SystemTimer.sleep(lastLoopTime+10-SystemTimer.getTime());
}
}
/**
* A class to handle keyboard input from the user. The class
* handles both dynamic input during game play, i.e. left/right
* and shoot, and more static type input (i.e. press any key to
* continue)
*
* This has been implemented as an inner class more through
* habbit then anything else. Its perfectly normal to implement
* this as seperate class if slight less convienient.
*
* @author Kevin Glass
*/
private class KeyInputHandler extends KeyAdapter {
/** The number of key presses we've had while waiting for an "any key" press */
private int pressCount = 1;
/**
* Notification from AWT that a key has been pressed. Note that
* a key being pressed is equal to being pushed down but *NOT*
* released. Thats where keyTyped() comes in.
*
* @param e The details of the key that was pressed
*/
public void keyPressed(KeyEvent e) {
// if we're waiting for an "any key" typed then we don't
// want to do anything with just a "press"
if (waitingForKeyPress) {
return;
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
leftPressed = true;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
rightPressed = true;
}
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
firePressed = true;
}
}
/**
* Notification from AWT that a key has been released.
*
* @param e The details of the key that was released
*/
public void keyReleased(KeyEvent e) {
// if we're waiting for an "any key" typed then we don't
// want to do anything with just a "released"
if (waitingForKeyPress) {
return;
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
leftPressed = false;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
rightPressed = false;
}
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
firePressed = false;
}
}
/**
* Notification from AWT that a key has been typed. Note that
* typing a key means to both press and then release it.
*
* @param e The details of the key that was typed.
*/
public void keyTyped(KeyEvent e) {
// if we're waiting for a "any key" type then
// the shoot or move keys, hence the use of the "pressCount"
if (waitingForKeyPress) {
if (pressCount == 1) {
waitingForKeyPress = false;
startGame();
pressCount = 0;
} else {
pressCount++;
}
}
if (e.getKeyChar() == 27) {
System.exit(0);
}
}
}
/**
* The entry point into the game. We'll simply create an
* instance of class which will start the display and game
* loop.
*
* @param argv The arguments that are passed into our game
*/
public static void main(String argv[]) {
Game g = new Game();
g.gameLoop();
}
}
Total 471 Lines of Code.
|
Source code formatted using showsrc by William Denniss
|