You are here: JGT Tutorial > 4. The spaceship

The spaceship

Let's now add a spaceship to the game. The main loop that we saw in the previous step will repeatedly update the position of the spaceship based on any key we press, and then redraw the spaceship at the right place on the screen.

In order to keep our code orderly and easy to read, we will put the code for the spaceship in a different file, Ship.java. The code for Ship.java is below.

import java.awt.Graphics;
import com.jpemartin.jgt.*;

public class Ship extends VisibleObject {

    MoveControl _move;
    Drawable _draw;

    public Ship(HasSize world, KeyboardStatus kbd) throws Exception {
        // get ship to initial state
        _alive=true;
        x = 100;
        y = 100;
        int worldWidth = world.getWidth();
        int worldHeight = world.getHeight();
        width=38;
        height=38;
        _move = new MoveKeyboardAccel(kbd,-width/2,-height/2,worldWidth+width/2,worldHeight+height/2);
        _draw = new DrawAnim("gamedata/ship.png");
    }
    

    public void move() {
        // change x and y
        _move.move(this);
    }

    public void draw(Graphics g) {
         // draw the ship
        _draw.draw(g,x-13,y-17);
    }
}
There are several important things going on in Ship.java. We're using the Java Game Toolkit to make it very simple to create the ship. There are three things we're using to simplify our life here.

First, we're using the DrawAnim class from JGT to draw the picture of the spaceship. The picture is in the file "ship.png". The following code loads the picture: "_draw = new DrawAnim("gamedata/ship.png")". DrawAnim will draw the ship with a transparent background by default. The background is determined by looking at the top-left pixel: whatever color that pixel happens to be will be made transparent.

Second, we're using the VisibleObject class from JGT. We're using it here because the ship is something that we'll draw inside of the main loop. Every VisibleObject must have a Draw(Graphics) method, and the idea is to call that method inside the main loop. In our case, draw simply lets the _draw object (remember, that's the DrawAnim where we loaded the picture) do the actual work of drawing the ship. This style of programming is called Delegation and it is very powerful. We use it a lot in JGT.

Third, we're using the delegation pattern again on an object of type MoveKeyboardAccel to handle keyboard interaction. The main loop calls the move function of the Ship object, and this function delegates the actual moving to the _move object. This is a very convenient way to specify objects that are controlled by the player. If using a joystick or mouse instead of the cursor keys, you can substitute something else instead of MoveKeyboardAccel, as long as it implements the MoveControl interface.

That's all it takes to get a ship in working order. To put that ship in the game we have to create the Ship object and call its move and draw methods inside of the main loop. Here's the new main loop from MyGame.java:

/** main game loop **/
   public void run() {
      try {
      _ship = new Ship(this);
      
      createImageAndGraphics();
      while (_gameRunning) {
         _ship.move(this);
         drawWorld();
         showAndWait();
      }
      } catch (Exception x) {
         x.printStackTrace();
         throw new RuntimeException("Sorry, there's some sort of bug",x);
      }
   }
The new game looks like the screenshot below. You can use the arrow keys to move the ship around. If you hold the key down, the ship will accelerate up to a point; this is a feature of MoveKeyboardAccel, it allows precise control of the spaceship.

Next: 5