• (3 Pages)
  • +
  • 1
  • 2
  • 3

Creating a fully-functional pong game. A step-by-step guide to creating a pong game in VB.NET Rate Topic: ***** 2 Votes

#16 Stippadippa  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 10-July 09

Posted 24 February 2010 - 11:00 AM

I really liked the tutorial and ofc the idea, but i think the tutorial needs some explain. I want to understand the code more and by that learn and improvice writing new parts to the game.
Was This Post Helpful? 0
  • +
  • -

#17 Guest_Mrs. Phelps*


Reputation:

Posted 06 March 2010 - 06:09 PM

How do i make it so that the computer player can be beatable too?
because the game is not fair.... just wondering
Was This Post Helpful? 0

#18 Guest_Jake Phillips*


Reputation:

Posted 27 March 2010 - 07:06 AM

Hi, my ball doesnt seem to rebound off of the paddles...can ya tell me why??

Public Class FrmPongMain
    'Variables'
    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

    Private Sub FrmPongMain_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        'Moving Player Paddle with up and down arrows'
        Select Case e.KeyCode
            Case Keys.Up
                tmrUp.Enabled = True
                picPaddlePlayer.Top = picPaddlePlayer.Top - 5

            Case Keys.Down
                tmrDown.Enabled = True
                picPaddlePlayer.Top = picPaddlePlayer.Top + 5

        End Select
    End Sub

    Private Sub tmrGameTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrGameTimer.Tick
        'Making the computer paddle move up and down corresponding to where the ball is'
        If picGameBall.Location.Y > 5 And picGameBall.Location.Y < Me.Height - 40 _
                - picPaddlePlayer.Height Then _
                picComputerPaddle.Location = New Point(picComputerPaddle.Location.X, picGameBall.Location.Y)

        'Making the ball move'
        picGameBall.Location = New Point(picGameBall.Location.X + xVel, picGameBall.Location.Y + yVel)

        'Making the ball rebound off of the top wall'
        If picGameBall.Location.Y < 0 Then
            picGameBall.Location = New Point(picGameBall.Location.X, 0)
            yVel = -yVel
        End If

        'Making the ball rebound off of the bottom wall'
        If picGameBall.Location.Y > Me.Height - picGameBall.Size.Height - 45 Then
            picGameBall.Location = New Point(picGameBall.Location.X, Me.Height - picGameBall.Size.Height - 45)
            yVel = -yVel
        End If

        ' Check for player paddle.
        If picGameBall.Bounds.IntersectsWith(picPaddlePlayer.Bounds) Then
            picGameBall.Location = New Point(picPaddlePlayer.Location.X - picGameBall.Size.Width, _
            picGameBall.Location.Y)
            xVel = -xVel
        End If

        ' Check for computer paddle.
        If picGameBall.Bounds.IntersectsWith(picComputerPaddle.Bounds) Then
            picGameBall.Location = New Point(picComputerPaddle.Location.X + picComputerPaddle.Size.Width + 1, _
            picGameBall.Location.Y)
            xVel = -xVel
        End If
    End Sub
End Class

This post has been edited by JackOfAllTrades: 27 March 2010 - 07:49 AM
Reason for edit:: Added code tags.

Was This Post Helpful? 0

#19 Guest_Anthony*


Reputation:

Posted 27 April 2010 - 03:21 AM

View PostFireRyder, on 04 February 2010 - 04:39 PM, said:

Help

I wrote down the code and made sure that the code is the same. But everytime i try to start the program, it shuts down a second after. I figured out it was the check top for the ball bounce but it won't work though. I need help and sorry if i didn't write this right.

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


I believe that you have just end, If i am rightly in saying it should be End Sub.
Hope that helps :)
Was This Post Helpful? 0

#20 Guest_nick*


Reputation:

Posted 28 April 2010 - 09:10 PM

could some one please tell me how to make it so that you can win the game
Was This Post Helpful? -1

#21 Guest_jacob erickson*


Reputation:

Posted 07 May 2010 - 09:20 AM

it does not bounce of the paddles can u help me

Public Class pongmain

