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

The left thumbstick controls the pink/blue 'spot' on the screen (shown here near the bottom of the screen, beneath the soccerball). 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. Note that when there's no soccerball on the screen, rolling the soccer ball does nothing. While this seems like a minor, obvious point, it's important that the program not crash when the user attempts to roll the non-existent soccerball. This is fairly easy to implement, but a very important detail to keep in mind.
2. SoccerBall.cs:
The code in this class is almost exactly the same as the prior tutorial. The only two differences are that
|
///
<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";
} |
2. Game1.cs:
There are several changes that are being made here:
|
protected
override void
InitializeWorld()
{
World.SetWorldCoordinate(new
Vector2(0, 0), 100.0f);
// Initially only the spot
m_TheBall = null;
m_TheSpot = new
XNACS1Circle(new
Vector2(INIT_X, INIT_Y),
SPOT_RADIUS); }
|
m_TheBall = null;
if (m_TheBall ==
null)
{
}
|
protected
override void
UpdateWorld()
{
if (GamePad.ButtonBackClicked())
this.Exit();
m_TheSpot.Center +=
GamePad.ThumbSticks.Left;
if (GamePad.ButtonAClicked())
{
if (null
!= m_TheBall)
m_TheBall.RemoveFromDraw();
m_TheBall = new SoccerBall(m_TheSpot.Center);
}
if (null
!= m_TheBall)
m_TheBall.RollTheSoccer(GamePad.ThumbSticks.Right.X);
EchoToTopStatus("Left Thumb Stick to
move the red spot, right thumb stick to roll the ball");
EchoToBottomStatus("Current Spot
position:" + m_TheSpot.Center); }
|
if (GamePad.ButtonAClicked())
{
if (null
!= m_TheBall)
m_TheBall.RemoveFromDraw();
m_TheBall = new SoccerBall(m_TheSpot.Center);
}
if (null
!= m_TheBall)
m_TheBall.RemoveFromDraw();
m_TheBall = new SoccerBall(m_TheSpot.Center);
FURTHER EXERCISES::
if (null
!= m_TheBall)
m_TheBall.RollTheSoccer(GamePad.ThumbSticks.Right.X);
In the code listed below, remove the if statement (for
example, comment it out), compile and run the program, and see what error
message you get:
if (null
!= m_TheBall)
m_TheBall.RemoveFromDraw();