This guide will show you how to make a simple pong game and I will add some features to make it a bit more advanced at the end.
This is in Visual Basic 6
So first we need to create the form so we will need
Form Size:
Width = 7545
Height = 5295
This is quite important because we need to check if the ball passes a certain point so if you have different Height and Width then you will need to change the code a bit later on
Actually I will add a form to download with no code just to make it easier
DOWNLOAD
MEGAUPLOAD - The leading online storage and file delivery service
3 shapes:
2 rectangles called
Paddle1
paddle2
1 circle call this
Ball
2 Labels
1 called LBLp1Score
1 called LBLp2Score
the rest of the form can be designed by you mine looks like:


But for now ignore the bottom but with the level and the time.
So after we design the form we start coding
First we will start with the global variables
Code:
Option Explicit
Dim Horizontal As Integer
Dim Vertical As Integer
Dim nStep As Long
These are used to make the ball move we will be using these very soon
Ok now we need to make a Timer, I just left mine being Called Timer1 but you can call it something more relevant,
Ok now in the "Form Load" put:
Code:
Horizontal = 1
Vertical = -1
nStep = 40
These just add values to the variables,
Horizontal = 1 'This controls the speed of the ball moving left to right
Vertical = -1 'This controls the speed of the ball moving up and down
nStep = 40 'This controls how may spaces it moves each time
these values can be changed if you want but the way there set now makes it look a lot smoother.
Ok now back into the Timer1 we need to set:
interval to 5
Enabled to true
and now start to add the code for Timer1
This is just for a message box later in the code i use X but you can use what ever
Next we do:
Code:
ball.Left = ball.Left + (nStep * Horizontal)
ball.Top = ball.Top + (nStep * Vertical)
This makes the ball start moving but it will just go through the wall and carry on going so we also need to add an if statement to make it change direction on collision with the walls
to do this we add
Code:
If ball.Left <= 0 Or ball.Left + ball.Width >= Me.ScaleWidth Then
Horizontal = Horizontal * -1
End If
If ball.Top <= 0 Or (ball.Top + ball.Height) >= Me.ScaleHeight Then
Vertical = Vertical * -1
End If
this just says if the ball touches the wall then change the direction the ball is going
There is one other thing we need to bounce the ball off when it comes in contact with it and that is the paddles
So also in the timer we add:
Code:
If ball.Top >= Paddle1.Top And _
ball.Top <= Paddle1.Top + Paddle1.Height And _
ball.Left <= Paddle1.Left + Paddle1.Width Then
Horizontal = Horizontal * -1
End If
If ball.Top >= paddle2.Top And _
ball.Top <= paddle2.Top + paddle2.Height And _
ball.Left + ball.Width >= paddle2.Left + paddle2.Width Then
Horizontal = Horizontal * -1
End If
These two If statements are the same just for the two different paddles so lets have a look at the code
If ball.Top >= Paddle1.Top And _
ball.Top <= Paddle1.Top + Paddle1.Height And _
ball.Left <= Paddle1.Left + Paddle1.Width
This just says if the ball .Top AND .Left is ever the same as the Paddle to execute the if statement
Horizontal = Horizontal * -1
End If
This is the executed bit which we have seen before it just changes the direction of the ball after its hit the paddle
The next part in the Timer is for score keeping and detecting if the ball ever passes the paddle
Code:
If ball.Left < 100 Then
x = MsgBox("Player 2 Scores!!", vbOKOnly, "Score!")
Paddle1.Top = 1560
ball.Left = 3480
LBLp2score.Caption = LBLp2score.Caption + 1
ElseIf ball.Left > 7000 Then
x = MsgBox("Player 1 Scores!!", vbOKOnly, "Score!")
paddle2.Top = 1800
ball.Left = 3480
LBLp1score.Caption = LBLp1score.Caption + 1
End If
Ok so lets take a little llook at the code:
If ball.Left < 100 Then
Oviously just checks if the Ball.Left is left that 100 and if it is execute the rest of the code
x = MsgBox("Player 2 Scores!!", vbOKOnly, "Score!")
Just tells the user that player 2 has scored and also change the X to what you used at the start of the timer if you used a different one
Paddle1.Top = 1560
ball.Left = 3480
This just resets the ball to a place around the middle
LBLp2score.Caption = LBLp2score.Caption + 1
This just adds 1 to the player2s score label
k Now we also need to do it for player one so we can either do it at an other if or just use Elseif
ElseIf ball.Left > 7000 Then
Same as the begining of the first if but just checks if its greater than 7000
The rest is just the same as the first if just for player1 and paddle1
ok were almost done just 1 last things and you should be able to play a basic pong game
Click on the form so you get your form load up then where it says "Load we need to change it to Key down
and in that we need to add
Code:
If KeyCode = vbKeyZ Then
Paddle1.Top = Paddle1.Top + 450
ElseIf KeyCode = vbKeyA Then
Paddle1.Top = Paddle1.Top - 450
ElseIf KeyCode = vbKeyUp Then
paddle2.Top = paddle2.Top - 450
ElseIf KeyCode = vbKeyDown Then
paddle2.Top = paddle2.Top + 450
If KeyCode = vbKeyZ Then
Paddle1.Top = Paddle1.Top + 450
this just says if the key a is press move the paddle 450 spaces up
and the same but down for z
so now we can move the paddles player1 uses A and Z and Player2 uses Up and Down
ok thats all for now i will be adding a few more features like stopping after 5 goals and levels
any problems ask: