In this tutorial we will be creating a "paddle" class which will hold all the information for the player paddle and the AI paddle.
We will then use this "paddle" class as a base for the player itself. We will extend the paddle class and then build the functions we need ontop to get all the functionality for the player such as movement.
Start off by selecting "Game1" inside the code of the Game1.cs file and right clicking > Refactor > Rename.
Rename the class to "Pong" and click ok.
In the next window make sure all classes are selected and then click on apply:
Now right click in the project view on the "game1.cs" file and click on rename. Rename this file to "Pong.cs".
Now that we have everything organised. Right click on our "pong" project and select "Add > Class".
Name the class "Paddle.cs" and click on ok. You will now have a new class with some code allready created for you. You will have to make some changes to this code to make it look like the following:
Code:
using System; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics;
namespace Pong { class Paddle { public void Initialize() { }
public void Update() { }
public void Draw() { } } }
We have now added 3 methods or functions which we will be accessing to initialize(), update() and draw our object to the screen. First we need to create some variables to hold/store data that we need to use later on.
After the first { under class Paddle add the following variables:
Code:
//Texture for each paddle public Texture2D PaddleTexture;
//Position of the paddle object public Vector2 Position;
//Score of the "paddle" object public int Score;
//Width of the paddle public int Width { get { return PaddleTexture.Width; } }
//Height of the paddle public int Height { get { return PaddleTexture.Height; } }
So we have now created variables to hold the paddle texture, its position, its score and we have created another 2 functions to return the textures width and height.
Now change the Initialize() method to look like the following:
Code:
public void Initialize(Texture2D texture, Vector2 position) { //Set the texture of the paddle PaddleTexture = texture;
//Set the starting position Position = position;
//Set the player score when first initialized Score = 0; }
So we are now passing a texture and a position as parameters to our paddle object when we initialize it. We can now use those values to draw our paddle object to the screen.
Replace the Draw() method to look like the following:
So here we are calling a "SpriteBatch" class which comes with XNA to handle drawing graphics to the screen. The spritebatch class has a "Draw()" method which we use. We pass the texture and the position of our object into it.
Dont worry about the rest of the arguments for now as we will not need to make use of them for this tutorial.
So we are done with our "Paddle.cs" class for now and are going to create our paddle object within our main game file.
Switch over toe "Pong.cs"
Inside this class you can see we allready have a lot of code created for us, This class is the main loop of our pong game. It is in this class where we will create our Paddle object and call the methods to draw it on the screen.
At the top of the code find the line: public class Pong : Microsoft.Xna.Framework.Game now under this find the line SpriteBatch spriteBatch;. Now under this add the following variable to hold our paddle.
Code:
//Variable for the enemy paddle Paddle EnemyPaddle;
Look down further for a method called protected override void Initialize() and inside this method add the following code:
Code:
//Initialize the paddle EnemyPaddle = new Paddle();
So we have now created a "Paddle" variable called EnemyPaddle and have assigned it to be a new copy off "Paddle()".
Next we will need to load the contents associated with the paddle class so we can initialize it properly.
Download the following image which will represent our paddle texture, You can replace this later on yourself with anything you like. For this tutorial I will just be using simple white "blocks" to represent the paddles and the ball.
We have to load this texture into Visual studio first so inside your project where it says "PongContent" right click and add a new folder and call it "Images".
Now drag the image you just downloaded above into the "Images" folder so it looks like the following:
We are now ready to load the image in our code and pass it to our paddle initialize method.
Look for a method called protected override void LoadContent(). Inside this method below the spritebatch creation add the following code:
Code:
//Load the paddle resources we need Vector2 paddlePosition = new Vector2(GraphicsDevice.Viewport.TitleSafeArea.Width - 30, GraphicsDevice.Viewport.TitleSafeArea.Y + GraphicsDevice.Viewport.TitleSafeArea.Height / 2); Texture2D paddleTexture = Content.Load<Texture2D>("Images/paddle");
//Initialize the paddle EnemyPaddle.Initialize(paddleTexture, paddlePosition);
Now that we have our paddle initialized we are ready to draw it as we have all the data we need. Look down the code and find the method called: protected override void Draw(GameTime gameTime) and add the following code underneath GraphicsDevice.Clear(Color.CornflowerBlue);
Code:
//Start the drawing loop spriteBatch.Begin();
//Draw the enemy paddle EnemyPaddle.Draw(spriteBatch);
//End the drawing loop spriteBatch.End();
Take note of the "spriteBatch.Begin()" and "spriteBatch.End()" any other drawings that we will do will be done between those lines of code as that is the draw loop.
Running the game
Click on the green "Debug" button again to run your game, It should compile without any errors. You should now see the following:
So we now have our game running with the enemy paddle drawing on the right side of the screen.
In the next tutorial we will create the player paddle and move it using the keyboard controls.
DOWNLOAD PROJECT FILES: If you are having problems getting your code to compile please feel free to download the project here and compare it with your code: