Planetalia - Java Training

Training and Consulting

Home

Tutorial - Writing a Space Invaders game in Java

  Previous Page - Shots Current Page - 19 - More shooting - Cluster bombs   Next Page - Collision detection
  Return to the index of free Java tutorials  

More shooting - Cluster bombs

(c) Alexander Hristov

We'll now see firsthand the benefits of using an OOP approach in games, by adding a new type of shot - a "cluster bomb". The player will have a limited number of these bombs (for example, initially 5) and each time he shoots one, eight fireballs will appear and dart in eight different directions. We will use the following graphics, which in reality come from a single image rotated, flipped and mirrored as necessary:

Java
bombUL.gif
Java
bombU.gif
Java
bombUR.gif
Java
bombL.gif
  Java
bombR.gif
Java
bombDL.gif
Java
bombD.gif
Java
bombDR.gif

The fireball has its own class, called Bomb, with the following source:


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 version19;
10    
11    
12    public class Bomb extends Actor {
13      public static final int UP_LEFT = 0;
14      public static final int UP = 1;
15      public static final int UP_RIGHT = 2;
16      public static final int LEFT = 3;
17      public static final int RIGHT = 4;
18      public static final int DOWN_LEFT = 5;
19      public static final int DOWN = 6;
20      public static final int DOWN_RIGHT = 7;
21      
22      protected static final int BOMB_SPEED = 5; 
23      protected int vx;
24      protected int vy;
25      
26      public Bomb(Stage stage, int heading, int x, int y) {
27        super(stage);
28        this.x = x;
29        this.y = y;
30        String sprite ="";
31        switch (heading) {
32          case UP_LEFT : vx = -BOMB_SPEED; vy = -BOMB_SPEED; sprite="bombUL.gif";break;
33          case UP : vx = 0; vy = -BOMB_SPEED; sprite="bombU.gif";break;
34          case UP_RIGHT: vx = BOMB_SPEED; vy = -BOMB_SPEED; sprite="bombUR.gif";break;
35          case LEFT : vx = -BOMB_SPEED; vy = 0; sprite = "bombL.gif";break;
36          case RIGHT : vx = BOMB_SPEED; vy = 0; sprite = "bombR.gif";break;
37          case DOWN_LEFT : vx = -BOMB_SPEED; vy = BOMB_SPEED; sprite="bombDL.gif";break;
38          case DOWN : vx = 0; vy = BOMB_SPEED; sprite = "bombD.gif";break;
39          case DOWN_RIGHT : vx = BOMB_SPEED; vy = BOMB_SPEED; sprite = "bombDR.gif";break;
40        }   
41        setSpriteNames( new String[] {sprite});
42      }
43      
44      public void act() {
45        super.act();
46        y+=vy;
47        x+=vx;
48        if (y < 0 || y > Stage.HEIGHT || x < 0 || x > Stage.WIDTH)
49          remove();
50      }
51    }

What's new here is that the visual aspect of the actor changes depending on the direction of movement. Also we can see that the act() method just moves the bomb and erases it if it reaches the limits of the screen

The Player class must be also modified in order to handle a new key for this type of shot (we'll use the "B" key for cluster bombs), and we must also handle the number of bombs we have left:


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 version19;
10    
11    import java.awt.event.KeyEvent;
12    
13    public class Player extends Actor {
14      protected static final int PLAYER_SPEED = 4;
15      protected int vx;
16      protected int vy;
17      private boolean up,down,left,right;
18 private int clusterBombs;
19 20 21 public Player(Stage stage) { 22 super(stage); 23 setSpriteNames( new String[] {"nave.gif"});
24 clusterBombs = 5;
25 } 26 27 public void act() { 28 super.act(); 29 x+=vx; 30 y+=vy; 31 if (x < 0 || x > Stage.WIDTH) 32 vx = -vx; 33 if (y < 0 || y > Stage.HEIGHT) 34 vy = -vy; 35 } 36 37 public int getVx() { return vx; } 38 public void setVx(int i) {vx = i; } 39 public int getVy() { return vy; } 40 public void setVy(int i) {vy = i; } 41 42 43 protected void updateSpeed() { 44 vx=0;vy=0; 45 if (down) vy = PLAYER_SPEED; 46 if (up) vy = -PLAYER_SPEED; 47 if (left) vx = -PLAYER_SPEED; 48 if (right) vx = PLAYER_SPEED; 49 } 50 51 public void keyReleased(KeyEvent e) { 52 switch (e.getKeyCode()) { 53 case KeyEvent.VK_DOWN : down = false;break; 54 case KeyEvent.VK_UP : up = false; break; 55 case KeyEvent.VK_LEFT : left = false; break; 56 case KeyEvent.VK_RIGHT : right = false;break; 57 } 58 updateSpeed(); 59 } 60 61 public void keyPressed(KeyEvent e) { 62 switch (e.getKeyCode()) { 63 case KeyEvent.VK_UP : up = true; break; 64 case KeyEvent.VK_LEFT : left = true; break; 65 case KeyEvent.VK_RIGHT : right = true; break; 66 case KeyEvent.VK_DOWN : down = true;break; 67 case KeyEvent.VK_SPACE : fire(); break;
68 case KeyEvent.VK_B : fireCluster(); break;
69 } 70 updateSpeed(); 71 } 72 73 public void fire() { 74 Bullet b = new Bullet(stage); 75 b.setX(x); 76 b.setY(y - b.getHeight()); 77 stage.addActor(b); 78 } 79
80 public void fireCluster() { 81 if (clusterBombs == 0) 82 return; 83 84 clusterBombs--; 85 stage.addActor( new Bomb(stage, Bomb.UP_LEFT, x,y)); 86 stage.addActor( new Bomb(stage, Bomb.UP,x,y)); 87 stage.addActor( new Bomb(stage, Bomb.UP_RIGHT,x,y)); 88 stage.addActor( new Bomb(stage, Bomb.LEFT,x,y)); 89 stage.addActor( new Bomb(stage, Bomb.RIGHT,x,y)); 90 stage.addActor( new Bomb(stage, Bomb.DOWN_LEFT,x,y)); 91 stage.addActor( new Bomb(stage, Bomb.DOWN,x,y)); 92 stage.addActor( new Bomb(stage, Bomb.DOWN_RIGHT,x,y)); 93 }
94 95 } 96

Curso de Space Invaders en Java



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 - Shots Current Page - 19 - More shooting - Cluster bombs   Next Page - Collision detection
  Return to the index of free Java tutorials  

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