Spielprogrammierung mit Java
HomeAufgabenDruckenJava-Online

Space Invader

 

In diesem Beispiel werden fünf Klassen verwendet:
SpaceInvader, Alien, Bomb, Explosion und SpaceShip.
Für die Arbeit mit Online Editor müssen alle Klassen in der gleichen Datei gespeichert werden. Falls Sie mit einer lokalen Entwicklungsumgebung arbeiten, ist es vorteilhaft für jede Klasse eine neue Datei einzurichten.

Wir verwenden Sprite Bilder von der Webseite http://www.cokeandcode.com/tutorials. Wenn Sie diese durch eigenen Bilder ersetzen möchten, speichern Sie diese lokal im Verzeichnis userhome/gamegrid/sprites. Die Bilder werden beim Erstellen des Jar-Files automatisch mitverpackt.


Programmcode downloaden (SpaceInvader.zip)

 

Programmcode:

// 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();
  }
}