Post: [TUT] How to create a Pong Game in VB.NET
05-09-2011, 11:46 PM #1
(adsbygoogle = window.adsbygoogle || []).push({});
Originally posted by kingdeath360 View Post
In this tutorial I'm gonna show you how to create a Pong Game in vb.net.

[size=medium][color=#FF1493]What will we need?
[/color][/size]
Windows Compute


Visual Basic 2008/2010

Free Time :thumbsup:

[size=medium][color=#FFA500]Lets Start[/color][/size]

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.

You must login or register to view this content.

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.

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

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

    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.

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

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

    
'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 Cool Man (aka Tustin) Lets set our Score System.

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

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


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

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

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

     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

    Public Class Form1
Dim speed As Single = 10 ' 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
Dim compScore As Integer = 0
Dim plrScore As Integer = 0
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
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Ball.Location = New Point(Ball.Location.X + xVel, Ball.Location.Y)
Timer1.Interval = 13
' 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
'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)
'Add 1 score to player if ball reach 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
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Cursor.Hide()
End Sub
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
End Class


[size=medium][color=#1E90FF]Our game is completed now![/color][/size]

You must login or register to view this content.


its a c&p///////////////////////////////

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo