Pong problem

brandnew741

New member
Joined
May 17, 2006
Messages
2
Programming Experience
Beginner
Okay, I am having a problem getting the ball to start moving. It is a picture box and I have a timer. I want it to bounce around the form hitting off the paddles. I know i need to figure out collision detection for that, right? so if someone could tell me how to star the ball moving, i'd be grateful. Also, if anyone has a pong game I could look at as an example, that would be most helpful. Thanks.
 

Luc

Well-known member
Joined
Nov 29, 2005
Messages
59
Programming Experience
1-3
Don't use picture boxes for a simple game as pong, use GDI+ in stead.
Here is an example of a pong game i wrote a time ago, this was one of the first projects i ever wrote so its not really good coding, but still it could be of use to you.

VB.NET:
    Protected Overrides Sub onpaint(ByVal e As System.windows.Forms.PaintEventArgs)
        Dim g As Graphics = e.Graphics
        Dim PX(4), PY(4), EX, EY, EY2, nul, uitzonderlijk As Integer
        Dim heh As Decimal
        PX(1) = 50
        PX(2) = 50
        PY(1) = 50 + b
        PY(2) = 100 + b
        PX(3) = 350
        PX(4) = 350
        PY(3) = 70 + c
        PY(4) = 120 + c
        EX = 300 + d
        EY = 100 + f
        EY2 = EY

        If PY(1) < 0 Then
            PY(1) = 0
            PY(2) = 50
        End If

        If PY(2) > 230 Then
            PY(1) = 180
            PY(2) = 230
        End If

        'De 2 palletten en het balletje maken
        Dim pen As Pen = New Pen(Color.DarkCyan, 3)
        Dim p1 As Point = New Point(PX(1), PY(1))
        Dim p2 As Point = New Point(PX(2), PY(2))
        Dim p3 As Point = New Point(PX(3), PY(3))
        Dim p4 As Point = New Point(PX(4), PY(4))
        g.DrawLine(pen, p1, p2)
        g.DrawLine(pen, p3, p4)

        Dim pen2 As Pen = New Pen(Color.DarkCyan, 6)
        Dim pen3 As Pen = New Pen(Color.LightSeaGreen, 4)
        g.DrawEllipse(pen2, EX, EY, 2, 2)

        ' de omgeving maken
        Dim Phelft1 As Point = New Point(200, 0)
        Dim Phelft2 As Point = New Point(200, 250)
        g.DrawLine(pen3, Phelft1, Phelft2)

        Dim Ponder1 As Point = New Point(0, 215)
        Dim Ponder2 As Point = New Point(400, 215)
        g.DrawLine(pen3, Ponder1, Ponder2)

        Dim Pboven1 As Point = New Point(0, 2)
        Dim Pboven2 As Point = New Point(400, 2)
        g.DrawLine(pen3, Pboven1, Pboven2)

        'de Goals maken
        Dim pen4 As Pen = New Pen(Color.Violet, 5)
        Dim Pgoal11 As Point = New Point(5, 50)
        Dim Pgoal12 As Point = New Point(5, 150)
        g.DrawLine(pen4, Pgoal11, Pgoal12)

        Dim Pgoal21 As Point = New Point(385, 50)
        Dim Pgoal22 As Point = New Point(385, 150)
        g.DrawLine(pen4, Pgoal21, Pgoal22)

        If EX < 0 Then EX = 5
        If EX > 390 Then EX = 385

        'weerkatsing en puntescoring van de goals---------------------------------------
        If EX = 5 Then
            If EY > 50 And EY < 150 Then
                richting = -1
                puntlinks += 1
                tmr2.Enabled = True
            Else
                richting = -1
            End If
        End If


        If EX = 385 Then
            If EY > 50 And EY < 150 Then
                richting = 1
                puntrechts += 1
                tmr2.Enabled = True
            Else
                richting = 1
            End If
        End If

        ' oleoleole het macheert, het botsen van de bal tegen de omgeving
        If EY < 0 Then EY = 0
        If EY = 0 And richting = -1 Then
            If schuin = 1 Then
                schuin = -1
            Else
                If schuin = -1 Then schuin = 1
            End If

        End If
        If EY = 0 And richting = 1 Then
            If schuin = 1 Then
                schuin = -1
            Else
                If schuin = -1 Then schuin = 1
            End If

        End If

        If EY = 0 Then EY = 1

        If EY > 215 And schuin = 1 Then schuin = -1
        If EY > 215 And schuin = -1 Then schuin = 1

        If richting = -1 Then d += snelheid
        If richting = 1 Then d -= snelheid
        If schuin = 1 Then f -= 1
        If schuin = -1 Then f += 1
        If schuin = 0 Then f = f


        ' het botsen van de bal tegen de palletjes------------------------------------------
        If EX = PX(1) Then
            If EY > PY(1) And EY < PY(2) Then
                richting = -1
                If boud > b Then schuin = 1
                If boud < b Then schuin = -1
                If b = boud Then schuin = rndmin(-1, 1)
            End If
        End If


        If EX = PX(3) Then
            If EY > PY(3) And EY < PY(4) Then
                richting = 1
                If coud > c Then schuin = 1
                If coud < c Then schuin = -1
                If c = coud Then schuin = rndmin(-1, 1)
            End If
        End If

        'de achterlijke inteligentie van het andere palletje ;)
        'speler optie nog nie echt ok

        'If speler = 1 Then
        If EY < 215 And EY > 0 Then
            If EY < PY(3) Then c -= tegenstandersnelheid
            If EY > PY(4) Then c += tegenstandersnelheid
        End If
        'End If

        '------------------------------------------------------------------------------------
        'oude gegevens opslaan
        boud = b
        coud = c
        'scorebord
        TXT1.Text = CStr(puntlinks)
        TXT2.Text = CStr(puntrechts)
        'aanpassing tegenstander

    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        b = 0
        tmr1.Enabled = True
        lblgoal.Visible = False
        lbllevel.Visible = False
        MenuItem5.Checked = True
        MenuItem12.Checked = True
        SetStyle(ControlStyles.DoubleBuffer, True)
        SetStyle(ControlStyles.AllPaintingInWmPaint, True)
    End Sub
    Private Sub tmr1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmr1.Tick
        Invalidate()
    End Sub
    Private Function rndmin(ByVal min As Integer, ByVal max As Integer)
        Dim getal As Integer
        Randomize()
        getal = Int((max - min + 1) * Rnd() + min)
        Return getal
    End Function

    Private Sub tmr2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmr2.Tick
        flash += 1
        If flash = 1 Or flash = 3 Or flash = 5 Or flash = 7 Then
            lblgoal.Visible = True
        Else
            lblgoal.Visible = False
        End If
        If flash = 8 Then
            tmr2.Enabled = False
            flash = 0
        End If
    End Sub

    Private Sub MenuItem5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem5.Click
        MenuItem5.Checked = True
        snelheid = 1
        MenuItem6.Checked = False
        MenuItem7.Checked = False
        MenuItem8.Checked = False
        MenuItem9.Checked = False
    End Sub

    Private Sub MenuItem6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem6.Click
        MenuItem6.Checked = True
        snelheid = 2
        MenuItem5.Checked = False
        MenuItem7.Checked = False
        MenuItem8.Checked = False
        MenuItem9.Checked = False
    End Sub

    Private Sub MenuItem7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem7.Click
        MenuItem7.Checked = True
        snelheid = 3
        MenuItem6.Checked = False
        MenuItem5.Checked = False
        MenuItem8.Checked = False
        MenuItem9.Checked = False
    End Sub

    Private Sub MenuItem8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem8.Click
        MenuItem8.Checked = True
        snelheid = 4
        MenuItem6.Checked = False
        MenuItem7.Checked = False
        MenuItem5.Checked = False
        MenuItem9.Checked = False
    End Sub

    Private Sub MenuItem9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem9.Click
        MenuItem9.Checked = True
        snelheid = 50
        MenuItem6.Checked = False
        MenuItem7.Checked = False
        MenuItem8.Checked = False
        MenuItem5.Checked = False
    End Sub

    Private Sub MenuItem12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem12.Click
        MenuItem12.Checked = True
        tegenstandersnelheid = 1
        MenuItem13.Checked = False
        MenuItem14.Checked = False
        MenuItem15.Checked = False
        MenuItem16.Checked = False
    End Sub

    Private Sub MenuItem13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem13.Click
        MenuItem12.Checked = True
        tegenstandersnelheid = 2
        MenuItem12.Checked = False
        MenuItem14.Checked = False
        MenuItem15.Checked = False
        MenuItem16.Checked = False
    End Sub

    Private Sub MenuItem14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem14.Click
        MenuItem14.Checked = True
        tegenstandersnelheid = 3
        MenuItem13.Checked = False
        MenuItem12.Checked = False
        MenuItem15.Checked = False
        MenuItem16.Checked = False
    End Sub

    Private Sub MenuItem15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem15.Click
        MenuItem15.Checked = True
        tegenstandersnelheid = 4
        MenuItem13.Checked = False
        MenuItem14.Checked = False
        MenuItem12.Checked = False
        MenuItem16.Checked = False
    End Sub

    Private Sub MenuItem16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem16.Click
        MenuItem16.Checked = True
        tegenstandersnelheid = 15
        MenuItem13.Checked = False
        MenuItem14.Checked = False
        MenuItem15.Checked = False
        MenuItem12.Checked = False
    End Sub

    Private Sub MenuItem17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem17.Click
        MenuItem17.Checked = True
        speler = 1
        MenuItem18.Checked = False
    End Sub

    Private Sub MenuItem18_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem18.Click
        MenuItem17.Checked = True
        MessageBox.Show("optie nog nie af", "schit", MessageBoxButtons.OK, MessageBoxIcon.Hand)
        MenuItem18.Checked = False
    End Sub

    Private Sub TXT2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TXT2.TextChanged

    End Sub

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        Dim t$
        Select Case e.KeyCode
            Case Keys.Up
                b -= 2
                Invalidate()
            Case Keys.Down
                b += 2
                Invalidate()

            Case Keys.Space
                b = 0
                c = 0
                d = 0
                f = 0
                schuin = 0
                puntlinks = 0
                puntrechts = 0

        End Select
    End Sub
 

brandnew741

New member
Joined
May 17, 2006
Messages
2
Programming Experience
Beginner
I don't really know how to draw shapes in VB. I'm quite novice really. Would there be a simple method for getting the ball to bounce around as a picture box? Thanks.
 

Luc

Well-known member
Joined
Nov 29, 2005
Messages
59
Programming Experience
1-3
Just search for a GDI+ tutorial, basicly what you do is you got to your form_onpaint sub and
dim g as graphic = e.graphics

then you dim the pen to write with
dim p as pen = new pen(color,width)

then you can draw the shape you want, in your case a small elipse
g.drawEllipse(pen, point1,point2)

If you want the form to refresh then you use the me.invalidate() method.
 
Top Bottom