C# Projects
Peace - 2D XNA Tutorial
Table of Contents
The first thing involved with game programming, well with any programming for that matter, is knowing what you want to program, or what the end result should do. For this tutorial, we will be creating a simple 2D sprite manipulation program. In order to complete this tutorial, let's make sure we have all of the required equipment!
Checklist:
Make sure Microsft XNA is installed and working. Start a new Microsoft XNA project and choose a save directory. Once the solution is created, download the Sprite Image to the Content folder of the solution you are using (ie. SolutionName\ProjectName\Content).
For this tutorial, we will be implementing OOP. Our first class we will be creating will be the Sprite class. Right-click on the project name in the Solution Explorer on the right side of the screen. A menu will pop down. Navigate to Add->New Item...
![]()
Once the Sprite class is opened, we will begin defining what a Sprite "is". Before we can begin declaring the Sprite vairables, we need to make sure we have access to certain variable types. To do this, we will include the XNA classes we will be using.

From here, we can now start coding the Sprite properties. Let's start with the Sprite Variables.
![]()
The #region and #endregion commands are used to organize "regions" of simlar code. We declared this region Variables so that anyone can easily indentify where the Sprite variables are declared. As for the variable types, Vector2 represents a 2-dimensional (X, Y) point, or vector. A Texture2D variable represents a 2-dimensional texture or image. After the Variables region, we will create the Sprite Constructor.
![]()
The Sprite Constructor is the method we will call to actually create an instance of a Sprite object. It takes in 4 parameters in order to create the Sprite; the starting float X and Y position, the float movement speed, and a boolean that tells whether the Sprite is the Player or the Enemy.
![]()
Within the Sprite Properties region, we create simple methods for manipulating the Sprite properties, that in turn manipulate the Sprite variables. Notice that these are declared as public in order to be accessed from classes outside of the Sprite class. This allows for other classes to get or set the desired value of the property of the Sprite. Also note that each one has a return value of either Vector2 or Texture2D. These methods, in a sense, work as a individual variables themselves.
![]()
The Sprite Update is where all of the game logic is going to be executed. This is where user input will be detected and translated into Sprite movement. Also, the Enemy movement will be updated based on the current Player position. The update initializes the keyboard for input then checks to see if the current Sprite is the Player or the Enemy and updates accordingly.
![]()
Finally we make it to the Sprite Draw method. This is where the actual Sprite, texture and all, will be drawn onto the screen. It checks to see if the Sprite is the player or the enemy and draws them with the correct color tint overlay.