GameGrid: Game programming with Java

Research project PHBern  
HomePrintJava-Online

Space Invader

 

This example uses five different classes:
SpaceInvader, Alien, Bomb, Explosion and SpaceShip.
Working with the Online Editor, all five classes need to be written and saved inside the same file (same editor window). If you are working with a local development environment, it is recommended to work with five different files, saved inside the same source directory.

In this example we work with Sprites from the webpage http://www.cokeandcode.com/tutorials. If you want to replace them with your own Sprites, please save them inside the local directory userhome/gamegrid/sprites. The pictures will be added automatically to the jar-file.

Run this example

Edit this example in the Online-Editor

 

Program code:

// SpaceInvader.java

import ch.aplu.jgamegrid.*;
import java.awt.event.KeyEvent;
import java.awt.*;

public class SpaceInvader extends GameGrid implements GGKeyListener
{
  private final int nbRows = 3;
  private final int nbCols = 11;

  public SpaceInvader()
  {
    super(200, 100, 5, false);
    setSimulationPeriod(50);
    for (int = 0; i < nbRows; i++)
    {
      for (int = 0; k < nbCols; k++)
      {
        Alien alien = new Alien();
        addActor(alien, new Location(100 - * nbCols + 10 * k, 10 + 10 * i));
      }
    }
    SpaceShip ss = new SpaceShip();
    addActor(ss, new Location(100, 90));
    addKeyRepeatListener(ss);
    setKeyRepeatPeriod(100);
    addKeyListener(this);
    getBg().setFont(new Font("SansSerif"Font.PLAIN, 12));
    getBg().drawText("Use <- -> to move, spacebar to shoot"new Point(400, 330));
    getBg().drawText("Press any key to start..."new Point(400, 350));
    show();
  }

  public boolean keyPressed(KeyEvent evt)
  {
    if (!isRunning())
    {
      setBgColor(java.awt.Color.black);  // Erase text
      doRun();
    }
    return false;  // Do not consume key
  }

  public boolean keyReleased(KeyEvent evt)
  {
    return false;
  }

  public static void main(String[] args)
  {
    new SpaceInvader();
  }
}

// -----------class Alien ---------------
class Alien extends Actor
{
  private final int maxNbSteps = 16;
  private int nbSteps;

  public Alien()
  {
    super("sprites/alien.gif");
    setSlowDown(7);
  }

  public void reset()
  {
    nbSteps = 7;
  }

  public void act()
  {
    if (nbSteps < maxNbSteps)
    {
      move();
      nbSteps++;
    }
    else
    {
      nbSteps = 0;
      int angle;
      if (getDirection() == 0)
        angle = 90;
      else
        angle = -90;
      turn(angle);
      move();
      turn(angle);
    }
    if (getLocation().> 90)
      removeSelf();
  }
}

// ---------------- SpaceShip ---------------

class SpaceShip extends Actor implements GGKeyRepeatListener
{
  private int nbShots = 0;
  private int isGameOver = 0;

  public SpaceShip()
  {
    super("sprites/spaceship.gif");
  }

  public void act()
  {
    if (isGameOver > 0)
      return;
    GameGrid gg = gameGrid;
    Location location = getLocation();
    if (gg.getNumberOfActorsAt(location, Alien.class) > 0)
    {
      gg.removeAllActors();
      gg.addActor(new Actor("sprites/explosion2.gif"), location);
      isGameOver = 1;
      return;
    }
    if (gg.getNumberOfActors(Alien.class) == 0)
    {
      gg.getBg().drawText("Number of shots: " + nbShots, new Point(10, 30));
      gg.getBg().drawText("Game constructed with JGameGrid (www.aplu.ch)"new Point(10, 50));
      gg.addActor(new Actor("sprites/you_win.gif")new Location(100,60));
      isGameOver = 2;
      return;
    }
  }

  public void keyRepeated(int keyCode)
  {
    if (isGameOver == 1)
      return;

    Location next = null;
    switch (keyCode)
    {
      case KeyEvent.VK_LEFT:
        next = getLocation().getAdjacentLocation(Location.WEST);
        moveTo(next);
        break;

      case KeyEvent.VK_RIGHT:
        next = getLocation().getAdjacentLocation(Location.EAST);
        moveTo(next);
        break;

      case KeyEvent.VK_SPACE:
        Bomb bomb = new Bomb();
        gameGrid.addActor(bomb, getLocation());
        nbShots++;
        break;
    }
  }

  private void moveTo(Location location)
  {
    if (location.x > 10 && location.x < 190)
      setLocation(location);
  }
}

// -------------class Bomb --------------------
class Bomb extends Actor
{
  public Bomb()
  {
    super("sprites/bomb.gif");
  }

  public void reset()
  {
    setDirection(Location.NORTH);
  }

  public void act()
  {
    // Acts independently searching a possible target and bring it to explosion
    move();
    if (gameGrid.removeActorsAt(getLocation(), Alien.class) != 0)
    {
      Explosion explosion = new Explosion();
      gameGrid.addActor(explosion, getLocation());
      removeSelf();
      return;
    }
    if (getLocation().y < 5)
      removeSelf();
  }
}

// ---------- class Explosion ----------------

class Explosion extends Actor
{
  public Explosion()
  {
    super("sprites/explosion1.gif");
    setSlowDown(3);
  }

  public void act()
  {
    removeSelf();
  }
}