Extending Point Pong

Let's extend the Point Pong code.  If we add a few more lines of code to the Point Pong, mostly if statements, we will be able to move our colorball around in the game space or world.

The current state of the Point Pong is that you have to modify variables and the build and run the program.  Hopefully you have given this a try.  It would be more interesting to move the colorball around using the keyboard.  To do this you will need to add the following code to your program.  First you have to add a class constructor to instantiate the build-in structure to read the KeyboardState, in this structure the structure is constructed using a class name that is the same as the built-in structure name.  What is the difference between a structure and a class?  This will be discussed in another posting. 

To use the code, take a look at the video at the end of this entry:

namespace PointPong
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        //********************************************************
        //Create an object type of variable to hold the Pong Ball 

        Texture2D t_Ball;

        Ball Ball;

//Add: " keyboardState keyboardState; " as shown below, you should have the code above already in your code.

//*********ADD The following CODE*****************************

KeyboardState keyboardState;

//**************************************

//**************************************

//  Then you can the following "method" to your code, this will allow your class module to call this method when needed

//  The "if" statement tests to see if there is a key pushed down (there another test for the key positions, "IsKeyUp")

//  the "Keys.Left" and so forth are actually measuring that the arrow keys are being pressed

//  Note: that the "Ball.pos.X", "Ball.pos.Y" move the ball moves in a negative direction when the Left arrow and the Up arrow keys are pressed.

//  Note: That the "Ball.pos.X", "Ball.pos.Y" move the ball in a positive direction when the Right arrow and Down arrow keys are pressed.

//  The initial position is set-up in the method you added in the last exercise called "Reset Game"

//The method call "CheckBounds();" calls the next method that you will add to your code.

void MoveBall()
        {
            keyboardState = Keyboard.GetState();

            if (keyboardState.IsKeyDown(Keys.Left))
                Ball.pos.X -= 5;
            if (keyboardState.IsKeyDown(Keys.Up))
                Ball.pos.Y -= 5;
            if (keyboardState.IsKeyDown(Keys.Right))
                Ball.pos.X += 5;
            if (keyboardState.IsKeyDown(Keys.Down))
                Ball.pos.Y += 5;

            CheckBounds();
        }

//**************************************

// Each time the method "MoveBall()" is called, "MoveBall()" checks to see if the keyboard has been used, then moves the

// colorball image around.  Once "MoveBall()" reaches the end of the if statements, and every one of the if statements are checked

// that can be a problem, but not in this case, then at the end of the "if" statements the "CheckBounds()" method is called. 

// The "CheckBounds()" method examines the location of the colorball if the colorball has passed the edge of the game space

// then it is moved back to the game space.  Give it a try, have fun!

void CheckBounds()
       {
           if (Ball.pos.X < 0)//check left side
               Ball.pos.X = 0;

           if (Ball.pos.X > 720)//check right side
               Ball.pos.X = 720;

           if (Ball.pos.Y > 525)//check bottom
               Ball.pos.Y = 525;

           if (Ball.pos.Y < 0)//check top
               Ball.pos.Y = 0;
       }