Training and Consulting | ||
| Home |
| Previous Page - Frames - II | Current Page - 16 - The Player | Next Page - Controlling the player |
| Return to the index of free Java tutorials |
The PlayerThe Player is nothing more than an actor that can be controlled by the user using the keyboard or mouse. We'll make the Player class extend from Actor, and we will adopt the following premises regarding its behaviour, in order to model it correctly:
Using these premises, it's clear that the ship of the player is very similar to a critter, with the only exception that is has both an x and a y speed. We will also use the following image for the ship, which will have a single frame::
Having said that, let's see how the Player class looks like (initially without movement):
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 version16; 10 11 public class Player extends Actor { 12 protected int vx; 13 protected int vy; 14 15 16 public Player(Stage stage) { 17 super(stage); 18 setSpriteNames( new String[] {"nave.gif"}); 19 } 20 21 public void act() { 22 super.act(); 23 x+=vx; 24 y+=vy; 25 if (x < 0 || x > Stage.WIDTH) 26 vx = -vx; 27 if (y < 0 || y > Stage.HEIGHT) 28 vy = -vy; 29 } 30 31 public int getVx() { return vx; } 32 public void setVx(int i) {vx = i; } 33 public int getVy() { return vy; } 34 public void setVy(int i) {vy = i; } 35 } 36 Of course the main class must be changed to include the player as an actor. We will not, however, add the player to the global list of actors since the player is a very particular actor and we want to handle all that happens to him in a very specific way. This does not change the fact that the player is still an actor, and so it will be updated, painted, etc.. just in the same way as any other factor. :
1 package version16; 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.Graphics2D; 16 import java.awt.event.WindowAdapter; 17 import java.awt.event.WindowEvent; 18 import java.awt.image.BufferStrategy; 19 import java.util.ArrayList; 20 21 import javax.swing.JFrame; 22 import javax.swing.JPanel; 23 24 public class Invaders extends Canvas implements Stage { 25 26 private BufferStrategy strategy; 27 private long usedTime; 28 29 private SpriteCache spriteCache; 30 private ArrayList actors;
|
||||||||||||||
Do you want to be notified when new tutorials or lessons are published? Press here
| Actor.java | Invaders.java | Monster.java | Player.java |
| SpriteCache.java | Stage.java | Test.java |
| Previous Page - Frames - II | Current Page - 16 - The Player | Next Page - Controlling the player |
| Return to the index of free Java tutorials |
(c) 2004 Planetalia S.L. All rights reserved. Unauthorized reproduction and/or mirroring is not permitted