#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 _
paddlePlayer.Location = New Point(paddlePlayer.Location.X, e.Y)

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 _
paddleComputer.Location = New Point(paddleComputer.Location.X, gameBall.Location.Y)
' 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.
If gameBall.Bounds.IntersectsWith(paddlePlayer.Bounds) Then
gameBall.Location = New Point(paddlePlayer.Location.X - gameBall.Size.Width, _
gameBall.Location.Y)
xVel = -xVel
End If
' Check for computer paddle.
If gameBall.Bounds.IntersectsWith(paddleComputer.Bounds) Then
gameBall.Location = New Point(paddleComputer.Location.X + paddleComputer.Size.Width + 1, _
gameBall.Location.Y)
xVel = -xVel
End If
' 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 Sub
#End Region

#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

Private Sub pongmain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub maintimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles maintimer.Tick
' Check for player paddle.
If gameBall.Bounds.IntersectsWith(paddlePlayer.Bounds) Then
gameBall.Location = New Point(paddlePlayer.Location.X - gameBall.Size.Width, _
gameBall.Location.Y)
xVel = -xVel
End If
' Check for computer paddle.
If gameBall.Bounds.IntersectsWith(paddleComputer.Bounds) Then
gameBall.Location = New Point(paddleComputer.Location.X + paddleComputer.Size.Width + 1, _
gameBall.Location.Y)
xVel = -xVel
End If
End Sub

Private Sub paddlePlayer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles paddlePlayer.Click

End Sub

Private Sub gameBall_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gameBall.Click

End Sub
End Class
Was This Post Helpful? 0

#22 jesusfreak783  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 2
  • Joined: 07-April 10

Posted 24 May 2010 - 01:26 AM

What do u use to write the code?
Do I need a program to run the code?
How do i run the code?

This post has been edited by macosxnerd101: 27 July 2010 - 10:48 AM

Was This Post Helpful? -1
  • +
  • -

#23 CodesALot  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 03-June 10

Posted 03 June 2010 - 12:06 PM

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
Was This Post Helpful? 0
  • +
  • -

#24 Guest_The helper who needs help*


Reputation:

Posted 27 July 2010 - 09:59 AM

To beat the comp, change

Dim speed As Single = 10 ' Ball Speed


to

Dim speed As Single = 15 ' Ball Speed


This way, if the ball goes really fast, the comp can miss it. Otherwise its impossible.



Note: Can someone please tell me how to design a multiplayer pong game in vb .net?
Was This Post Helpful? 0

#25 Guest_Jacques Malan*


Reputation:

Posted 18 August 2010 - 10:57 AM

Best article I have come accross. As a beginner I feel like I have both learned and accomplished something. Thanks a million!!
Was This Post Helpful? 0

#26 Guest_Jacques Malan*


Reputation:

Posted 18 August 2010 - 11:23 AM

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?
Was This Post Helpful? 0

#27 TechKid  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 82
  • Joined: 04-September 10

Posted 27 September 2010 - 12:23 PM

View Postozzyosborne68, on 29 November 2008 - 09:06 AM, said:

great
even i understood it and im only 15


I'm 12.. :o

View PostThe helper who needs help, on 27 July 2010 - 08:59 AM, said:

To beat the comp, change

Dim speed As Single = 10 ' Ball Speed


to

Dim speed As Single = 15 ' Ball Speed


This way, if the ball goes really fast, the comp can miss it. Otherwise its impossible.



Note: Can someone please tell me how to design a multiplayer pong game in vb .net?


The only way you would have a shot at a multi player pong game is using arrow keys and like 'W' and 'S' etc to go up and down.
Was This Post Helpful? 0
  • +
  • -

#28 Guest_equilibrium*


Reputation:

Posted 06 December 2010 - 08:37 AM

Very good tutorial!
I was considering how to make the computer beatable, create a variable which determines whether or the computer paddle can reach the ball within a certain distance. I'm new to coding so i'm not entirely sure how to put it but if the ball is within X distance and Y distance then
dont move, else if continue?

Sorry for resurrecting the thread.
Was This Post Helpful? 0

#29 Guest_Eug*


Reputation:

Posted 16 December 2010 - 09:29 AM

God Bless u brov!
Was This Post Helpful? 0

#30 Guest_Midloo*


Reputation:

Posted 20 December 2010 - 09:55 AM

I'm working through the issue of the ball going through the paddles as well. wondering if it's because I'm using VS2005 or the fact that some of the datatype conversions are screwy. With "Option Strict On", it disallows conversions like those in the declaration:

"dim xVel as Single = Math.Cos(rndInst.Next(5, 10)) * speed"
Was This Post Helpful? 0

  • (3 Pages)
  • +
  • 1
  • 2
  • 3