| 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 the current location of the soccer ball at the bottom of the screen. You may also notice that the size of the ball is continuously, randomly changing.
2. Examining The Program:Using a function that is built into the CS1XNALib
Since the source code to this program is nearly identical to the program uses in the previous tutorial, we will only examine the code that's new, or different.
Let's start by examining how to use a pre-existing function, that returns a
value.
Our goal here is to randomly change the radius of the soccer ball with each
update of the game. We will call a pre-existing function, that will
produce the random number, and we'll use that to set the radius of the circle.
|
private
void
PrintMessageToTopStatus() { EchoToTopStatus("Watch the ball size change randomly?!"); } |
|
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 + RandomFloat(); 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; // Calling the RandomFloat() function to get a random number between 0 to 1 aBall.Radius = BALL_INIT_RADIUS + RandomFloat(); // here is the function that prints! PrintMessageToTopStatus(); EchoToBottomStatus("Soccer Ball position: " + aBall.Center + " Random Integer=" + GetANumber()); } |
2. Examining The Program:Creating Our Own, New Function
Let's move on to examining how to use a function that returns a value, that we've created ourselves.
|
private
void
PrintMessageToTopStatus() { EchoToTopStatus("Watch the ball size change randomly?!"); } /// <summary> /// This is a simple function that returns an integer. /// The purpose of this function is to demonstrate how to /// implement a function with a return value. /// </summary> /// <returns>A random integer.</returns> private int GetANumber() { return RandomInt(5, 15); } #endregion
|
/// <summary>
/// This is a simple function that returns an integer.
/// The purpose of this function is to demonstrate how to
/// implement a function with a return value.
/// </summary>
/// <returns>A random integer.</returns>
|
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; // Calling the RandomFloat() function to get a random number between 0 to 1 aBall.Radius = BALL_INIT_RADIUS + RandomFloat(); // here is the function that prints! PrintMessageToTopStatus(); EchoToBottomStatus("Soccer Ball position: " + aBall.Center + " Random Integer=" + GetANumber()); } |
will call the
GetANumber() function that we defined, above. GetANumber will call
RandomInt to produce a number as low as 5, and as high as 14.
GetANumber will then return that value back to here, where the code will
concatenate that number onto the string that we're displaying at the bottom
of the screen.
PrintMessageToTopStatus();

and replacing it with
EchoToBottomStatus("Soccer Ball position: {X:50 Y:35} Random Integer=" + 9);
FURTHER EXERCISES::