Training and Consulting | ||
| Home |
| Previous Page - Collision detection | Current Page - 21 - Status | Next Page - Score |
| Return to the index of free Java tutorials |
StatusNow that we are able to kill the critters, it would be useful to have some kind of score to motivate us to go on. In this step we will add a status bar in the lower end of the screen. Our status bar will consist of the following:
Let's start first with the score and the shield meter. These are attributes that belong to the player class, so we add them there, together with their accessor methods. I also found that it was confusing and annoying for the player ship to "bounce" off the walls of the screen (and this is a genuine mistake, not a "planned" one as the previous,really!, so we've also changed the behaviour of the act() method. Now, if the player tries to go off the screen, it simply stays there.
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 version21; 10 11 import java.awt.event.KeyEvent; 12 13 public class Player extends Actor { 15 public static final int MAX_BOMBS = 5; 16 protected static final int PLAYER_SPEED = 4; 17 protected int vx; 18 protected int vy; 19 private boolean up,down,left,right; 20 private int clusterBombs; 23 24 25 public Player(Stage stage) { 26 super(stage); 27 setSpriteNames( new String[] {"nave.gif"}); 28 clusterBombs = MAX_BOMBS; 30 score = 0; 31 } 32 Now we must change the size of the playing area, since we must reserve a part of the screen for drawing the current status. So we add a constant to the Stage class called PLAY_HEIGHT that contains the height of the playing area:
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 version21; 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; 17 public static final int SPEED=10; 18 public SpriteCache getSpriteCache(); 19 public void addActor(Actor a); 20 } 21 The biggest change will take place in the Invaders class, where we must add the four methods for drawing each part of the status:
1 package version21; 2 /** 3 * Curso B?sico de desarrollo de Juegos en Java - Invaders 4 * 5 * (c) 2004 Planetalia S.L. - Todos los derechos reservados. Prohibida su reproducci?n 6 * 7 * http://www.planetalia.com 8 * 9 */ 10 11 12 import java.awt.Canvas; 13 import java.awt.Color; 14 import java.awt.Dimension; 15 import java.awt.Font; 16 import java.awt.Graphics2D; 17 import java.awt.Rectangle; 18 import java.awt.event.KeyEvent; 19 import java.awt.event.KeyListener; 20 import java.awt.event.WindowAdapter; 21 import java.awt.event.WindowEvent; 22 import java.awt.image.BufferStrategy; 23 import java.awt.image.BufferedImage; 24 import java.util.ArrayList; 25 26 import javax.swing.JFrame; 27 import javax.swing.JPanel; 28 29 public class Invaders extends Canvas implements Stage, KeyListener { 30 31 private BufferStrategy strategy; 32 private long usedTime; 33 34 private SpriteCache spriteCache; 35 private ArrayList actors; 36 private Player player; 37 38 public Invaders() { 39 spriteCache = new SpriteCache(); 40 41 42 JFrame ventana = new JFrame("Invaders"); 43 JPanel panel = (JPanel)ventana.getContentPane(); 44 setBounds(0,0,Stage.WIDTH,Stage.HEIGHT); 45 panel.setPreferredSize(new Dimension(Stage.WIDTH,Stage.HEIGHT)); 46 panel.setLayout(null); 47 panel.add(this); 48 ventana.setBounds(0,0,Stage.WIDTH,Stage.HEIGHT); 49 ventana.setVisible(true); 50 ventana.addWindowListener( new WindowAdapter() { 51 public void windowClosing(WindowEvent e) { 52 System.exit(0); 53 } 54 }); 55 ventana.setResizable(false); 56 createBufferStrategy(2); 57 strategy = getBufferStrategy(); 58 requestFocus(); 59 addKeyListener(this); 60 } 61 62 public void initWorld() { 63 actors = new ArrayList(); 64 for (int i = 0; i < 10; i++){ 65 Monster m = new Monster(this); 66 m.setX( (int)(Math.random()*Stage.WIDTH) ); 67 m.setY( i*20 ); 68 m.setVx( (int)(Math.random()*20-10) ); 69 70 actors.add(m); 71 } 72 73 player = new Player(this); 74 player.setX(Stage.WIDTH/2); 75 player.setY(Stage.PLAY_HEIGHT - 2*player.getHeight()); 76 } 77 78 public void addActor(Actor a) { 79 actors.add(a); 80 } 81 82 public void updateWorld() { 83 int i = 0; 84 while (i < actors.size()) { 85 Actor m = (Actor)actors.get(i); 86 if (m.isMarkedForRemoval()) { 87 actors.remove(i); 88 } else { 89 m.act(); 90 i++; 91 } 92 } 93 player.act(); 94 } 95 96 public void checkCollisions() { 97 Rectangle playerBounds = player.getBounds(); 98 for (int i = 0; i < actors.size(); i++) { 99 Actor a1 = (Actor)actors.get(i); 100 Rectangle r1 = a1.getBounds(); 101 if (r1.intersects(playerBounds)) { 102 player.collision(a1); 103 a1.collision(player); 104 } 105 for (int j = i+1; j < actors.size(); j++) { 106 Actor a2 = (Actor)actors.get(j); 107 Rectangle r2 = a2.getBounds(); 108 if (r1.intersects(r2)) { 109 a1.collision(a2); 110 a2.collision(a1); 111 } 112 } 113 } 114 } 115 The result is:
|
||||||||||||||||||||
Do you want to be notified when new tutorials or lessons are published? Press here
| Actor.java | Bomb.java | Bullet.java | Invaders.java |
| Monster.java | Player.java | SpriteCache.java | Stage.java |
| Previous Page - Collision detection | Current Page - 21 - Status | Next Page - Score |
| Return to the index of free Java tutorials |
(c) 2004 Planetalia S.L. All rights reserved. Unauthorized reproduction and/or mirroring is not permitted