| XNA Game-Themed CS1 Examples (XGC1) | |
|
Release 2.0 (XNA V3.1) |
|
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 (along with the ball's radius). As you move the ball around on the screen, you'll notice that the size of the ball changes in proportion to the height of the ball - as you move the ball upwards, it gets bigger, and as you move it downwards, it gets smaller. You'll also notice that the ball gradually sinks towards the bottom of the screen.
2. Clarifying The Algorithm Using Functions.
Since the source code to this program is nearly identical to the program used in the previous tutorial, we will only examine the code that's new, or different.
The term "algorithm" is basically a fancy word for "a series of steps that you do in order to achieve some goal". One could say that there's an algorithm for driving from your house to the grocery store (which might look something like: (1) go north on the street you live for 5 blocks, (2) take a left onto Grocery St, (3) go 3 blocks on Grocery St, (4) turn right into the grocery store's parking lot), that there's an algorithm for assembling a peanut butter and jelly sandwich, etc, etc. In some ways, "algorithm" is very similar to "recipe", except that "algorithm" is usually used to describe a series of steps that are specific to calculating or computing something. Let's look at how functions can help clarify an algorithm by examining the UpdateWorld routine:
|
protected
override
void
UpdateWorld() { if (GamePad.ButtonBackClicked()) this.Exit(); // Step 1: move the soccer MoveSoccerByThumbStick(); // Step 2: Drop the ball gradually DropSoccerGradually(); // Step 3: Compute soccer size based on current height ComputeSoccerSize(); // Step 4: Print message for the user PrintMessageForUser(); } |
|
protected
override
void
UpdateWorld() { if (GamePad.ButtonBackClicked()) this.Exit(); // Step 1: move the soccer // Change Ball position with Right Thumb Stick m_TheSoccer.CenterX += GamePad.ThumbSticks.Right.X; m_TheSoccer.CenterY += GamePad.ThumbSticks.Right.Y; // Step 2: Drop the ball gradually const float DROP_RATE = 0.1f; m_TheSoccer.CenterY -= DROP_RATE; // Step 3: Compute soccer size float yPos = m_TheSoccer.CenterY; const float RADIUS_SCALE_FACTOR = 0.16f; m_TheSoccer.Radius = BALL_INIT_RADIUS + (RADIUS_SCALE_FACTOR * yPos); // Step 4: Print message for the user EchoToTopStatus("Simple 4-step instruction for this program!"); EchoToBottomStatus("Soccer Ball position: " + m_TheSoccer.Center + " Radius:" + m_TheSoccer.Radius); } |
|
///
<summary> /// Change the center of the soccer ball by right thumb stick movements. /// </summary> private void MoveSoccerByThumbStick() { // Change Ball position with Right Thumb Stickm_TheSoccer.CenterX += GamePad.ThumbSticks.Right.X; m_TheSoccer.CenterY += GamePad.ThumbSticks.Right.Y; } |
|
///
<summary> /// decrease soccer's Y position by some constant value /// </summary> private void DropSoccerGradually() { const float DROP_RATE = 0.1f;m_TheSoccer.CenterY -= DROP_RATE; } |
is equivalent to:
m_TheSoccer.CenterY = m_TheSoccer.CenterY + DROP_RATE;
You'll notice that
m_TheSoccer.CenterY -=
DROP_RATE;
is equivalent to:
m_TheSoccer.CenterY = m_TheSoccer.CenterY - DROP_RATE;
|
///
<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 PrintMessageForUser() { EchoToTopStatus( "Simple 4-step instruction for this program!");EchoToBottomStatus( "Soccer Ball position: " + m_TheSoccer.Center + " Radius:" + m_TheSoccer.Radius);} |
FURTHER EXERCISES::
Adding Steps To Your Algorithm
For this exercise, you should use the same project that was explained in
the above tutorial.
Let's say that you want to add a rectangle to the screen, which will move up
and down along the vertical line X = 10. The Y value of the center of the
rectangle will always be exactly the same as the Y value of the soccer ball.