Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

[TUT] How to create a Pong Game in VB.NET

What we're gonna do?

In this tutorial I'm gonna show you how to create a Pong Game in vb.net.

What will we need?

Windows Compute

Visual Basic 2008/2010

Free Time 👍

Lets Start

Step 1) Create a new Windows Form and rename it to whateveryouwant. Mine will be called PongZ.

Step 2) Add two labells, two panels, a picturebox, a timer and display them as the folowing image.

[IMG]http://img844.imageshack.us/img844/6867/semttulovvp.th.png[/IMG]

Step 3) Rename the left label to "ComputerScore", rename the right label to "PlayerScore", rename the left panel to "Computer", rename the right panel to "Player" and rename the PictureBox to "Ball".

Step 4) Now, lets make the "Player Panel" move according to the mouse position.

Code
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
  If e.Y > 5 And e.Y < Me.Height - 40 - Player.Height Then _
  Player.Location = New Point(Player.Location.X, e.Y)
End Sub

If you test your game now you'll note that the ball doesn't move. If you want to make that piece of sh#$ we call ball move, read step 5. 😝

Step 5) Make the ball move by adding the folowing code to your form (GV).

Code
Dim speed As Single = 10 ' This is Ball Speed
Dim rndInst As New Random() ' Random instance
Dim xVel As Single = Math.Cos(rndInst.Next(5, 10)) * speed
Dim yVel As Single = Math.Sin(rndInst.Next(5, 10)) * speed

And add this code to the Timer you just added on the begining of tutorial.

Code
Ball.Location = New Point(Ball.Location.X + xVel, Ball.Location.Y

Step 6) Now, lets make the ball bounce when colides with "everything". Its easier than what you might be thinking.

Add the folowing code to the Timer1 (i could be here saying to add an individual code to every wall and panel but I just put all together).

Code
' Colides with top wall.
  If Ball.Location.Y < 0 Then
    Ball.Location = New Point(Ball.Location.X, 0)
    yVel = -yVel
  End If
  ' Colides with down wall.
  If Ball.Location.Y > Me.Height - Ball.Size.Height - 45 Then
    Ball.Location = New Point(Ball.Location.X, Me.Height - Ball.Size.Height - 45)
    yVel = -yVel
  End If
  ' Colides with player panel.
  If Ball.Bounds.IntersectsWith(Player.Bounds) Then
    Ball.Location = New Point(Player.Location.X - Ball.Size.Width, _
    Ball.Location.Y)
    xVel = -xVel
  End If
  ' Colides with computer panel.
  If Ball.Bounds.IntersectsWith(Computer.Bounds) Then
    Ball.Location = New Point(Computer.Location.X + Computer.Size.Width + 1, _
    Ball.Location.Y)
    xVel = -xVel
  End If

Step 7) Now lets make the computer play like an intelligent player. Add the folowing code to Timer1.

Code
'Make the computer moves according to ball position
  If Ball.Location.Y > 5 And Ball.Location.Y < Me.Height - 40 _
  - Player.Height Then _
  Computer.Location = New Point(Computer.Location.X, Ball.Location.Y)

After you add that code the computer will be a master playing pong and you will never beat him.

Don't be sad! You can always add another timer and set the computer speed so you can beat him.

But, for now we just want to make a playable game.

Step 8) Lets set our Score System.

You need to add two global variables in order to give score.

Code
Dim compScore As Integer = 0
Dim plrScore As Integer = 0

Lets give some use to those gv. Add the folowing code to timer1.

Code
'Add 1 score to player if ball reaches the left side of the field
  If Ball.Location.X < 0 Then
  plrScore += 1
  : Ball.Location = New Point(Me.Size.Width / 2, Me.Size.Height / 2)
  PlayerScore.Text = Convert.ToString(plrScore)
End If

'Add 1 score to computer if ball reaches the right side of the field
If Ball.Location.X > Me.Width - Ball.Size.Width - Player.Width Then
    compScore += 1
    Ball.Location = New Point(Me.Size.Width / 2, Me.Size.Height / 2)
    : ComputerScore.Text = Convert.ToString(compScore)
End If

Step 9) Lets hide our cursor.

Code
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  Cursor.Hide()
End Sub

Step 10) Now lets add a key to close our game [ESC].

Code
Private Sub From1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
  If e.KeyValue = Keys.Escape Then
    Me.Close()
  End If
    End Sub

Full Source Code Spoiler (Click to View)

Our game is completed now!

[IMG]http://img704.imageshack.us/img704/3808/77722840.png[/IMG]
Very nice! I did not know you could make any type of games in VB.NET.
(Aug 26, 2010 04:29 AM)Shippage Wrote:  Very nice! I did not know you could make any type of games in VB.NET.

You can!

All we need is some imagination. ✌


21 views and one comment?

Com'0n guys!
nice to share with us, i'm sure it well help people but: http://www.dreamincode.net/forums/topic/...pong-game/
exact the same, u just renamed a few controls 😁
Loosk good, although games in vb.net are usually pointless.
(Aug 26, 2010 05:09 AM)Kris` Wrote:  nice to share with us, i'm sure it well help people but: http://www.dreamincode.net/forums/topic/...pong-game/
exact the same, u just renamed a few controls 😁

Yes! I folowed that tut to create my first pong game.

Then I wrote this. 😊
thanks this is very usefull im going to read this over a couple of times and make some classes for a first person zombie shooter game ;)
(Aug 30, 2010 04:34 PM)DeToX Wrote:  thanks this is very usefull im going to read this over a couple of times and make some classes for a first person zombie shooter game ;)

Are u thinking about making a 3D FPS in vb.net? 😶
You can also do this with VB.NET and XNA, and of course you can make a game in vb.net, you can do anything your imagination will think up :)
2D lol

not 2 D lmao >.<
thank you i will try this ;)
Thanks for your comments!

I'm creatting a new pong game (more advanced), I'll post it later!
i had few errors with source
so i used full source but ball isn`t moving and computer isn`t moving :\

what should u do?


i found what`s wrong i didn`t turned timer :D
but i have 2 more problems
1.ball is just going in 1 row...it isn`t goind up or down
2.i added super cool big picture in bacground and it`s slowing my game :\ can i speed up it without deleting background??
(Sep 1, 2010 04:50 PM)RuleBreaker Wrote:  i had few errors with source
so i used full source but ball isn`t moving and computer isn`t moving :\

what should u do?


i found what`s wrong i didn`t turned timer :D
but i have 2 more problems
1.ball is just going in 1 row...it isn`t goind up or down
2.i added super cool big picture in bacground and it`s slowing my game :\ can i speed up it without deleting background??

1. Use the folowing code instead of the first line of your timer:

Code
Ball.Location = New Point(Ball.Location.X + xVel, Ball.Location.Y + yVel)

The code you're looking for looks like the one I gave you.

2. Use a picturebox with an image (no background of picbox) instead of background of the form.


(Sep 1, 2010 04:50 PM)RuleBreaker Wrote:  i had few errors with source
so i used full source but ball isn`t moving and computer isn`t moving :\

what should u do?


i found what`s wrong i didn`t turned timer :D
but i have 2 more problems
1.ball is just going in 1 row...it isn`t goind up or down
2.i added super cool big picture in bacground and it`s slowing my game :\ can i speed up it without deleting background??

When you get your pong finished share with us.
Fail its using a timer why not use multithreading ?


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Shell] by ɩᴄᴀʀᴜȿ ᴌוᴠᴇѕ™Sep 24, 2026 09:38 AM 4 84 Sep 24, 2026 09:38 AM
Last Post: Anonynuk
[Shell] by ɩᴄᴀʀᴜȿ ᴌוᴠᴇѕ™Jan 17, 2018 08:07 PM 5 501 Jan 17, 2018 08:07 PM
Last Post: techcrunch
[Shell] by VeiiApr 18, 2017 09:51 AM 0 211 Apr 18, 2017 09:51 AM
Last Post: Veii