Planetalia - Java Training

Training and Consulting

Home

Tutorial - Writing a Space Invaders game in Java

  Previous Page - Status Current Page - 22 - Score   Next Page - Getting Killed
  Return to the index of free Java tutorials  

Score

(c) Alexander Hristov

This step is almost a trivial one. When we kill a monster, we want our score to increase by - let's say - 20 points. So we do this in the collision notification method of the Monster class. However, the score is an attribute of the player, and the Monster class does not have access to that variable. We might need access to the player instance in many other situations, for example for increasing the amount of cluster bombs, for decreasing the shields, for adding power-ups, whatever. So it's useful if we give other classes the ability to obtain a reference to the player object. The most appropriate place to do this is to add a new method to the Stage class:


1     /**
2      * Curso B?sico de desarrollo de Juegos en Java - Invaders
3      * 
4      * (c) 2004 Planetalia S.L. - Todos los derechos reservados. Prohibida su reproducci?n
5      * 
6      * http://www.planetalia.com
7      * 
8      */
9     package version22;
10    
11    import java.awt.image.ImageObserver;
12    
13    public interface Stage extends ImageObserver {
14      public static final int WIDTH=640;
15      public static final int HEIGHT=480;
16      public static final int PLAY_HEIGHT = 400; 
17      public static final int SPEED=10;
18      public SpriteCache getSpriteCache();
19      public void addActor(Actor a);
20 public Player getPlayer();
21 } 22

And of course we must implement this method in the Invaders class:


           . . .  
81      
82      public Player getPlayer() {
83        return player;
84      }
85      
           . . .  

Now anyone can obtain a reference to the player, and we use this to increase his score when a monster gets killed:


1     /**
2      * Curso B?sico de desarrollo de Juegos en Java - Invaders
3      * 
4      * (c) 2004 Planetalia S.L. - Todos los derechos reservados. Prohibida su reproducci?n
5      * 
6      * http://www.planetalia.com
7      * 
8      */
9     package version22;
10    
11    public class Monster extends Actor {
12      protected int vx;
13      
14      public Monster(Stage stage) {
15        super(stage);
16        setSpriteNames( new String[] {"bicho0.gif","bicho1.gif"});
17        setFrameSpeed(35);
18      }
19      
20      public void act() {
21        super.act();
22        x+=vx;
23        if (x < 0 || x > Stage.WIDTH)
24          vx = -vx;
25      }
26    
27      public int getVx() { return vx; }
28      public void setVx(int i) {vx = i; }
29      
30      public void collision(Actor a) {
31        if (a instanceof Bullet || a instanceof Bomb) {
32          remove();
33 stage.getPlayer().addScore(20);
34 } 35 } 36 } 37


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
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 - Status Current Page - 22 - Score   Next Page - Getting Killed
  Return to the index of free Java tutorials  

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