Space Invaders! The classic arcade game is back, and this time built using a single Lego Mindstorms EV3 kit.
I decide to write this game in leJOS using the 0.8.1-beta release (see the leJOS wiki for more on getting it installed). I contemplated writing it in the EV3 graphical language but the thought of coding the invader movements made me cry, so Java it is!
What’s does this little game have in it? A coin reader that waits for you to put a coin in the slot before you can play the game (just like in the arcade), a theme tune playing in the background, realistic space invader movements, a joystick using an EV3 large servo motor and invaders that fire at you as you shot at them.
Graphics and Sounds
leJOS supports simple graphics using the Image class. An Image is a series of bytes that describe a graphics object on the screen. Unfortunately you can’t take a standard graphics format such as PNG or JPG and display it on the EV3 screen. The EV3 uses a LEGO image format and the leJOS code expects the image to display to be in this format. However the EV3Control tool provided with leJOS 0.8.1-beta allows you to convert between a bitmap file and the EV3 image format.
I used Paintbrush to create the original graphics for my Space Invaders. Paintbrush is a basic image drawing program for Mac OS, which suits my needs perfectly. Each invader is a 20×20 pixel bitmap which I draw by hand. The player’s ship is also a 20×20 pixel bitmap file. I then used EV3Control to convert the bitmaps into EV3 images. These files are available in this ZIP file: ImageFiles
- Invader bitmap files. Two versions for each move of the invader: one and two.
- Ship bitmap file
- Explosion bitmap.
- Invader EV3 image file.
- Ship EV3 image file.
- Explosion image file.
I made a little theme tune to play while the game waits for you to enter a coin. I composed this using Garage Band on my iPad and then downsampled it into an 8-bit WAV file to play on the EV3. Sorry about the quality. Download it here: Theme2
You will need to copy the EV3 image files and the sound file into the same directory holding the Space Invaders jar file on the EV3 using scp. This would require you to use the Terminal and type the following commands. I assume you have your EV3 connected via a USB cable, if you use wifi then replace 10.0.1.1 with the IP address of your EV3 shown on the display. The root password on the EV3 is r00tme.
scp Theme2.m4a firstname.lastname@example.org:/home/lejos/programs
scp invader1.lni email@example.com:/home/lejos/programs
scp invader2.lni firstname.lastname@example.org:/home/lejos/programs
scp ship.lni email@example.com:/home/lejos/programs
scp explosion.lni firstname.lastname@example.org:/home/lejos/programs
The code is broken down into a number of class each of which implement a basic function. I make heavy use of Java threads to keep everything moving in parallel.
- Sprite: the Sprite class represents an image on the screen at an x,y position. Each Sprite class holds a single image and can render it onto the screen, erase it or move it. The Sprite class is used by the Ship and Invader classes to represent the graphics on the screen.
- Score: the Score class represents the player’s score, number of lives left and ammo remaining. The score class runs as a thread to update the score on the screen.
- Invader: represents a single space invader on the screen. An invader has an x,y position and can move sideways and down. The invader understands that when it reaches the edge of the screen it has to change directions and move down a line.
- Invaders: this class represents all of the invaders currently left in the game. The invaders are stored in an ArrayList of Invader class objects. Each invader is placed along a grid at its starting position when the constructor is called. Each time the invaders move I iterate over this list and tell each invader to move itself on the screen. If an invader is shot down then I simply delete it from the ArrayList and it disappears off the screen. The invaders run on their own thread so that they keep moving in the background.
- Ship: the player’s ship. This class handles reading from the joystick to control the ship movement and firing missiles at the invaders. It also runs as a thread so the player can always move the ship.
- Missile: a single missile fired by either a player or an invader. The direction of travel is controlled by the movingUp boolean variable set when the missile is created. A missile is just a 3×3 rectangle on the screen as it is faster to draw than an image. A missile runs as a thread so that it continues to move until it either goes off the edge of the screen or intersects with another sprite.
- Missiles: represents all of the missiles currently fired in the game. The key feature of this class is to run a thread to periodically check which missiles have hit which objects. If a missile has hit an invader then the player scores a point. If the missile has hit the ship then the player loses a life.
- SpaceInvaders: the main class that starts everything running.
How to play
Here’s how to play the Space Invaders game:
- Install leJOS for the EV3 on an SD card and boot your EV3.
- Copy the class files into a new project, or just grab the SpaceInvaders.jar.
- Copy the image files into /home/lejos/programs
- Run Space Invaders
- Insert a coin!
- Move the joystick left/right to move the ship and the touch sensor fires missiles.
- You start with 50 ammo so use it wisely!
- The invaders will be shooting at you so dodge their bullets while firing your missiles.
- Remember the lower down the screen the invaders get the faster they move!
- All of the image files as a zip: ImageFiles
- The compiled Space Invaders executable jar file ready to run on the EV3. Compressed as a ZIP file. SpaceInvaders.jar
- The Space invaders theme tune in wav format compressed to 8 bit for the EV3. Groovy! Theme2
- If you prefer you can download and import the SpaceInvaders Eclipse project and import it into your workspace in Eclipse. SpaceInvaders