XNA GameStudio 2.0
Creating A Gameobject Class
Before starting to make this game, it is recommended that you complete the First Game tutorials in this section. Some basic XNA concepts are introduced very gently in that game and are not repeated in full or at all in this game.
The instructions here are for a simple 2D shooter and focus on the simplest aspects of the game. Therefore, you will find help on how to draw and move a rocket ship, how to fire bullets, create enemy ships to shoot at and make sure that they are destroyed at the right time. Things like sound, background textures and fancy stuff are all your responsibility.
Create a new game project the same way that you did for the First Game, making sure that you choose the Windows Game project template. You will also need to create and import the following sprite textures - again, the same way that you did for the first game.
When you are done, the Solution Explorer window should look like this,
Save the work you have so far and get ready for some programming.
The Gameobject Class
There are certain things we need to keep track of for any objects in our game. These things include the sprite that is being used to draw the object on screen, the position of the object and the velocity of the object. By setting up a Gameobject class, we will define these things in one place in our code and will be able to use the class to make all of our other items work.
The Gameobject class will be very similar to the Bat class in the Pong game.
Start by creating a new class in your project called GameObject.cs - use the same capitalisation as I have. You will need to copy the Using Statements from the main Game class and paste them at the top of the code window.
The entire class should read as follows,
public Texture2D sprite;
public Vector2 position;
public Vector2 velocity;
public bool alive;
public GameObject(Texture2D loadedTexture)
position = Vector2.Zero;
velocity = Vector2.Zero;
sprite = loadedTexture;
alive = false;
The four fields are used to store the texture that is used when the game object is drawn on the screen, to store the position and velocity. The alive field will be used to keep track of whether objects like bullets and enemies should be drawn on the screen.
Notice that all four fields are assigned a value in the constructor method.
Save your project. Running the project at this time won't do much but it will highlight any typing errors that you have made.