Planetalia - Java Training

Training and Consulting

Home

Tutorial - Writing a Space Invaders game in Java

  Current Page - 00 - Introduction   Next Page - The First Window
  Return to the index of free Java tutorials  

Introduction

(c) Alexander Hristov
English Version proofreading and corrections : Jeff Lunt

The purpose of this tutorial is to show how it's possible to develop a simple java game with a more than adequate performance. We'll make use of some new features introduced in the JDK 1.4 such as built-in support for double buffering, and some others. These techniques are not usually taught in the standard courses or books about Java programming.

Instead of the traditional system of showing a completed source code and then explaining it, I prefer following a different approach, which IMHO is better for teaching, and which mirrors more closely the process that programmers usually follow when developing programs : We'll develop the game in little steps, each step adding some small feature on top on the previous steps, and explaining, in each step, what was done, and why it was done that way. I believe this helps make the design decisions more clear, and yields a better understanding of the source code as a whole.

However, understand that developing this game didn't happen in a "straight line", from the initial line of code to the last, without any changes in between; that sort of thing is extremely rare. When a programmer is developing a program, he makes mistakes, comes up with better ways of doing things, refactors source code, reorganizes responsibilities, new requirements appear, etc. Because of the reality of developing software, it would be unfair to start with a polished program and then develop it step by step as if, from the very beginning, we had a clear view of the result.
In this tutorial, I prefer to mirror the behaviour and thoughts of someone who is just starting to program, who makes some very common mistakes, and who doesn't always design adequately in the beginning, and then show how these miscalculations will come to haunt us afterwards.

I recommend that you follow this philosophy when you develop your programs. Once you've made your initial analisys and modelling (which I won't cover in this tutorial), it is very useful to have as soon as possible something "that works". Don't wait to have 200.000 lines of code before pressing the "Run" key for the first time to see if the your huge codeball makes any sense. Having something that is already working provides a steady foundation on which to fall back if some changes that you just added break everything. If you don't have a source-control system that keeps old versions of files, use the poorman's approach I've used here : create separate packages for each version. With IDEs such as Eclipse,copying and moving classes between packages is a breeze

The aim of this Java tutorial will be to develop a "Space Invaders" type of game, meaning : monsters coming from above, player below, and "shoot everything that moves" gameplay.
We will be adding more features as the development progresses, since (once we get the basics down) it will be very easy to add more interesting elements such as different shooting modes, power ups, etc.

Finally, I would like to emphasize that this is not a Java Tutorial, but a tutorial specifically aimed at building games using the built-in features of Java. Maybe I'll write some other tutorials covering JOGL or LWJGL, but for now, this is it.
If you don't know about "inheritance" or "polymorphism", or what "Graphics2D" or a "JFrame" is, then this tutorial is definitely not for you.
In order to follow this tutorial successfully, you should already know about the following topics:

  • The Java Language (of course)
  • Basic knowledge about AWT/Swing, events, event handling and components
  • Java Collections, at least HashMap and ArrayList
  • Graphics2D



Do you want to be notified when new tutorials or lessons are published? Press here


Full list of Java source files for this step


Full list of resources

explosion.wav disparo2.gif bombUL.gif oceano.gif
bombDR.gif disparo0.gif bicho0.gif bombD.gif
missile.wav test.gif disparo1.gif bombU.gif
bombDL.gif bombL.gif bicho.gif Thumbs.db
bombR.gif bicho2.gif bicho1.gif disparo.gif
musica.wav bombUR.gif photon.wav misil.gif
nave.gif      

  Current Page - 00 - Introduction   Next Page - The First Window
  Return to the index of free Java tutorials  

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