Developing Game-Themed Applications with XNA Game Studio
Section I: Overview and Introduction
e1. Defining and Controlling a circle
back to workshop main page
References:
Goals:
- To experience with defining objects and interacting with users via the
XNSCS1Lib library.
- We will create this program where the program
draws and allows the user to move a circle by manipulating the left
thumbstick (or i,j,m,k keys).
Notice: To work with the XNACS1Lib Library, a bare minimum project must include:
- Fonts: the Arial.spritefont must be part of the project
- 2 functions:
- InitializeWorld(): allocate memory and initialize instance
variables.
- UpdateWorld(): poll user input and update application state.
- Note: drawing happens by default!
BeginTemplateSource:
Download and unzip the begin template source folder and you will see two
folders:
- SourceCode folder contains an template SimpleXNA project.
- UsefulFiles folder contains:
- Arial.spritefont - this is the font we need in the
project. We must create a proper folder and add this file into the
project.
- XCS1Lib_PC.dll and
XNACS1Lib_XBOX.dll - these are the library files for
Windows/PC projects and for XBOX 360 console projects respectively.
We must include the reference to the appropriate file in our
project.
If we try to compile this project, we will get an error messages. In this
tutorial we will
- Fix the compile error by including XNACS1Lib_PC.dll as a library
reference for the project.
- Include code to define and control the circle.
- Show how to include the font file in the Content folder.
1. Reference to the DLL library:
- Create a folder for the library file:
- Go to explorer
- navigate to SimpleXNA
- Create a new folder: name it SupportFiles
- Copy UsefulFiles/XNACS1Lib_PC.dll into
SupportFiles folder
- Include the library file as part of the project:
- Go back to XNA GS
- Right-Mouse-Button click over the project Reference
folder:
- Click on Browse tab
- navigate and select: SupportFiles/XNACS1Lib_PC.dll
Now we can compile and run the application. Though, nothing shows up,
this is expected because our application is complete empty.
2. Define and control a circle:
- Instance variable: Define a circle
|
XNACS1Circle
circle;
// The circle |
- InitializeWorld(): during initialization, we allocate
memory and initialize the center position:
|
protected
override void
InitializeWorld() {
World.SetWorldCoordinate(new
Vector2(0f, 0f), 15f);
// Define world coordinate
circle = new
XNACS1Circle(new
Vector2(5f, 5f),
// center
1.0f);
// radius
} |
- UpdateWorld(): update the center of the circle by the left
thumbstick (or the arrow keys):
|
protected
override
void UpdateWorld() {
if (GamePad.ButtonBackClicked())
this.Exit();
center.Center += GamePad.ThumbSticks.Left;
EchoToBottomStatus("Circle drawn at:
" + circle.Center);
EchoToTopStatus("i-up m-down
j-left k-right");
} |
Now we can compile and run the application and see the circle. However, we
cannot see any output messages. This is because we have not included the font
files in the project.
3. Including the font file:
- Create the proper resource folder, in the GS:
- Right-Mouse-Button over the Content folder
- Add -> New Folder: name the new folder:
Resources
- Right-Mouse-Button over the newly created Resources
folder
- Add -> New Folder: name the new folder: Fonts
- Copy UsefulFiles/Arial.spritefont into
SupportFiles folder
- Include the font file as part of the project:
- Right-Mouse-Button click over the newly created Fonts
folder:
- Add -> Existing Item ...
- Navigate to UsefulFiles/Arial.spritefont
Now finally we can compile and run the application and observe the output
messages.
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.