Game-Apps für Smartphones und Tablets |
|
Bern University oh Teacher Education |
Das Ziel des Spiels ist es, alles Aliens zu treffen, bevor sie unten gelandet sind. Das Raumschiff kann mit der Volumentasten bewegt werden, mit der Menü-Taste kann man schiessen. Mit Back-Taste kann das Spiel wiederholt werden. Beim Emulator verwendet man anstelle der Lautstärke-Tasten die Taste Ctrl+F5 und Ctrl+F6. Das Spiel ist im Querformat programmiert. Der Emulator lässt sich mit den Tasten Ctrl+F11 bzw. Ctrl+F12 in den Querformati umdrehen un wieder zurücksetzte.
|
![]() |
|
|
Programmcode:
// SpaceInvader.java package app.spaceinvader; import ch.aplu.android.*; import android.graphics.*; public class SpaceInvader extends GameGrid { private final static int nbRows = 3; private final static int nbCols = 9; protected final static int dx = 6; protected final static int nbHorzCells = 81; protected final static int nbVertCells = 51; private final static int cellSize = 5; public SpaceInvader() { super(nbHorzCells, nbVertCells, cellSize, Color.TRANSPARENT); setScreenOrientation(LANDSCAPE); } public void main() { setSimulationPeriod(50); getBg().drawText("Button actions:", new Point(30, 10)); getBg().drawText("VOLUME to move spaceship", new Point(30, 40)); getBg().drawText("MENU to shoot", new Point(30, 60)); getBg().drawText("BACK to restart", new Point(30, 80)); getBg().drawText("Please wait for game start...", new Point(60, 120)); refresh(); delay(10000); init(); } protected void init() { getBg().clear(Color.BLACK); removeAllActors(); for (int i = 0; i < nbRows; i++) { for (int k = 0; k < nbCols; k++) { Alien alien = new Alien(); addActor(alien, new Location(nbHorzCells / 2 - (nbCols / 2) * dx + k * dx, 10 + 5 * i)); } } SpaceShip ss = new SpaceShip(this); addNavigationListener(ss); addActor(ss, new Location(nbHorzCells / 2, 45)); doRun(); } } // --------------class Alien ----------- class Alien extends Actor { private final int maxNbSteps = 16; private int nbSteps; public Alien() { super("alien"); setSlowDown(7); } public void reset() { nbSteps = 7; } public void act() { if (nbSteps < maxNbSteps) { move(SpaceInvader.dx / 2); nbSteps++; } else { nbSteps = 0; int angle; if (getDirection() == 0) angle = 90; else angle = -90; turn(angle); move(); turn(angle); } if (getLocation().y > 90) removeSelf(); } } // --------------class Ship ------------- class SpaceShip extends Actor implements GGNavigationListener { private int nbShots = 0; private SpaceInvader app; public SpaceShip(SpaceInvader app) { super("spaceship"); this.app = app; } public void act() { GameGrid gg = gameGrid; Location location = getLocation(); if (gg.getNumberOfActorsAt(location, Alien.class) > 0) // Spaceship hit { gg.removeAllActors(); gg.addActor(new Actor("explosion2"), location); gg.getBg().drawText("Press 'BACK' to play again", new Point(30, 50)); gg.doPause(); return; } if (gg.getNumberOfActors(Alien.class) == 0) // All aliens dead { gg.removeAllActors(); gg.getBg().drawText("Game over. You survive.", new Point(30, 10)); gg.getBg().drawText("Number of shots: " + nbShots, new Point(30, 30)); gg.getBg().drawText("Press 'BACK' to play again", new Point(30, 50)); gg.doPause(); } } public void navigationEvent(GGNavigationEvent event) { switch (event) { case VOLUME_INCREASE_REPEAT: setDirection(Location.WEST); if (getX() > 5) move(SpaceInvader.dx); break; case VOLUME_DECREASE_REPEAT: setDirection(Location.EAST); if (getX() < SpaceInvader.nbHorzCells - 5) move(SpaceInvader.dx); break; case MENU_DOWN: Bomb bomb = new Bomb(); gameGrid.addActor(bomb, getLocation()); nbShots++; break; case BACK_DOWN: app.doPause(); app.init(); break; } } } // --------- class Bomb ------------------------- class Bomb extends Actor { public Bomb() { super("bomb"); } 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("explosion1"); setSlowDown(3); } public void act() { removeSelf(); } } |