• (4 Pages)
• 1
• 2
• 3
• 4

Creating a fully-functional pong game. A step-by-step guide to creating a pong game in VB.NET Rate Topic: 2 Votes //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=39686&amp;s=7bd1c94105122068f5ef0847f8692319&md5check=' + ipb.vars['secure_hash'], cur_rating: 5, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#31 Bongi m

• New D.I.C Head

Reputation: 0
• Posts: 1
• Joined: 25-February 11

Posted 04 March 2011 - 05:04 AM

daneroo, on 25 September 2008 - 07:54 AM, said:

Great tutorial, well commented and explained thoroughly, Thank-you />

Dan

Hey thats great hey man your good keep it up.can you also post the battleship game tutorial please

This post has been edited by macosxnerd101: 07 June 2013 - 09:07 AM
Reason for edit:: Removed email link per site policies

#32 ohkelvin

• New D.I.C Head

Reputation: 0
• Posts: 1
• Joined: 26-March 11

Posted 26 March 2011 - 03:43 PM

RodgerB, on 17 December 2007 - 12:34 AM, said:

5) Making the ball bounce.

Making the ball bounce is easier than you may initially think. We need to put code in the gameTimer timer again, because we need it constantly checking if the ball needs to bounce or not.

To make the ball bounce, it is just a matter of setting the ball's position to contact the surface of the wall, and set the velocity opposite to it's current velocity. How would we set it opposite you ask? By making it negative. So hypothetically, we will be testing to see if the ball hits the wall, and if it does make it bounce off the wall. Here is how we would do this using the top wall. Note that you will need to add this to the Main Timer region, inside the timer subroutine.

```' Check for top wall.
If gameBall.Location.Y < 0 Then
gameBall.Location = New Point(gameBall.Location.X, 0)
yVel = -yVel
End If

```

Note that we only want to invert the Y Axis as we aren't dealing with any advanced spinning physics. The ball should now bounce off the wall and continue in it's path; which is obviously what we want.

Now we will need to make the code for the bottom wall. When checking for the collision, we need to to check if the ball is higher than the form's height minus the height of the ball, and we will take off 45 to line it up.

```' Check for bottom wall.
If gameBall.Location.Y > Me.Height - gameBall.Size.Height - 45 Then
gameBall.Location = New Point(gameBall.Location.X, Me.Height - gameBall.Size.Height - 45)
yVel = -yVel
End If

```

Now we have the ability to make the ball bounce up and down. Now that we can do this... I think it would be a good time to demonstrate how we can make the ball bounce off the paddles. It is actually relatively simpler than the form's walls, so don't worry, the head scratching is done for this section.. kinda. We will start with the player paddle. We will need to add the following to the main timer, as the main timer is for collisions and other miscellaneous tasks.

```' Check for player paddle.
gameBall.Location = New Point(paddlePlayer.Location.X - gameBall.Size.Width, _
gameBall.Location.Y)
xVel = -xVel
End If

```

This will check if the ball has intersected with the paddle object and bounce it off if it did. It is the same with the computer paddle too; the process involved is the same pretty much.

```' Check for computer paddle.
gameBall.Location = New Point(paddleComputer.Location.X + paddleComputer.Size.Width + 1, _
gameBall.Location.Y)
xVel = -xVel
End If

```

Now that we have the ball bouncing, we need to add Game Flow.

6) Game Flow.

Game Flow in pong is basically, you lose if the ball goes past the paddle. When it goes past the paddle we want the ball to go back to the middle of the form, and give the winner a point, which will be displayed in the label.

In order to give the players a score, we will need two global variables indicating them. In the Globals region, add the following code so we can start using these score variables.

```' The player's scores.
Dim compScore As Integer = 0
Dim plrScore As Integer = 0

```

Now we want to do something with these variables, otherwise we would just be wasting precious memory. Add the following to the main timer, as we are checking for a collision with the left wall.

```' Check for left wall.
If gameBall.Location.X < 0 Then
plrScore += 1
gameBall.Location = New Point(Me.Size.Width / 2, Me.Size.Height / 2)
plrScoreDraw.Text = Convert.ToString(plrScore)
End If

```

We will add one to the player's score, as he/she has scored, set the ball's position to the middle of the form and we will set the plrScoreDraw.Text property to display the player's score.

