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.
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.