XNA Game-Themed CS1 Examples (XGC1)

Release 2.0 (XNA V3.1)

Topic: Topic.7.ObjectsAndClasses
Example: Ex_1.SimpleObject

OOP: Instance Variables and Methods



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.

When the game starts, you'll see a screen that looks similar to this:

By using the left thumbstick, you can move the soccer ball around the screen.  By using the right thumbstick, you can roll the soccer ball.  To "roll" the ball means to rotates (or turn) the image of the soccer ball, while moving it sideways.  Currently, you can only roll the ball left or right - you can't roll it up or down.  Pressing the 'A' button causes the game to create a new soccer ball, back at the center of the screen.  Any older soccer balls are left on the screen, and cannot be controlled after the new soccer ball is created. 

During the tutorial, you will change this so that when the 'A' button is pressed, a new soccer ball is created in the middle of the screen (just like now), but the old soccer ball is destroyed, instead of simply leaving it on the screen.

2. Examining The Program:

In this chapter, we will begin to focus on many of the important concepts of Object Oriented Programming, which is commonly abbreviated as OOP.

Throughout many of these tutorials, we have used SoccerBalls in all our programs. In each tutorial, the soccer balls have all had the sort of properties - where are they, what texture (picture) to display, and (possibly) it's velocity. While every single soccer ball that we've ever coded up is not quite identical to the others, they are clearly very, very similar, and so it would be nice to not have to re-type this all the SoccerBall code each and every time. Instead, we would like to separate all the code (including data) about SoccerBalls into a separate 'block', that we can then easily reuse. We call this block a class; code that is neatly separated into a separate class is said to be encapsulated. (Technically, to say that a class is properly encapsulated means that the data fields and logic/methods have been set up with appropriate access modifiers, but we'll ignore this detail for right now :)  )

In addition to encapsulating all the data and logic (methods) that related to the SoccerBall into it's own class, we will also physically move that class into a separate file. This way, the details about how a soccer ball works goes into the separate file, and everything that relates to how the soccer ball gets used specifically in this game still goes into the Game.cs file. For example, the logic that causes the soccer ball to roll is put into the soccer ball's file. The logic that says that when the player pushes the right thumbstick rightwards, the soccer ball should be told to roll righwards goes into the Game.cs file.   The goal is to make Game1.cs much more simple, by 'hiding' the details of exactly how the SoccerBall rolls

As you go through this tutorial, try to focus on the difference between a class, which defines what all objects in a particular category have in common, and an object (also known as an instance of a class), which is a particular block of memory, with particular set of values associated with it.  For example, we might define a class to describe all cars.  In our simplified Car class, each car might have an engine, a maximum capacity to store some fuel, and a data field dedicated to recording how much fuel each Car object currently has in it.  One particular instance of this class, such as the author's car, might have a V8 engine, a fuel capacity of 15 galleons (of gasoline), and might currently (as of this writing) have exactly 1.2 galleons in the tank.  You might own a car with a V6 engine, a fuel capacity of 10 galleons, and might currently have 9.7 galleons in the tank.  The point here is that there is an abstract definition of what all cars have in common (this is the class), which defines what each individual object have in common.  The individual objects (the author's car, your car) store information such as exactly how much fuel is left in a particular, concrete car.

Note: we're starting this tutorial with a very basic example - there's no real need to wrap the soccer ball into it's own class, because the XNACS1Circle does most of what we need already.  Knowing this, you should use your familiarity with how SoccerBalls (and basket balls, etc, etc) have been implemented in previous tutorials to allow you to focus on those concepts that pertain specifically to Object-Oriented Programming.

2. SoccerBall.cs:

Let's examine the C# source code contained in the separate SoccerBall.cs file. 

(You may need to select the View → Solution Explorer menu item, then open up the project, and double-click on the file named SoccerBall.cs, in order to examine the source code directly)

2. Game1.cs:

Now that we've moved all the code that pertains to the soccer ball into the separate class (and file), you'll notice how simple this file becomes.  In addition to the standard, boilerplate code at the top of the file, the actual program has become trivial.  InitializeWorld sets the dimensions of the world, and then creates a SoccerBall object.  UpdateWorld creates a new SoccerBall object (if the 'A' button has been pressed), and then has a collection of single-line updates to the SoccerBall object.  Because the SoccerBall object is responsible for actually updating (and drawing) the image, this code is very simple.

You'll notice that what we've done with the code (between Game1.cs and SoccerBall.cs) is to rearrange the existing logic into two separate classes, so that the overall program is now better (it's more object oriented).  This is the essence of (code) refactoring - to refactor code means to rearrange it so that the resulting code is more desirable in some way.


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.