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

For this tutorial, there is a single paddle on the left side of the screen, which is controlled by the left thumbstick. The pink spot is controlled by the right thumbstick; when the 'A' button is pressed, a soccer ball appears and immediately begins to move across the screen. The soccer ball will bounce of the screen's edges, and the paddle.
The major objective of this tutorial is to examine how multiple objects, each one made from a different class, can be made to interact with each other. In this case, there's a class for the SoccerBall, and a separate class for the Paddle.
SoccerBall.cs:
For this tutorial, we will mostly leave the SoccerBall class unchanged. The only major difference between the SoccerBall class in this tutorial and the previous tutorial is that we've removed MoveTheBall and RollTheSoccer methods that we no longer use (along with the ROLL_ANGLE and MOVE_UNIT constants that they use). We'll also add the GetCircle method, which Paddle objects will use to determine if the SoccerBall's circle overlaps/collides with the Paddle.
|
///
<summary>
///
///
</summary>
///
<returns>Returns
the circle representing the soccer for interaction.</returns>
public
XNACS1Circle GetTheCircle()
{
return m_TheBall;
} |
2. Game1.cs:
This class is also mostly unchanged from the prior tutorial. The only major change is for the Paddle object has been added to the program. In order to do this the PADDLE_X constant and the m_ThePaddle instance variable have been added to the class. The variable is initialized in the InitializeWorld method, similarly to what we did for the SoccerBall class.
UpdateWorld has been changed slightly, as well:
|
protected
override void
UpdateWorld()
{
if (GamePad.ButtonBackClicked())
this.Exit();
m_TheSpot.Center +=
GamePad.ThumbSticks.Right;
m_ThePaddle.UpdatePaddlePosition(GamePad.ThumbSticks.Left.Y);
if (GamePad.ButtonAClicked())
{
if (null != m_TheBall)
m_TheBall.RemoveFromDraw();
m_TheBall = new
SoccerBall(m_TheSpot.Center);
}
if (null
!= m_TheBall)
{
m_TheBall.UpdateBall();
m_ThePaddle.BounceTheSoccer(m_TheBall);
}
EchoToTopStatus("Left Thumb Stick to
move the left paddle, right thumb stick to move the red spot");
EchoToBottomStatus("Current Spot
position:" + m_TheSpot.Center); } |
m_ThePaddle.UpdatePaddlePosition(GamePad.ThumbSticks.Left.Y);
if (null
!= m_TheBall)
{
m_TheBall.UpdateBall();
m_ThePaddle.BounceTheSoccer(m_TheBall);
}
3. Paddle.cs:
You'll notice that the Paddle class is cery similar to the SoccerBall class - it has several named constants, an instance variable to keep track of the on-screen rectangle, and a constructor that has a parameter for the X part of the paddle's initial (X,Y) position. What's interesting are the UpdatePaddlePosition and BounceTheSoccer methods.
|
///
<summary>
/// Allow
paddle up/down movement
///
</summary>
///
<returns></returns>
public void
UpdatePaddlePosition(float dy)
{
m_Rec.CenterY += dy;
XNACS1Base.World.ClampAtWorldBound(m_Rec);
}
|
|
///
<summary>
/// Bounce
the soccer ball.
///
</summary>
///
<returns>true
if paddle has bounced soccer, otherwise false</returns>
public bool
BounceTheSoccer(SoccerBall
theBall)
{
XNACS1Circle theBallCircle
= theBall.GetTheCircle();
bool hit = m_Rec.Collided(theBallCircle);
if (hit)
{
theBallCircle.VelocityX = -theBallCircle.VelocityX;
// now move the circle to outside
of the paddle
theBallCircle.CenterX = m_Rec.MaxBound.X +
theBallCircle.Radius;
}
return hit; }
|
We can't ask the SoccerBall directly if has collided with the Paddle. Instead, we have to get the SoccerBall's XNACS1Circle, and see if that circle has collided with the paddle's XNACS1Rectangle. On the first line of the method, get obtain a reference to the SoccerBall's circle object using the previously described GetTheCircle method:
XNACS1Circle theBallCircle =
theBall.GetTheCircle();
Next, we'll see if the soccer ball's circle has collided with the
paddle's rectangle, using the Collided method that is built into the
rectangle. We will store the result (true if the two objects have
collided, false otherwise) into the local variable hit, so that we can
return it at the end of the method.
bool hit = m_Rec.Collided(theBallCircle);
If the ball has collided with the paddle, then we will reverse the horizontal speed of the ball, and then make sure that the ball is moved rightwards so that it no longer overlaps with the paddle.
if (hit)
{
theBallCircle.VelocityX = -theBallCircle.VelocityX;
// now move the circle to outside of
the paddle
theBallCircle.CenterX = m_Rec.MaxBound.X + theBallCircle.Radius;
}
return hit;
FURTHER EXERCISES: