Developing Game-Themed Applications with XNA Game Studio
Section III: The Block Breaker
Game
a. The simple circle again
back to workshop main page
Building and Running:
Here is the source code to a very simple XNA project
based on the XnaAssignmentBase library.
Download and unzip this file and you should see a SourceCode folder. With
XNA Game Studio Express installed on your PC, you should be able to double click
on the SimpleXNA.sln file to invoke the IDE.
You can now Build
and run this program. When the application first starts, you will see:
The right thumbstick on the gamepad controller controls the relative
position of the circle. Notice that, in this case, you can also use the arrow
keys on your keyboard to control the circle. The XnaAssignmentBase maps
all the controller functionality to the keyboard. Here is the
mapping between keyboard and the controller.
Implementation: comparing this project to the
one we worked on previous, we notice a few differences. Each of the following
changes are enclosed by regions with label:
__CODE_ADDED__:
-
Super class of Game1: is now XnaAssignmentBase.
-
Declare library usage:
|
#region
__CODE_ADDED__ using
using XnaAssignments;
// Using the XnaAssignments library
#endregion |
-
Constructor:
|
#region
__CODE_ADDED__ constructor
public Game1() :
base (new
Vector2(0, 0), 15) { }
#endregion |
-
Initialization: (in InitializeWorld() function):
|
#region
__CODE_ADDED__ InitializeWorld
protected
override
void InitializeWorld() {
pos = new
Vector2(); //
Memory allocation
pos.X = 5.0f; //
Initial X position
pos.Y = 4.0f; //
Initial Y position
}
#endregion |
-
Poll the game pad thumbstick position (in UpdateWorld() function):
|
#region
__CODE_ADDED__ UpdateWorld
protected override
void UpdateWorld() {
if (XnaAssignmentBase.GamePad.ButtonBackClicked())
this.Exit();
Vector2 p =
XnaAssignmentBase.GamePad.ThumbSticks.Left;
pos += p;; // left thumbstick
state
}
#endregion |
-
Draw the triangle based on the thumbstick position (in Draw()
function):
|
#region
__CODE_ADDED__ DrawWorld
protected override
void DrawWorld() {
XnaAssignmentBase.DrawCircle(
pos,
/* center of circle */
1.0f, /* Radius of circle */
0.0f, /* rotation */
Color.Aquamarine,
/* outside color */
Color.Purple,
/* inside color */
null);
/* texture file */
}
#endregion |
Lesson Learned:
Mush simpler code supporting similar functionality!Also, notice the clear
structure of event-driven model:
- Constructor: creation of the object
- Initialization: Memory allocation and initialization.
After the two one-time only functions, the rest of the application is
a continuous cycle of:
- UpdateWorld: called at real-time rate (e.g., once every 25
milliseconds), we are responsible to
- Poll: user input device, and
- Update: perform any necessary state changes.
- DrawWorld: draw everything.
It is important to note that, under this model, our program should not
draw during UpdateWorld() and should not update
application state during DrawWorld().
This document and the related materials are developed with support from
Microsoft Research Computer Gaming Initiative under the Computer Gaming
Curriculum in Computer Science RFP, Award Number 15871, and 16531.