This hands on exercise is based on a demo by Sam Stokes called Pong in Five Minutes. I appreciate Sam’s help in this effort. You can watch Sam’s demo in a video and get the code and other files used in this lab from http://blogs.msdn.com/devschool/archive/2008/02/03/pong-in-5-minutes.aspx  If you want these materials more easily available. So I have uploaded them to Skydrive at the links below.

Includes:

• Step by step instructions
• Code Snippits
• graphical content

Open a new Windows Game Project from the XNA Section for the C# language in Visual Studio 2008.

Select Content from the Solution Explorer and choose the Add Existing Item option for the Project menu.

Navigate to where you have saved the image files and add them to the project.

Set the Copy to Output Directory property of both images to “Copy Always”

Now we are going to add two classes to do the work of the game. We will add a ball class and a paddle class. First add the Ball class by selecting Add Class from the Project menu. This opens a window where we can name the class Ball.

Enter the following code for the Ball class.

```       class Ball
{
//position of ball
public Point pos;
public int h_speed, v_speed;

//constructor - position
public Ball(int x, int y)
{
pos = new Point(x, y);

Random rand = new Random();
h_speed = rand.Next(3, 7);
if (rand.Next(0, 2) == 0) h_speed *= -1;

rand = new Random();
v_speed = rand.Next(3, 7);
if (rand.Next(0, 2) == 0) v_speed *= -1;
}
}```

Now create the Paddle class the same way and enter the following code for the paddle.

```    class Paddle
{
public Point pos;
public int speed;

//constructor - position
{
pos = new Point(x, y);
speed = 3;
}
}```

Add a call to the Microsoft.XNA.Framework libraries to the top of the the Ball and Paddle class files so that these libraries will be available.

Return to the Game class and define the variables and objects we need for the game.

```   public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;

SpriteBatch spriteBatch;

Ball ball;

Random rand = new Random();
KeyboardState currentState;

Now let’s create an UpdateBall class to move the ball around the screen.

```
void UpdateBall()
{
//update positions
ball.pos.X += ball.h_speed;
ball.pos.Y += ball.v_speed;

//check for boundaries
//bottom
if (ball.pos.Y > (Window.ClientBounds.Height - 10 - t_ball.Height))
ball.v_speed *= -1;

//top
if (ball.pos.Y < 10)
ball.v_speed *= -1;
}```

```     void UpdatePaddles()
{
//get keyboard keys
currentState = Keyboard.GetState();

//check for up and down arrow keys

//paddle to move according to ball

//check boundaries

}```

In the Initialize method that was created for us automatically we have to add a call to ResetGame to start our game.

```        protected override void Initialize()
{
ResetGame();
base.Initialize();
}```

```        protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.

t_ball = Texture2D.FromFile(graphics.GraphicsDevice, @"content\ball.png");
spriteBatch = new SpriteBatch(graphics.GraphicsDevice);

}```

Lastly we need to add code to the Update method to call the methods that make the game run.

```        protected override void Update(GameTime gameTime)
{
// Allows the game to exit