It is currently Wed Feb 22, 2012 7:38 pm

All times are UTC


Microsoft XNA Tutorial 3: The Player & Movement
In this tutorial we cover the player and the movement. By: xxxgamer

  

 

If you need the project files to continue you can download them in the last tutorial.

In this tutorial we will cover creating the player paddle and the movement associated with it. We will be using the keyboard controls for now as it is easier to debug and then later change it to the phone acceleration controls.

So first start of inside the Pong.cs file and find the line where we create the enemy paddle: "Paddle EnemyPaddle;"

Below that add our new player paddle:

Code:
//Variable for the players paddle
Paddle Player;


Now find the Initialize() method and after initializing the enemy paddle add:

Code:
//Initialize player paddle
Player = new Paddle();


Now inside the LoadContent() method we have to create a new variable to hold the players position but we can use the paddleTexture again.

Find the line: EnemyPaddle.Initialize(paddleTexture, paddlePosition);

And after add the following:

Code:
//Initialize the player
Vector2 playerPosition = new Vector2(GraphicsDevice.Viewport.TitleSafeArea.X + 20, GraphicsDevice.Viewport.TitleSafeArea.Y + GraphicsDevice.Viewport.TitleSafeArea.Height / 2);
Player.Initialize(paddleTexture, playerPosition);


Now all thats left to do is request the draw method of the paddle class inside the Draw Method. So add the following inside the Draw() method after "EnemyPaddle.Draw(spriteBatch);"

Code:
//Draw the player paddle
Player.Draw(spriteBatch)


Now that we are drawing the players paddle we need to get keyboard input to move the paddle once we register a pressed button.

We will create a new method for this and call it from inside the update method.

Go back to the top of our Pong class and find the line where we create the player variable "Paddle Player;"

After this add the following:
Code:
// Keyboard states used to determine key presses
KeyboardState currentKeyboardState;
KeyboardState previousKeyboardState;

float moveSpeed;


Now inside the Initialize() method we have to initialize the moveSpeed variable we have just created. Scan down the method and find where we created the Player: "Player = new Paddle();"

and add the following underneath:

Code:
//Set the move speed of the paddles
moveSpeed = 10.0f;


Now we will create a new method to add into our Pong class. To do this we have to find some empty space inside the class. To do this look at the bottom of the class and look for the last 2 } characters.

Now go one line up from the second last one and you should be in a safe area :).

Now add the following there:

Code:
//This is the method which handles input to move the player paddle
private void movePlayer(GameTime gameTime)
{
       if (currentKeyboardState.IsKeyDown(Keys.Up))
       {
             Player.Position.Y -= moveSpeed;
        }
        if (currentKeyboardState.IsKeyDown(Keys.Down))
        {
             Player.Position.Y += moveSpeed;
         }

         //This makes sure the player paddle doesnt go off the screen
         Player.Position.Y = MathHelper.Clamp(Player.Position.Y, 0, GraphicsDevice.Viewport.Height - Player.Height);
}


Now it may be a bit confusing at first but it is very simple. All we are doing is checking if keyboard Up or Down keys are pressed and if so then we change the players position by the movement speed. Then we make sure that the player doesn't go out of bounds using a MathHelped.clam() function.

Now that we have our function created we need to call it somewhere. All our updates are done within the Update() function so proceed to that and find the line which says "TODO: Add your update logic here" after that add:

Code:
//Save previous keyboard presses so we can determine single presses
previousKeyboardState = currentKeyboardState;

//read the current keyboard state and store it
currentKeyboardState = Keyboard.GetState();

//Now call our method to update the player paddle
movePlayer(gameTime);


Now you should be ready to compile and run the game without any errors. Make sure when you run the emulator you press "Page Up" on your keyboard to enable it within the emulator.

So you should end up with the following:

Image

Now if you use the up and down arrow keys on your keyboard the left player paddle should move up and down. Again if you are having problems you can download the full project file to compare to yours here:


Add Comment:
You must be logged in to place a comment!
Page 1 of 1
Be the first to comment on this tutorial!
Page 1 of 1

Return back to the Tutorials