Creating a Pong Game | CodeGuru

Creating a Pong Game

Pong: one of the simplest possible games to make. If you’re doing an assignment, or just want to start making games, this tutorial will get you up and running with a working game ASAP. It assumes you know how to add controls in the designer and switch between the designer and code views. 1. Starting […]

Written By
CodeGuru Staff
CodeGuru Staff
Jun 5, 2008
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Pong: one of the simplest possible games to make. If you’re doing an assignment, or just want to start making games, this tutorial will get you up and running with a working game ASAP.

It assumes you know how to add controls in the designer and switch between the designer and code views.

1. Starting Up in the Designer

Make three picture boxes (one player paddle, one computer paddle, one ball). Name and position them accordingly: playerPaddle at the bottom, computerPaddle at the top, ball in the centre. Add a timer with an Interval value of 50, and an Enabled value of true. Double-click the timer.

2. Moving the playerPaddle

Now, add the following:

playerPaddle.Location = _
   Me.PointToClient(New Point(MousePosition.X, _
   Me.PointToScreen(playerPaddle.Location).Y))

This will move the bottom paddle to be in line with the mouse every update. If you run it now, you should have a controllable bottom paddle.

Me.PointToClient converts the Sceen Coordinates of MousePosition.X to Form1 Coordinates. This means that it won’t affect the gameplay if you move the form around.

3. Declaring Ball Speed Variables

Add this code directly to your namespace (not in the timer event handler):

Dim ballXSpeed As Single
Dim ballYSpeed As Single

This sets aside two variables for use later on.

Advertisement

4. Initialising Ball Speed Variables

Go back to the designer and double-click the form. In the Form1_Load event handler, add:

ballXSpeed = 0
ballYSpeed = -5

This initialises the ball speed variables. It will immediately move in the negative Y direction (towards the computer).

5. Applying Ball Speed Variables

Back in the timer event handler, add:

ball.Location = New Point(ball.Location.X+Math.Round(ballXSpeed), _
   ball.Location.Y+Math.Round(ballYSpeed)

This adds the balls X and Y speeds to the ball’s location every frame. If you run the program now, you should see the ball fly off the top of the screen.

6. Determining Collision with the playerPaddle

This is more complex. You need to check whether or not the ball’s position is within the paddles bounding box. To do this for the player’s paddle, add:

'Checks that the ball's X position is within the left/right edges
'of the paddle
If ball.Location.X < playerPaddle.Right And ball.Location.X > _
   playerPaddle.Left Then
'Checks that the ball's Y position is within the top/bottom edges
'of the paddle
   If ball.Location.Y < playerPaddle.Bottom And ball.Location.Y > _
      playerPaddle.Top Then
      'A collision has occurred
   End If
End If

Read this code thoroughly to understand how it works before you continue.

7. Acting on Collision with the playerPaddle

When the ball collides with the playerPaddle, you want it to go up, towards the computerPaddle. Hence, you modify the ballYSpeed within the collision check:

'A collision has occurred
   ballYSpeed = -10
   ballXSpeed = New Random(DateTime.Now.Millisecond).Next(-10,10)

This also randomises the X speed of the ball, to make things more interesting. The new Random class takes DateTime.Now as a seed, to prevent it always returning the same pseudorandom number everytime it is initialised.

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.