Aug 26, 2010 04:25 AM
(This post was last modified: Dec 6, 2010 01:39 PM by Obi-Wan Cannabis.)
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.
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).
And add this code to the Timer you just added on the begining of tutorial.
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).
Step 7) Now lets make the computer play like an intelligent player. Add the folowing code to Timer1.
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.
Lets give some use to those gv. Add the folowing code to timer1.
Step 9) Lets hide our cursor.
Step 10) Now lets add a key to close our game [ESC].
Full Source Code Spoiler (Click to View)
Our game is completed now!
[IMG]http://img704.imageshack.us/img704/3808/77722840.png[/IMG]
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 SubIf 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)) * speedAnd 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.YStep 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 IfStep 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 = 0Lets 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 IfStep 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 SubStep 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 SubFull Source Code Spoiler (Click to View)
Our game is completed now!
[IMG]http://img704.imageshack.us/img704/3808/77722840.png[/IMG]