We will do the same thing if the computer wins, except we will be checking to see if the right wall has been hit.

```' Check for right wall.
If gameBall.Location.X > Me.Width - gameBall.Size.Width - paddlePlayer.Width Then
compScore += 1
gameBall.Location = New Point(Me.Size.Width / 2, Me.Size.Height / 2)
compScoreDraw.Text = Convert.ToString(compScore)
End If

```

Now that we have this functionality, the game is in a playable state. Now to a couple of 'nice to have' features.

My ball won't bounce... here is my code

```Public Class Game

Dim speed As Single = 10 'speed of ball'
Dim random As New Random() 'Random instance
Dim xVel As Single = Math.Cos(random.Next(5, 10)) * speed
Dim yVel As Single = Math.Cos(random.Next(5, 10)) * speed

Private Sub PictureBox2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox2.Click
Help.Visible = True
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
'Set the computer player to move according to the ball's position."

If ball.Location.Y > 5 And ball.Location.Y < Me.Height - 40 _
- Player.Height Then

PC.Location = New Point(PC.Location.X, ball.Location.Y)
End If
'Move the ball'
ball.Location = New Point(ball.Location.X + xVel, ball.Location.Y + yVel)

'Watch out for top wall.'
If ball.Location.Y < 0 Then
ball.Location = New Point(ball.Location.X, 0)
yVel = -yVel
End If

'Watch out for bottom 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

'Check 4 player
If ball.Bounds.IntersectsWith(Player.Bounds) Then
ball.Location = New Point(Player.Location.X - ball.Size.Width, _
ball.Location.Y)
xVel = -xVel
End If

'Check 4 computer
If ball.Bounds.IntersectsWith(PC.Bounds) Then
ball.Location = New Point(PC.Location.X + PC.Size.Width + 1, _
ball.Location.Y)
xVel = -xVel
End If
End Sub

' Move the paddle according to the mouse position.
Private Sub Player_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Player.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 Game_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Set up the game. (hid cursor)
Windows.Forms.Cursor.Hide()
End Sub
End Class

```

#33 Jordan.M

• New D.I.C Head

Reputation: 0
• Posts: 16
• Joined: 26-April 11

Posted 26 April 2011 - 01:58 PM

ozzyosborne68, on 29 November 2008 - 10:06 AM, said:

great
even i understood it and im only 15
dude im 12 and i made a similar tut

#34 Jordan.M

• New D.I.C Head

Reputation: 0
• Posts: 16
• Joined: 26-April 11

Posted 26 April 2011 - 02:07 PM

Jacques Malan, on 18 August 2010 - 11:23 AM, said:

I tried to add some code to close game once comp. player reaches 5 points, but my message box flies around and the game continues in the background.
```If compScoreDraw.Text = 5 Then
MessageBox.Show("You lose, better luck next time")
Me.Close()
End If

```

Any Help?

the code is

if compScoreDraw.Text = 5 Then
MsgBox("You lose, better luck next time")
End If

the reason you need to do this is if you say it your way it will show the message box, you need to just right the text after. plus you wrought the short form for message box wrong its "msgbox"

This post has been edited by Jordan.M: 26 April 2011 - 02:08 PM

#35 manowar689

Reputation: 0
• Posts: 55
• Joined: 26-September 10

Posted 27 May 2011 - 09:24 AM

Ok I have successfully adapted this code to work in a game of mini golf which in in the screenshot that I have posted below I'm having issues with first of all making the ball move to the direction of the cursor until it reaches a wall and two the wall shooting the ball off in the opposite direction aka using the angle of the line shape which one point is attached to the ball and the other end attached to the cursor with the mousemove form event so what I need to do is have it travel that line till a wall intersection and the come off at the opposite angle of the wall any ideas

Reputation: 3
• Posts: 139
• Joined: 23-May 11

Posted 22 July 2011 - 11:22 AM

How do I move the ball so that sometimes it will go faster than others?

#37 lbecker34

• New D.I.C Head

Reputation: 0
• Posts: 10
• Joined: 23-November 11

Posted 23 November 2011 - 05:59 PM

Cookiesliyr, on 09 June 2009 - 04:54 AM, said:

will nice work Rodger, about the difficult it won't be funny to make the computer slower as easier , to make the computer easier and beatable make it play as a human ! will how is that ? will not exactly like human but what does human do ? will they r not perfect so they won't follow the ball all the time , so u can make a random number which the computer will decide what to do, for example

random number between 1 to 100

if the number is between 1 to 50 => follow the ball
if the number is between 51 to 80 => stand still (do nothing)
if the number is between 80 to 100 => move in opposite direction

so in different difficult the range will change , so easy computer will stand still or move in the wrong way most of the time unlike the hard one.

i hope u get the idea how to make a beatable computer, btw i won't write the code i am sure u can do it

I generated the random number and it did make the computer beatable, but it made the computers movements very jumpy and of course random. Is there a way to make it smoother? Or do you think I may have entered the code wrong?

#38 lbecker34

• New D.I.C Head

Reputation: 0
• Posts: 10
• Joined: 23-November 11

Posted 23 November 2011 - 07:04 PM

CodesALot, on 03 June 2010 - 12:06 PM, said:

If you use the following code to move the computer paddle, the game is winnable. I used the suggestion of one of the others to incorporate random numbers. You can play with the range of the random numbers to affect the skill of the computer. I also added an invisible label control to slow the game down a bit (if the random number generated is an even number) which increases the likelyhood that the computer paddle will not always move to the correct location. I also created a constant for the X axis of the computer paddle since it will always be the same.

' Move computer depending upon random number generated
Dim objRandom As Random
Dim intRandom As Integer
objRandom = New Random()
intRandom = objRandom.Next(0, 100)
If (intRandom Mod 2) = 0 Then
lblRandom.Text = intRandom.ToString
End If
If (intRandom < 5) Or (intRandom > 95) Then
paddleComputer.Location = New Point(iCOMPUTER_X, gameBall.Location.Y)
End If

Where did you get "lblRandom" from?

#39 Othinolis

• New D.I.C Head

Reputation: 0
• Posts: 1
• Joined: 28-January 12

Posted 28 January 2012 - 09:28 AM

When I build the program, both paddles end up on the right side. Here's my code:
```Public Class pongMain
#Region "Globals"
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
' The player's scores.
Dim compScore As Integer = 0
Dim plrScore As Integer = 0
#End Region
#Region "Keep the paddle and score labels in the correct position when the form is resized."
Private Sub pongMain_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged
paddlePlayer.Location = New Point(Me.Width - 44, paddlePlayer.Location.Y)
plrScoreDraw.Location = New Point(Me.Width - 54, plrScoreDraw.Location.Y)
End Sub
#End Region

#Region "Hide Cursor"
' Set up the game.
Private Sub pongMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Windows.Forms.Cursor.Hide()
End Sub
#End Region

#Region "End Game on Escape Press"
' Escape the game when escape has been pressed.
Private Sub pongMain_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 Region

#Region "Move the paddle according to the mouse"
' Move the paddle according to the mouse position.
Private Sub pongMain_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 - paddlePlayer.Height Then _

End Sub
#End Region

#Region "Main Timer"
Private Sub gameTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gameTimer.Tick
'Set the computer player to move according to the ball's position."
If gameBall.Location.Y > 5 And gameBall.Location.Y < Me.Height - 40 _
- paddlePlayer.Height Then _
' Move the game ball.
gameBall.Location = New Point(gameBall.Location.X + xVel, gameBall.Location.Y + yVel)
' Check for top wall.
If gameBall.Location.Y < 0 Then
gameBall.Location = New Point(gameBall.Location.X, 0)
yVel = -yVel
End If
' Check for bottom wall.
If gameBall.Location.Y > Me.Height - gameBall.Size.Height - 45 Then
gameBall.Location = New Point(gameBall.Location.X, Me.Height - gameBall.Size.Height - 45)
yVel = -yVel
End If
' Check for player paddle.
gameBall.Location = New Point(paddlePlayer.Location.X - gameBall.Size.Width, _
gameBall.Location.Y)
xVel = -xVel
End If
' Check for computer paddle.
gameBall.Location = New Point(paddleComputer.Location.X + paddleComputer.Size.Width + 1, _
gameBall.Location.Y)
xVel = -xVel
' Check for left wall.
If gameBall.Location.X < 0 Then
plrScore += 1
gameBall.Location = New Point(Me.Size.Width / 2, Me.Size.Height / 2)
plrScoreDraw.Text = Convert.ToString(plrScore)
End If
' Check for right wall.
If gameBall.Location.X > Me.Width - gameBall.Size.Width - paddlePlayer.Width Then
compScore += 1
gameBall.Location = New Point(Me.Size.Width / 2, Me.Size.Height / 2)
compScoreDraw.Text = Convert.ToString(compScore)
End If
End If

End Sub
#End Region
End Class

```

