Programming the Space Invaders – C# XNA 4 – Part 1


Work in progress

So, I started writing Space Invaders with XNA/C#. I will extend this post once I actually finish the game. However, each milestoneI’m going to take the time to document it and write it here. Let me know if I need to flesh anything out. I hope I can help someone as I help myself. The link to download the actual Visual Studio project is at the bottom of this file, but below is the explanation of the code. I will improve it as I go. I’ve wrote a couple of things already, Pong and the majority of Arkanoid – unfortunately part of the collision detection really stumped me. I will return to it, but to take a break from the physics, I decided to start on Space Invaders. Here’s the outcome.

Please remember that I am not an experienced programmer and my way may not be either the best way or the right way. This is my journey and I will update the posts as I hit problems and learn how to do things in a better way, until the post becomes pointless and forgotten at which time I will ‘de-publish’ it. ūüėČ

This first section will be enough to set up the boilerplate required for the game, display your ship, allow you to move it with AD or Cursor LEFT, RIGHT.

Part 1 – Getting the ship on screen and moving it.

a. Create new Windows Game with XNA. Let’s call it Space Invaders. Drag the Ship.png file into your content pipeline and call it what you want. I call it Ship.
b. Create a new class called Ship.cs. This class will be the object that is your ship to fight the space invaders.
c. Before or after creating this class, we need to write the main program.

a. The graphics

The ship

b. Ship.cs – The player’s ship object/class

Include the XNA Framework in the class.
Include the XNA Framework Graphics subsystem in the class.

Create internal variables:

  • Texture2D shipTexture – this will hold the sprite image/picture.
  • Rectangle shipRectangle – this is the rectangle boundary of your sprite/ship (image, really).
  • Rectangle viewportBoundary – this rectangle contains the screen dimensions – the viewport.. the boundary.
  • public float shipVector – This contains the vector/direction/position of your ship. It will only move on one axis – X, or horizontal.¬†Publicly¬†accessible outside of the class.
  • public int shipLives – The number of lives your ship/player has left.¬†Publicly¬†accessible¬†outside of the class.

Create the methods:

  • Publicly accessible class method Ship. This will import into the class the image to load for the ship sprite, and the dimensions of the viewport/boundary at game initialization/load time/run time. ¬†Parameters are two, Texture2D shipTexture, and Rectangle viewportBoundary.
  • Publicly accessible method of type Rectangle called getShipRectangle. This populates the variable containing the rectangle boundary of your sprite/ship (image, really). Parameters are two integers: int shipVectorX, int shipVectorY. — MAY REMOVE THIS METHOD LATER.
  • Private ¬†void called startShip – using this to contain the object initialization sequence. — MAY HAVE VARIOUS STATE LOGIC HERE LATER WHEN IMPLEMENTING DEATH/REBIRTH/NEW LEVELS.
  • Public void called Initialize() – using this to call the initialization sequence from the outside. All this does for now is call startShip();
  • Public void called Draw. This is where the object/ship/sprite is redrawn every frame and essentially is how it ‘moves’ the ship. It accepts a SpriteBatch parameter called mainSpriteBatch. mainSpriteBatch is where I am going to load all my sprites/ships/aliens/projectiles [I think].
  • Public void called Update. This accepts no parameters but essentially handles the change in ship/sprite position. It will update the shipRectangle.X setting (position of the ship’s rectangle/boundary of the ship/sprite/image) to whatever has been set via the public floating point variable (ironically, casting/converting it to an integer) shipVector. — MAY REMOVE THIS TO PREVENT PUBLIC ACCESS TO THE SHIP’S POSITION WITHOUT GOING THROUGH A CLASS METHOD.

c. Game1.cs – The main game object/class

Create class variables:

  • Ship shipPlayer1. Defining that the variable shipPlayer1 will contain an object of the type Ship (our Ship class from earlier).
  • Rectangle viewportBoundary. This contains a rectangle of the screen dimensions, used for viewport boundary.

Create the methods:

  • In the class method, public Game1. Add the definition to create/instantiate the viewportBoundary rectangle to the desired resolution.
  • Within the protected method Initialize, set the graphics viewport object to have a backbuffer width and height of the desired resolution. Remember to call the ApplyChanges() method for the graphics viewport object.
  • Create private method called gameStart() – will use this to initialize the objects and settings back to the start of a game state, or for example a new level. For now, it just calls the shipPlayer1.Initialize() method. (Remember the Initialize method in the Ship class we wrote earlier? It calls that for the instantiated object you defined at the top of this class; shipPlayer1).
  • Update the protected method LoadContent() to load the sprite image for your ship into your object while instantiating it. shipPlayer1 = new Ship(Content.Load<Texture2D>(“Ship”), viewportBoundary); – Remember we set the parameters up for the class method earlier to accept a Texture2D and a Rectangle for both the image and the screen resolution rectangle. Also here we call the gameStart(); method.
  • Within the protected Update() method, I make a call to another method called inputKeyboard(); We also call the shipPlayer1.Update() method [This runs the Update method we wrote for the Ship class above, remember?]
  • Within the protected Draw() method, this is where we start our spriteBatch object, call shipPlayer1.Draw method and pass it the spriteBatch object, then we end the spritebatch object.
  • Create a private void method called inputKeyboard(). In here we just check for if the Escape key is depressed – if so then exit. If the A or Left cursor key is pressed then update the shipPlayer1.shipVector public variable to decrement by 10. — PROBABLY WANT TO CLEAN THIS UP USING A VARIABLE LATER. ¬†We also do the same for D or Right cursor, but increment the shipVector by 10.

When you compile, you should have a ship that moves around.

Download the Visual Studio 2010 project here:

Part 1