XNA GameStudio 2.0
Creating A Bat

Our Pong game is for two players, there will be two bats on the screen during the game. We will start by creating the programming logic to make one bat work - we will use the same unit of code for the second player's bat.

Click on Project - Add Class on the menu. Choose to add a new class called Bat.cs.

XNA screenshot

A new code window will be created and a new item added to the Solution Explorer window.

Job 1 - Using Statements

At the top of the code window there is a section of statements that all begin with the word using. You need to copy the statements at the top of the Game1.cs file so that they are also at the top of the Bat.cs file. The statements are as follows,

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

Job 2 - Fields

The fields of the Bat class are the variables that we can change to affect the way that the bat looks or behaves. Start by adding the 3 statements shown below - they should be placed immediately below the line that reads class Bat.

class Bat
{
   public Texture2D sprite;
   public Vector2 position;
   public float velocity = 3.0f;
}

There are 3 main things that we monitor for our bat,

  • sprite - the graphic that we use to represent a bat in our game
  • position - the coordinates of the bat on the screen
  • velocity - how many pixels the bat moves when the player presses the appropriate key on the keyboard or game controller

The value for velocity is set to 3.0f. You can change this number to another decimal value - the letter f should be used to tell C# that you are writing a decimal (floating point value). You may wish to adjust this when you test the bat later.

Job 3 - Constructor

There are some things that we want to do automatically for each bat object that we create in our game. We do these things in what we call a Constructor . Change your code so that it reads like the example below,

class Bat
{
   public Texture2D sprite;
   public Vector2 position;
   public float velocity = 3.0f;
   public Bat(Texture2D loadedTexture)
   {
      position = Vector2.Zero;
      sprite = loadedTexture;
   }
}

Our constructor sets up a starting position of (0,0) - which we will change in another part of our code. It also specifies the graphic that we are going to use to display the bat on screen.

Job 4 - Bat Movement

Our last job is to add 2 procedures that make the bat move up or down the screen. When the bat moves, we need to change the Y coordinate of its position. We subtract the velocity when we move up the screen, we add the velocity when we move down. The code for the class should now read as follows,

class Bat
{
   public Texture2D sprite;
   public Vector2 position;
   public float velocity = 3.0f;
   public Bat(Texture2D loadedTexture)
   {
      position = Vector2.Zero;
      sprite = loadedTexture;
   }
   public void MoveUp()
   {
      position.Y -= velocity;
   }
   public void MoveDown()
   {
      position.Y += velocity;
   }
}

At this stage we have some code for the bat but we haven't yet made a bat appear on the screen. You will not see a bat on the screen if you run the program now - but you will be given feedback about any errors that you have made in the code.