#40 filiparag

• New D.I.C Head

Reputation: 0
• Posts: 1
• Joined: 28-May 12

Posted 28 May 2012 - 12:16 PM

I think there is a quite easy solution! I know VB.NET but I don't
know the correct solution in Visual Basic. Here is my "solution" :

```
Dim compSkill As Random()

...

If compSkill <= 100 Then
Else
paddleComputer.Location = New Point("Help me make PC like another human") 'I want to invert my moves or make them random
End If

```

...and thanks for the code, RodgerB!

#41 NM138

• New D.I.C Head

Reputation: 0
• Posts: 2
• Joined: 03-June 12

Posted 03 June 2012 - 05:54 AM

Hi, i made the game but when i enable the timer the escape button doesn't work anymore. Do you know how i can solve this?
thx NM138

#42 NM138

• New D.I.C Head

Reputation: 0
• Posts: 2
• Joined: 03-June 12

Posted 04 June 2012 - 11:08 AM

hi,i've got a problem with the escape key when I insert the timer then the escape key doesn't work anymore do you have any idea how I can solve this?
cheers

#43 mina62

• New D.I.C Head

Reputation: 0
• Posts: 1
• Joined: 07-June 13

Posted 07 June 2013 - 08:48 AM

```Public Class frmPongMain
'This game is called pong and is a very fun game in which the human player has to compete with the computer player and the one with the most points wins.
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
' The player's scores.
Dim compScore As Integer = 0
Dim plrScore As Integer = 0
#Region "Hide cursor"
' Set up the game.
Private Sub pongMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Windows.Forms.Cursor.Hide()
End Sub
#End Region

#Region "Move the paddle according to the mouse"
' Move the paddle according to the mouse position.
Private Sub pongMain_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 - playerPaddle.Height Then _
End Sub
#End Region

#Region "Main Timer"
Private Sub gameTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gameTimer.Tick
If gameBall.Location.Y > 5 And gameBall.Location.Y < Me.Height - 40 _
- playerPaddle.Height Then _
gameBall.Location = New Point(gameBall.Location.X + xVel, gameBall.Location.Y + yVel)
If gameBall.Location.Y < 0 Then
gameBall.Location = New Point(gameBall.Location.X, 0)
yVel = -yVel
End If
If gameBall.Location.Y > Me.Height - gameBall.Size.Height - 45 Then
gameBall.Location = New Point(gameBall.Location.X, Me.Height - gameBall.Size.Height - 45)
yVel = -yVel
End If
gameBall.Location = New Point(playerPaddle.Location.X - gameBall.Size.Width, _
gameBall.Location.Y)
xVel = -xVel
End If
gameBall.Location = New Point(paddleComputer.Location.X + paddleComputer.Size.Width + 1, _
gameBall.Location.Y)
xVel = -xVel
End If
If gameBall.Location.X < 0 Then
plrScore += 1
gameBall.Location = New Point(Me.Size.Width / 2, Me.Size.Height / 2)
plrScoreDraw.Text = Convert.ToString(plrScore)
End If
If gameBall.Location.X > Me.Width - gameBall.Size.Width - playerPaddle.Width Then
compScore += 1
gameBall.Location = New Point(Me.Size.Width / 2, Me.Size.Height / 2)
compScoreDraw.Text = Convert.ToString(compScore)
End If
End Sub
#End Region

#Region "End Game on Escape Press"
' Escape the game when escape has been pressed.
Private Sub pongMain_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 Region

#Region "Keep the paddle and score labels in the correct position when the form is resized."

Private Sub pongMain_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged
playerPaddle.Location = New Point(Me.Width - 44, playerPaddle.Location.Y)
plrScoreDraw.Location = New Point(Me.Width - 54, plrScoreDraw.Location.Y)
End Sub
#End Region
End Class
```

the problem occuring is that the player paddle is going to the side of the computer paddle (on the right side) when I debug. But, in the design view the player paddle is on the left side. What is wrong in the coding. Please help me out, my teacher wants to see it working by 14th. Please. Thank you very much.

This post has been edited by macosxnerd101: 07 June 2013 - 09:09 AM
Reason for edit:: Please use code tags. Removed personal info per user request.

#44 PhobicVOid

• New D.I.C Head

Reputation: 0
• Posts: 2
• Joined: 17-November 13

Posted 17 November 2013 - 10:53 PM

Hi I've had a problem where my pong ball has being knocked out the top of the 640, 480 form it will disappear and when
it respawns it does it again continuously so I have to restart it.
Thanks PhobicVOid

#45 pulpdrew

• New D.I.C Head

Reputation: 0
• Posts: 1
• Joined: 17-December 13

Posted 17 December 2013 - 03:10 PM

This is my edition. I added in the ability for the computer player to lose:

```Public Class pongMain

#Region "Globals"
Dim speed As Single = 15 ' Ball Speed
Dim rndInst As New Random() ' Random instance
Dim xVel As Single = Math.Cos(rndInst.Next(7, 10)) * speed
Dim yVel As Single = Math.Sin(rndInst.Next(5, 7)) * speed
Dim compScore As Integer = 0
Dim plrScore As Integer = 0
Dim shouldIGo As Integer
#End Region

#Region "Move the paddle according to the mouse"
Private Sub pongMain_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 - paddlePlayer.Height Then
End If
End Sub
#End Region

#Region "Main Timer"
Private Sub gameTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gameTimer.Tick
If (shouldIGo > 6) Then
If gameBall.Location.Y > 5 And gameBall.Location.Y < Me.Height - 40 - paddlePlayer.Height Then
End If
End If
gameBall.Location = New Point(gameBall.Location.X + xVel, gameBall.Location.Y + yVel)
If gameBall.Location.Y < 0 Then
gameBall.Location = New Point(gameBall.Location.X, 0)
yVel = -yVel
End If
If gameBall.Location.Y > Me.Height - gameBall.Size.Height - 45 Then
gameBall.Location = New Point(gameBall.Location.X, Me.Height - gameBall.Size.Height - 45)
yVel = -yVel
End If
gameBall.Location = New Point(paddlePlayer.Location.X - gameBall.Size.Width, gameBall.Location.Y)
xVel = -xVel
End If
gameBall.Location = New Point(paddleComputer.Location.X + paddleComputer.Size.Width + 1, gameBall.Location.Y)
xVel = -xVel
End If
If gameBall.Location.X < 0 Then
plrScore += 1
gameBall.Location = New Point(Me.Size.Width / 2, Me.Size.Height / 2)
plrDrawScore.Text = Convert.ToString(plrScore)
End If
If gameBall.Location.X > Me.Width - gameBall.Size.Width - paddlePlayer.Width Then
compScore += 1
gameBall.Location = New Point(Me.Size.Width / 2, Me.Size.Height / 2)
compDrawScore.Text = Convert.ToString(compScore)
End If
End Sub
#End Region
#Region "Hide Cursor"
Private Sub pongMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Windows.Forms.Cursor.Hide()
End Sub
#End Region
#Region "End Game on Escape Press"
Private Sub pongMain_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 Region
#Region "Keep the paddle and score labels in the correct position when the form is resized."
Private Sub pongMain_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged
paddlePlayer.Location = New Point(Me.Width - 70, paddlePlayer.Location.Y)
plrDrawScore.Location = New Point(Me.Width - 54, plrDrawScore.Location.Y)
End Sub
#End Region

Private Sub paddleDecide_Tick(sender As Object, e As EventArgs) Handles paddleDecide.Tick
shouldIGo = rndInst.Next(1, 10)
End Sub
End Class

```

It looks a little glitchy, but it works

This post has been edited by macosxnerd101: 24 December 2013 - 12:52 PM
Reason for edit:: Removed quote and added code tags