| XNA Game-Themed CS1 Examples (XGC1) | |
|
Release 2.0 (XNA V3.1) |
|
Need (library reference):
References:
Goals:
1. Obtain the example code
When the game starts, you'll see a screen that
looks similar to this:

As you can see, there is a single soccer ball on the screen. Using the right thumbstick, you can move the soccer ball around (if you want, you can even move it off the screen). The program prints out a simple, purely textual message at the top of the screen, and it prints the current location of the soccer ball at the bottom of the screen.
The game will exit if you press the 'Back' button, as is true for all the tutorials so far. Because this is a standard feature of our programs, it will not be mentioned in future tutorials.
2. Examining The Program:
Let's examine the
C# source code that produces the behavior we see on-screen
We need to declare (to define, really) our instance variables before we can use them.
|
public
class Game1 : XNACS1Base { private XNACS1Circle aBall; // a soccer ball // Constants for circle/ball definition private const float BALL_INIT_X = 50.0f; private const float BALL_INIT_Y = 35.0f; private const float BALL_INIT_RADIUS = 3.0f;
|
private const float BALL_INIT_Y = 35.0f;
|
protected
override
void
InitializeWorld() { World.SetWorldCoordinate(new Vector2(0,0), 100.0f); // initialize the soccer ball aBall = new XNACS1Circle();aBall.Center = new Vector2(BALL_INIT_X, BALL_INIT_Y);aBall.Radius = BALL_INIT_RADIUS; aBall.Texture = "SoccerBall";}
|

|
protected
override
void
UpdateWorld() { if (GamePad.ButtonBackClicked()) this.Exit(); // Change Ball position with Right Thumb Stick aBall.CenterX += GamePad.ThumbSticks.Right.X; aBall.CenterY += GamePad.ThumbSticks.Right.Y; // here is the function that prints! PrintMessageToTopStatus(); EchoToBottomStatus("Soccer Ball position: " + aBall.Center); } |
aBall.CenterY += GamePad.ThumbSticks.Right.Y;
is really the same thing as
aBall.CenterX
= aBall.CenterX +
GamePad.ThumbSticks.Right.X;
In other words, take the current X value off the right thumbstick, add that to the current value of the soccer ball's CenterX property, and then assign that sum back to the soccer ball's CenterX property. The next line does the same thing, but for the ball's CenterY property
PrintMessageToTopStatus();
This line of code represents a call to a function (just like EchoToBottomStatus), except that this is not a built in function. Instead, this is a function that we have defined, ourselves.
|
#region
utility functions
///
<summary>
/// This is the "comment area" for the function. You should /// use this area to explain the details of what your function does. /// In this case: /// /// This function prints a simple message to the /// TopStatus area in the application window. /// </summary> private void PrintMessageToTopStatus() { EchoToTopStatus( "A Ball and function call. This is pretty cool eh?!");} #endregion |
/// <summary>
/// This is the "comment area" for the function. You should
/// use this area to explain the details of what your function does.
/// In this case:
///
/// This function prints a simple message to the
/// TopStatus area in the applicaiton window.
/// </summary>
The THREE slashes is a hint to C#, to tell C# that we've formatted the comment in such a way that we can use various C# tools to quickly produce documentation, automatically, from these comments. For our purposes, the two different styles will do the same thing, so we won't spend any more time on them here.
PrintMessageToTopStatus();

FURTHER EXERCISES::