| 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:

Just like in the prior tutorial, the left thumbstick controls the pink/blue 'spot' on the screen. Until the user presses 'A' button for the first time, the screen is blank except for the spot (and the top/bottom status messages). When the player presses the 'A' button, the current soccer ball (if any) disappears from the screen (and from the program's internal logic), and a new one is created on the spot's current location. If there's a soccerball on the screen, then the right thumbstick can be used to roll it, and the same sort of logic is still used to ensure that the program doesn't crash, prior to the first soccer ball's appearance.
What's new in this tutorial is that the ball moves constantly. Here is where we'll start to see the power of OOP, as we can add this 'always moving' functionality to the SoccerBall, and not the overall game. While this 'always moving' functionality is pretty simple, you could imagine more complicated functionality that we could add to the ball, while keeping the core game logic simple. A couple of examples include the ball fading in-and-out, the ball 'jumping' from one place to another, etc, etc
2. Game1.cs:
This is almost identical to what we looked at in the prior tutorial. The only thing that's new is that whenever the game as a whole updates itself (i.e, whenever the UpdateWorld method is executed), we will need to update the SoccerBall (in order to make sure that when it reaches the edge of the screen, it reverses it's currently horizontal/vertical direction).
While we could do this all in the UpdateWorld, it will be better (more object-oriented) to do have the SoccerBall to update itself. In order to make this possible, the SoccerBall class will implement an UpdateBall method, which we will here in UpdateWorld:
|
if (null != m_TheBall)
{
m_TheBall.RollTheSoccer(GamePad.ThumbSticks.Right.X);
m_TheBall.UpdateBall();
}
|
1. SoccerBall.cs:
We've decided to neatly encapsulate all the 'motion related' logic here inside the SoccerBall class. Let's examine the details of the code:
|
#region constants private to this
class
private
const float RADIUS = 3.0f;
private
const float ROLL_ANGLE = 1.2f;
private
const float MOVE_UNIT = 0.1f;
private
const float INIT_VELOCITY_X =
1f;
private
const float INIT_VELOCITY_Y =
1f; #endregion |
|
/// <summary>
/// Constructs a SoccerBall object at
the specified position
/// </summary>
/// <param
name="centerPosition">position to create the soccer object at.</param>
public
SoccerBall(Vector2 centerPosition)
{
m_TheBall = new
XNACS1Circle(centerPosition,
RADIUS);
m_TheBall.Texture =
"SoccerBall";
// set the soccer in motion!
m_TheBall.ShouldTravel = true;
m_TheBall.VelocityX = INIT_VELOCITY_X;
m_TheBall.VelocityY = INIT_VELOCITY_Y;
} |
m_TheBall.VelocityX = INIT_VELOCITY_X;
m_TheBall.VelocityY = INIT_VELOCITY_Y;
|
///
<summary>
/// Make
sure the soccer ball does not go outside of the bound
///
</summary>
public void
UpdateBall()
{
BoundCollideStatus status
= XNACS1Base.World.ClampAtWorldBound(m_TheBall);
switch (status)
{
case
BoundCollideStatus.CollideBottom:
case
BoundCollideStatus.CollideTop:
m_TheBall.VelocityY = -m_TheBall.VelocityY;
break;
case
BoundCollideStatus.CollideLeft:
case
BoundCollideStatus.CollideRight:
m_TheBall.VelocityX = -m_TheBall.VelocityX;
break;
} }
|
FURTHER EXERCISES::