XNA Game-Themed CS1 Examples ( XGC1 )

Release 2.0 (XNA V3.1)

Topic: Topic.1.GTC_Program_Model
Example: Ex_2.WithXNACS1Lib_FromStarterProject

Creating Game-Themed Applications from a Starter Project


1. Obtain the example code

Download and unzip the zip file and you will see an ExampleProgram folder. Open the ExampleProgram folder, the EXE folder contains the compiled program and you can double click on the .sln file to work with the source code.

2 . Get Visual Studio and XNA installed

In order to do anything with the game that has been , you'll need to edit the program, using Microsoft Visual Studio.  As of this writing, you can use either Microsoft Visual Studio Professional Edition, or the freely downloadable Microsoft Visual C# Express Edition.  Once you've got Visual Studio installed, you'll need to install the extra XNA stuff.  Instructions for doing this can be found elsewhere, so we won't repeat them here. 


Please ask your instructor which software packages you should use, if you haven't already been told.


No matter which version you installed (Visual Studio Pro, Visual C#, etc, etc), these instructions will refer to the software as 'Visual Studio'


3 . Open, Compile, Watch It Work (Or Not Work)!  

If you're provided with a starter project (or if you just finished the tutorial on how to create your own XNACS1 game, starting from a normal, basic XNA program), the you'll have a number of files, contained in a couple different directories. 


In order to do anything with the game, you'll need to edit the program, using Microsoft Visual Studio. You start editing a program similar to the way that you would start editing, say, a Microsoft Word document:

  1. Find the file you want to edit, double-click on it, and Microsoft Windows will figure out that you want to edit it with Visual Studio, then start Visual Studio, and tell Visual Studio which file to edit.
  2. A different option is to start Microsoft Visual Studio, then tell Visual Studio to open the file you want to work with.  Just like in Word, you'll use the File menu.  However, since you want to work with an entire project (which is composed of multiple files), you'll want to use the File → Open Project menu option, instead of the normal Open File option.

If you want to edit the program by double-clicking on a file (perhaps one that you found in My Computer), then you'll need to follow these steps:

If you want to edit the program by first opening Visual Studio, then you'll need to follow these steps:


Once you've opened the Solution/Project, you can now compile and run the program/game:

4. Structure of the project

Now, let's look at each file, and a brief description of what it does, using the above picture as a guide.  The files that are particular important, and that you'll use frequently (inside or outside of Visual Studio) are highlighted like this .


(in Windows)
(in Visual Studio's Solution Explorer)
SimpleXNA.sln Solution 'SimpleXNA' (1 project)

(this is the top line in the Solution Explorer)

.sln - this is the solution file.  This can contain multiple projects (see the .CSPROJ entry below for more on projects)

You will frequently use this file to start Visual Studio

SimpleXNA.suo <not visible in the Solution Explorer> .suo - this is data file that Visual Studio uses. It's a temporary file that Visual.Studio uses to store information it has generated from the other files, so we will not change this file directly.


Note: Your instructor may ask you to delete this file before turning in your homework, since it takes up space, and yet Visual Studio will automatically generate it from the other files in the solution.

SimpleXNA /SimpleXNA.csproj SimpleXNA

(it's the line that's immediately underneath the

Solution 'SimpleXNA' (1 project) line)

This is the project description file for the .sln solution file. In general, a solution (one .sln file) can consist of many different projects (many .csproj) files.  Each project might be a separate program, that are all related to the overall solution.

For example, 1 project might be a game that we're making, another project might be the installer program that will install our game onto customers' computers

SimpleXNA / Properties /AssemblyInfo.cs Properties /AssemblyInfo.cs Description for the project, e.g., name to appear on the window title bar.


Note : Technically, your program (your game) can still be compiled even if this file is missing, by removing this file from the project.  HOWEVER, you should make sure that you hand this file in, since it will make it easier for your instructor to quickly and easily grade the assignment.

<not visible in Windows> References This is a list of D ynamic L ink L ibraries - files that contain code that extend the C# language to do new things.  You can't directly see this in Windows because these are really a list of references to already existing files (think of them as being like hyperlinks to existing files).
What these files do:
  • Microsoft.Xna.Framework , and Microsoft.Xna.Framework.Game DLLs make it possible for our C# program to use functionality built into XNA. 
  • The System and mscorlib libraries together provide the 'basic' functionality of the .Net Framework Class Library.
  • The XNACS1Lib , provides all the extra functionality (on top of XNA) that we'll be using in these exercises and tutorials here.
SimpleXNA / Content /Content.contentproj Content This is the content project description file. Fonts, file texture images, and audio wave files are examples of contents . We will need to describe how these contents will integrate into our project via this " content project".


Note : You normally won't really need to do anything with these files.  If you're creating a game on your own, you may want to add a new font, or new images, and those sorts of things would go into this directory.

<not visible in Windows> Content/References Much in the same way that the overall References folder contains additional DLLs that extend the C# language to use things like XNA, this folder contains additional DLLs that extend Visual Studio itself.  In particular, these libraries are used in the 'compile' phase, in order to take something like the Arial.spritefont file, and process is so it can be included the game's executable file.
<not visible in Windows> Content/Resources/Fonts/ Arial.spritefont This contains a definition of the Arial font, which the XNACS1Lib requires in order for the game to run.
<not visible in Windows> Content/Resources/Textures < This is not present in the above picture.>

This folder contains any graphics that you want to display during the game. 
Allowable image types are listed here , but you may have different results, based on the exact version of XNA that you're using.

SimpleXNA / Content / bin and obj <not visible in the Solution Explorer> Build results from the content-related compile phase of creating the game.


Note: The directories, and everything in them, are generated from the other files in the solution, similar to the SimpleXNA/bin and SimpleXNA/obj directories, above


Note #2 : Because these files can take up a fair amount of space, and because these files are generated from other things in the solution, your instructor may ask you to delete these directories before handing them in.

SimpleXNA /Game.ico Game.ico This is the graphical icon on the title bar of the running program.
SimpleXNA /Game1.cs
(this is in the SimpleXNA directory, and the file is named Game1.cs)
Game1.cs This file contains the Game1 class. This is the main file we will work with.  It contains almost all the C# source code that we'll use in our game; you change your game by editing the source code, and then telling Visual Studio to re-compile the source code into a program (into a game), and then running that program (game).


You will frequently use this file to edit your game.  You will use this file more than any other file, most likely.


NOTE: The name of this file may be different from what you see here - it may or may not be Game1.cs exactly.  To find this, look for a file ending in .CS that isn't the Program.cs, nor the AssemblyInfo.cs.


Note #2 : A program can actually be stored in multiple .CS files (normally 1 class goes into each file), in which case you'll see multiple .CS files, possibly located in subfolders (you'll see the same subfolder in both Windows and Visual Studio)

SimpleXNA /GameThumbnail.png GameThumbnail.png This is the graphical file for representing this game on the XBOX 360 console. By default, this looks like a some circles
SimpleXNA /Program.cs Program.cs If you open this up, you'll see that it's very short, and almost identical for every game you'll work with.

The purpose of this file is to start the program, by loading the Game1 class, and telling it to run.

Normally, you'll never need to look at this file, nor will you need to change it.

SimpleXNA / bin and
SimpleXNA obj
<not visible in the Solution Explorer> Compile result. As the names suggests, obj contains the compile object files, while bin contains the executable binary files.


Note: The directories, and everything in them, are generated from the other files in the solution.  For example, when Visual Studio compiles the source code into an actual program (an actual game), Visual Studio will put the store the program in files in these subdirectories.


Note #2 : Because these files can take up a fair amount of space, and because these files are generated from other things in the solution, your instructor may ask you to delete these directories before handing them in.

5. Overview of Game1.cs

Since this is the file that you'll spend most of your time editing, let's look at it in more detail:

6 . Initializing the game: The InitializeWorld method

There are multiple ways to initialize different parts of the game, and as these tutorials progress, you'll see more about them, and what to use them for.  For now, we're going to look at the InitializeWorld method, and how it defines the coordinates of the lower-left hand corner of the screen, the width of the screen, and (by implication) the height of the screen.


InitializeWorld(): During InitializeWorld, we allocate any additional memory our game needs, and set up everything so that our game is ready to draw something on the screen (in the above pictures, we drew rectangles on the screen - InitializeWorld is where we would set the center position, width, and height of the rectangles).  Even though we're not drawing anything on the screen right now, we still need to define the coordinate-space used by our game, like so:


protected override void   InitializeWorld()


    World.SetWorldCoordinate( new Vector2 (0,0), 100.0f); // Sets lower left corner and the width of the drawing area


7 . The UpdateWorld method :  

Normally, the UpdateWorld methods would be full of code.  In fact, most of your time will spent either in these locations, or else in other places in the code... that were called from UpdateWorld.  Right now it's nearly empty since our start project isn't really doing anything.


Note:   To work with the XNACS1Lib Library, a bare minimum project must include:

Project home page : The Game-Themed Introductory Programming Project.
Kelvin Sung
Computing and Software Systems
University of Washington, Bothell
Michael Panitz
Business And Information Technology
Cascadia Community College

Microsoft Logo This work is supported in part by a grant from Microsoft Research under the Computer Gaming Curriculum in Computer Science RFP, Award Number 15871 and 16531.