Planetalia - Java Training

Training and Consulting

Home

Tutorial - Writing a Space Invaders game in Java

  Previous Page - A Scrolling playfield Current Page - 26 - Monster respawning   Next Page - Simple sound
  Return to the index of free Java tutorials  

Monster respawning

(c) Alexander Hristov

Currently when we kill a monster nothing special happens, and the monsters are quickly exterminated. We can take several approaches here:

  • Spawn new monsters as older ones are killed
  • Change the current "level" switching to different backgrounds and different critters
  • Making new monsters appear periodically regardless of how many are killed

To illustrate our point, we'll use the first approach here - we'll spawn a new monster for each monster killed. This is as easy as adding the following to our Monster class:


           . . .  
33      public void collision(Actor a) {
34        if (a instanceof Bullet || a instanceof Bomb) {
35          remove();
36 spawn();
37 stage.getPlayer().addScore(20); 38 } 39 } 40
41 public void spawn() { 42 Monster m = new Monster(stage); 43 m.setX( (int)(Math.random()*Stage.WIDTH) ); 44 m.setY( (int)(Math.random()*Stage.PLAY_HEIGHT/2) ); 45 m.setVx( (int)(Math.random()*20-10) ); 46 stage.addActor(m); 47 }
48 49 public void fire() { . . .


Do you want to be notified when new tutorials or lessons are published? Press here


Full list of Java source files for this step

Actor.java Bomb.java Bullet.java Invaders.java
Laser.java Monster.java Player.java SpriteCache.java
Stage.java      

Full list of resources

bicho.gif bicho0.gif bicho1.gif bicho2.gif
bombD.gif bombDL.gif bombDR.gif bombL.gif
bombR.gif bombU.gif bombUL.gif bombUR.gif
disparo.gif disparo0.gif disparo1.gif disparo2.gif
explosion.wav misil.gif missile.wav musica.wav
nave.gif oceano.gif photon.wav test.gif
Thumbs.db      

  Previous Page - A Scrolling playfield Current Page - 26 - Monster respawning   Next Page - Simple sound
  Return to the index of free Java tutorials  

(c) 2004 Planetalia S.L. All rights reserved. Unauthorized reproduction and/or mirroring is not permitted