| 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 game in this tutorial is very similar to the prior tutorial - there are two paddles on the screen, and the ball bounces on it's own. The right thumbstick moves the right paddle up or down and the left thumbstick controls the left paddle. The ball can also bounce off the Block in the middle of the screen, which keeps track of how many times it's been hit. In this tutorial, youll notice that the paddles behave like Blocks that the player can move - they will keep track of how many times the ball has hit each paddle, place themselves at a random height when the game starts, etc.
The paddles behave like Blocks the players can move because the major change in this tutorial is to have the Paddle class inherit from the Block class. As you can see, a Paddle is really just a Block that responds to user input, and that's how we'll set up the code.
1. SoccerBall.cs, Block.cs:
These are effectively identical to what was presented in the prior tutorial. We'll make the radius of the soccer ball a bit smaller, since there's now more things on the screen, but that's about it.
2. Paddle
The single most important change is that the Paddle now inherits all the methods and instance variables (data fields) from the Block class. As we refactor the code in order to make that change work, we'll end up changing a bunch of relatively small things, but the most important thing to keep in mind is that our changes are done with the intention of making the Paddle inherit from the Block.
|
///
<summary>
/// Class:
Paddle, this class encapsulate the functionality of a rectangular
paddle
/// that
are capable of bouncing a SoccerBall.
///
</summary>
public
class Paddle
: Block
{
|
The most important change to the code in this tutorial is also the smallest. We tell the C# compiler that we want the Paddle class to inherit from the Block class by adding : Block after public class Paddle, and BEFORE the opening curly brace ( { ).
After this change is made, the Paddle will now have an (invisible) copy of all the methods and instance variables that the Block class declares.
|
#region Constants for the
paddle
// width and height of the paddle
private
const float
PADDLE_WIDTH = 1f;
// width of a paddle
private
const float
PADDLE_HEIGHT = 10.0f; // height of a
paddle #endregion
|
You'll notice that we've removed the PADDLE_POSITION_Y constant. Instead of creating the paddles at a given height, we use the Block's constructor to randomly pick a height for each of the paddles.
Also, you'll notice that the Paddle class now has NO instance variables. This is because the base class (Block) already declares an instance variable to keep track of the on-screen rectangle. Since the Paddle class gets a free copy of that instance variable, there's no need to declare another one here.
|
///
<summary>
///
Create the a paddle at xPos
///
</summary>
///
<param name="xPos">X-Position
of the paddle.</param>
public Paddle(float
xPos)
: base(xPos)
{
m_Rec.Texture = null;
m_Rec.Width = PADDLE_WIDTH;
m_Rec.Height = PADDLE_HEIGHT; }
|
The first change is that after a Paddle object is created, and before any of the code in this class's constructor is called, we want to call the base-class constructor (i.e., the constructor on the Block class). We do this with the : base(xPos), that we've added after the parameter list.
This will cause the Block class's constructor to be run on the current Paddle object. Refering back to the Block.cs file, we can see that the constructor will initialize the hit count to zero, the 'active' status of the paddle to true, and then randomly pick a height to create the on-screen rectangle.
Once that's done, the program's execution resumes here. In this method, we remove the rectangle's texture (by setting it to null) so that the paddles look like colored rectnagles, rather than looking like a Block. Also, we re-set the width and height of the rectangle to be the proper dimensions for a Paddle (instead of a Block)
|
|
The biggest change in the Paddle class is that we've completely removed the BounceTheSoccer method. This is because the Block class already has a method named CollideWithSoccer that does the exact same thing. Since we don't need (or want) two methods that do the same thing, we'll remove the duplicate method here in the derived class.
3. Game1.cs:
As you can see, this file requires almost no changes:
|
m_TheBall.UpdateBall();
m_LeftPaddle.CollideWithSoccer(m_TheBall);
m_RightPaddle.CollideWithSoccer(m_TheBall);
m_TheBlocker.CollideWithSoccer(m_TheBall);
|
Since the BounceTheSoccer method on the Paddle has been replaced with the (inherited) CollideWithSoccer method, all we need to do is replace the BounceTheSoccer with CollideWithSoccer in UpdateWorld
FURTHER EXERCISES::