Home
Tutorial
Onlinegames
Downloads
Links
Contact
German version

A shooting spaceship

In this chapter I want to talk about the "problem" of how to make a starship (or any other game object) shoot. This behaviour is used in nearly every action game like MarsAttacks or my game J-Rio and even though it's really simple to implement it, I got many mails from people who didn't know how to start this (which is absolutly no shame;-). Well, let's start this chapter!

Outline of the classes and the important steps

As always I want to start this chapter by explaining the outline of the classes and the main steps and techniques I'm going to use. As Java is a object oriented programming language and a shot has many attributes and behaviours (movement, tests for collisions with enemies, walls..., ...) it should be clear that we will implement a class Shot to implement all this behaviour. This class will hold all the attribute variables (x and y position, speed...) of the shot and will have methods to represent behaviour like movement... . In most cases a shot is generated by a player object, so we will have a player object with a method called generateShot. This method will generate a shot (no wonder with this name ;-) at the position of the player, with the right direction... and will return this object to the calling class. In my example applet the calling class will be the class Main. This class stores the generated shot in a array of shot objects. Everytime the run - method of this main class is called, we will iterate over this array to move the shots, destroy them if they leave the game area and we can also test for collisions with enemies or level elements (which is not implemented in my example applet). The paint - method of the applet will also run through the shots array and will draw every existing shot in it.
If you understand everything so far, which means that you got the message, you don't have to read on but you can download the example source code and take a look at it on your own. The classes are really simple and nothing special. But of course you may read on, I'll show you the sourcecode and I'll also give some explanations to it.

The class Shot

As I already said, this class stores the coordinates of the shot, has a moveShot - method to implement moving behaviour in y - direction and a drawShot() - method, to paint the shot to the screen. Ok, here comes the code:

The class Player

This class is simple, too. It also has a move - method (to move player in x - direction), a draw - method and stores the coordinates of the spaceship. The only "interesting" method is the generateShot - method.

The class Main

And now we'll take a look at the class Main. I've deleted all for the shooting behaviour of the spaceship unimportant parts (marked with "..."). You can take y look at those parts if you download the sourcecode.

Conclusion

I hope I could convince you that it is really simple to generate shots. The technique to store game objects of the same kind in a array and iterate over this array everytime the run and paint - methods are called is used very often when programming games and other applications, so don't forget about it if you are in a similar situation. I hope that I could help you a little bit with this chapter and as always you can download the sourcecode and take a look at the example applet.

SourceCode download (*.zip - Datei)
Take a look at the applet

Next chapter

Platform game basics