Starting Game Dev With Space Invaders
December 21, 2008
A lot of my friends asked me a simple question: How to start making game and what tool they need to use to develop? I’m sure if you interested in developing game, you did try to go to Google and type in Start Game Development and/or game engine for beginner… You will find out there are thousand of game engines out there help you to make game, so many software even don’t need you to know any about 3d or programming. So which one you should choose?
I’m not good enough to say which game engine is good what is bad but I believe you could start with any game engine. It gives you the basic step, the feeling of game developing. Whatever game engine it is, it’s all the same in the first step.
The game take me 1 hour and half to finish. It’s fun but I know I’m not gonna go far with it so I keep it at minimum level.
I will using C#.net and XNA 3.0 in my tutorial. You could download free Visual Studio Express and XNA game studio on Microsoft website. You will require intermediate knowledge about C# to start this tutorial.
My code for this tutorial are quite straight forward. You could find it at the of the tutorial. You could just read the code to learn it yourself. Bellow here is a bit of explanation what make me do thing like that.
Our Data Model will be quite simple and I hope it explain itself:
GameObject will our standard class. It is the base for all other class. Then I will have a MoveableObject. Why should I have a Moveable objects? Because a lot of classes could use variables and functions from it and you could reuse those two classes for other games as well.
Try to keep as much variables and function in those two classes as possible so we could avoid repeating codes.
You could see Alien class and AlienGroup class, why I need two of them. Well sure you could just use Alien but Alien move in a group, if we group them together, it will be much easier every we try to change the direction of the whole group.
Let start by create a new XNA project.
Start Visual Studio (VS). And choose create new project, and choose XNA Game StudioWindows Game. Choose the name as you like.
It will auto generate a project as bellow.
I you open the file Game1.cs. You will see the structure of your game. It is an infinity loop. Check XNA document for more detail.
{
get user input
update information, calculate, check collision…
draw objects out to screen
} repeat;
Create a new folder and call it “GameClasses”. This is where you keep all the classes need for your game.
In GameClasses, create a new Class name it GameObject. This will be our standard object for the game. The GameObject will inherit from the DrawableGameComponent.
Remember to add using Microsoft.Xna.Framework; Without it, VS couldn’t recognize any XNA data type.
So we have our base class. It didn’t do much, did it? That’s alright ‘cause we don’t want a class look so mess up after all.
Now we need the second class. Create a new class name it (as you guess :P) MovableObject and let it inherit from the GameObject class. It only need two methods: to move the object and check if the object go off the screen. (Check the source code for more detail).
When I say move the object, it actually only change the position variables. When the game start redraw it will know the object moved already.
So we could move the object, well when a ship shoot, we create a new Bullet class, set its speed and direction then just let it go.
One more important part of the game is collision detect. In here I just use a simple method. I track movement of every single bullet and see if it collide with any GameObject or not. Using Rectangle.IntersectWith(Rectangle) to detect the collision. Sure the object isn’t just rectangle, but it works alright for simple game like this.
If the bullet hit an object and that’s object are different with is parent object, then we dispose the object. That’s it. simple job.
The main structure of the final game should be like that
Load data
{
Update()
{
Get user input;
Move the player;
Move the AlienGroup;
Check if any bullet hits any object;
if it is destroy the bullet and the object;
}
Draw()
{
Clear Screen;
Draw out the AlienGroup;
Draw out the Player;
}
}Repeat until user want to exit.
The game will miss a lot of functions(Alien didn’t shoot or move down yet). It’s the part I want to leave out so you could use what you learnt to apply in.
That’s it for now. Sure the game isn’t just that. You need a start screen, point for player, end screen… but I will save it for part 2. Hope this tutorial’s useful for you.
Thank mate, I’m only starting with XNA. Found this quite useful for me. I really like your blog. Keep it up.
Hi, I like your tutorial, but when I try to make the alien shoot back. It’s not seem to work. Could you explain a bit more about that please.
Hi, sorry, I posted it kind of rush. I don’t feel sastify with the tut myself. Hope every could leave me some comments, I know this tutorial are useful for you, I will carry on with another tutorial.
One good thing, after 1 days, there are over 800 page views for this article :D. Great news for me :P. But only 2 comments so far