Home > Articles > Programming > C#

  • Print
  • + Share This
From the author of

GameObject Class

Although you could put all the code for the game in Game1.cs, it's not a very robust way to write code for a game. If you did it this way, it would be harder in the long run to add to and maintain the game.

What you'll do is make a base GameObject class and then have any objects in the world (including the paddle, ball, and bricks) derive from this base class. Any functionality that all game objects will need can be placed in this class, which will streamline the code.

Although a simple object model wouldn't be used on a AAA game, in this case, it strikes a balance between robustness and ease to write.

To create a new .cs file in Visual Studio, right-click the project in the Solution Explorer and select Add>New Item. Select Class and name it GameObject.cs. The process will be very similar in the other environments.

At the top of this file, you may notice using statements. These statements are much like #include in C++ or import in Java. You want to add a few more using statements to specify that you're using classes from the XNA package:

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

Next, make the GameObject class "public," which signifies that you want to be able to use the class in other files. To do this, simply add a public in front of class GameObject, like this:

public class GameObject

Next, you need to add some member variables inside the class declaration:

  •   A string, which stores the name of the texture this object uses (protected)
  •   A Texture2D, which is for this game object (protected)
  •   A Game instance, which you'll set when you create a GameObject (protected)
  •   A Vector2 to store the position, which specifies where the center of the object is onscreen (public)

These variable declarations should look like this:

protected string textureName = "";
protected Texture2D texture;
protected Game game;
public Vector2 position = Vector2.Zero;

Recall that protected variables are ones that child classes are allowed to modify. Public variables are ones that anyone can access, and although strict object-oriented programming usually doesn't use public variables, for these articles, it will make things easier to make position public.

In any event, once you have these variables, you then need to add some member functions. First, you need to have a constructor that takes in a Game class as a parameter and sets the game member variable to this parameter:

public GameObject(Game myGame)
{
   game = myGame;
}

Next, make a LoadContent function. This function loads in the texture specified in textureName, provided that the string isn't empty:

public virtual void LoadContent()
{
   if (textureName != "")
   {
      texture = game.Content.Load(textureName);
   }
}

Remember that a virtual function is one that can be overridden by child classes. Right now, you won't be overriding anything, but you will do so later on.

Next, you want to make an Update function that takes a float parameter called deltaTime. This function should also be virtual. I'll discuss what this represents in a little bit.

The function will look like this:

public virtual void Update(float deltaTime)
{
}

Finally, you want a Draw function that draws the texture, provided that it's loaded. In order to draw the texture, this function needs to take in a SpriteBatch as a parameter.

Because the position member variable is relative to the center of the GameObject, but drawing is relative to the top-left corner, you need to subtract width / 2 from the X position and half the height / 2 from the Y position to figure out the draw position, as shown in the following figure:

This leaves you with the following Draw function:

public virtual void Draw(SpriteBatch batch)
{
   if (texture != null)
   {
      Vector2 drawPosition = position;
      drawPosition.X -= texture.Width / 2;
      drawPosition.Y -= texture.Height / 2;
      batch.Draw(texture, drawPosition, Color.White);
   }
}

Paddle

Now that you have a GameObject class, you can make a Paddle class that inherits from it. So make a new class called Paddle.cs, and add the same using statements you added to GameObject.

Next, you want to change the declaration of Paddle so it is public and inherits from GameObject, which looks like this:

public class Paddle : GameObject

You need to make a constructor for Paddle that takes in a Game and passes it to the base GameObject class. You pass this using an initializer list, which you might be familiar with if you've used C++ before.

Inside this constructor, set the textureName variable to "paddle":

public Paddle(Game myGame):
   base(myGame)
{
   textureName = "paddle";
}

You'll add some custom code to Paddle in a little while, but for now leave it as is. Instead, you can create an instance of Paddle in Game1.cs.

First, add a member variable to Game1 of type Paddle (I named the variable paddle). After you load the background in LoadContent, add the following code to initialize the paddle:

paddle = new Paddle(this);
paddle.LoadContent();
paddle.position = new Vector2(512, 740);

This code creates the paddle, loads the texture, and sets the position of the paddle to be vertically centered and near the bottom of the window.

Finally, in the Draw function in Game1, after you draw the background, but before the spriteBatch.End call, add this call to draw the paddle:

paddle.Draw(spriteBatch);

Once this is done, you should end up with a paddle on screen that looks like the following figure:

Moving the Paddle

The final thing to do in this article is add the ability to move the paddle. You want to make it so the left and right arrow keys move the paddle.

To do this, I need to introduce the concept of delta time. Games are typically updated several times per second. Each update is called a frame, so "60 frames per second" means that the game updates 60 times in a second.

Delta time is the time, in seconds, that has elapsed since the last frame. In general, everything that moves in a game should be made a function of delta time, which will make the code much more flexible.

What you want to do in Paddle is override the Update function that you declared in GameObject. Add the following function to Paddle.cs:

public override void Update(float deltaTime)
{
   base.Update(deltaTime);
}

Note that the function calls base.Update, which is the Update function declared in the parent class. Before this base.Update call, check the keyboard to figure out whether the left or right arrow is pressed.

You can get the state of the keyboard by using the following line:

KeyboardState keyState = Keyboard.GetState();

Once you have the KeyboardState, you can then check whether a specific key is down. For the left arrow key, you could use the following code:

if (keyState.IsKeyDown(Keys.Left))

And a similar else if could be added for the right arrow key.

Before you actually move the paddle, though, you should add a public float member variable that stores the speed of the Paddle. This speed should correspond to the number of pixels per second you want the paddle to be able to move. For now, set the speed to 500.

For the actual movement code, you want to change the X component of the position by the pixels per second multiplied by the delta time. For example, to move left, you could do this:

position.X -= speed * deltaTime;

And moving right would be the same, except that it's += instead of -=. By writing the code this way, if you later adjust the speed, you won't have to change the update code.

The final thing you need to do is actually call the paddle's Update function. Go back to Game1.cs and look at Update. Under the TODO, you first need to calculate the value of deltaTime.

You can get this by using the following code:

float deltaTime = (float)gameTime.ElapsedGameTime.TotalSeconds;

Finally, call Update on the paddle and pass in the deltaTime.

When you run the game now, you should be able to move the paddle left and right. (You can also move the paddle off the screen, but you'll fix that in the next article!)

Although you certainly could have gotten this far with fewer lines of code, in the long run putting in the extra time to create the GameObject class will pay off.

This will become readily apparent in Part 2, in which you'll add a ball, bricks, and collision with everything.

  • + Share This
  • 🔖 Save To Your Account