Welcome to Dream.In.Code
Become a VB Expert!

Join 137,233 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,738 people online right now. Registration is fast and FREE... Join Now!




pong

 
Reply to this topicStart new topic

pong, a couple probelms with it

orangeslide8
17 Mar, 2007 - 05:08 AM
Post #1

D.I.C Head
Group Icon

Joined: 29 Dec, 2006
Posts: 190



Thanked: 1 times
Dream Kudos: 25
My Contributions
i have made pong but there are a few glitches. 1 when it starts again using the restart sub but then i throws a "A generic error occurred in GDI+. " also the ball sometimes goes through my paddle odly enough.
heres my code
CODE

Public Class form1
    Dim y As Integer
    Dim x As Integer
    Dim youhit As Boolean
    ' Move a paddle by the mouse
    ' With a bouncing ball

    Dim aispeed As Integer
    ' module variables  -- these variables exist across Subs

    Dim px, py As Integer
    ' establish a variable called paper of type Graphics
    Dim paper As Graphics

    ' ball position variables
    Dim bx, by, bdx, bdy As Integer
    Dim bx2, by2, bdx2, bdy2 As Integer
    ' horizontal mouse position
    Dim mousex, oldmx As Integer

    ' maximum of picture box
    Dim xmax, ymax As Integer

    Dim px2, py2 As Integer
    Const paddleheight As Integer = 10
    Const paddlewidth As Integer = 40



    Private Sub start_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles start.Click
        ' set paper = to the existing picture box
        ' (which we named picBox in the form)
        paper = PictureBox1.CreateGraphics()
        Me.Cursor.Dispose()
        ' make the picture box all white
        paper.Clear(Color.Blue)

        ' top left coordinates of paddle


        ' horiz is center - 20
        px = PictureBox1.Width / 2 - 20
        px2 = PictureBox1.Width / 2 - 20
        ' vertical is
        py = PictureBox1.Height - 40
        py2 = 40
        paper.FillRectangle(Brushes.Red, px, py, paddlewidth, paddleheight)
        paper.FillRectangle(Brushes.Green, px2, py2, paddlewidth, paddleheight)
        ' initial values for ball
        bx = 35   ' x,y is coordinates of top left corner
        by = 3
        bdx = 2 ' change in x each tick
        bdy = 1  ' change in y each tick

        ' initial horizontal value for mouse
        oldmx = Control.MousePosition.X

        ' right and bottom sides of picturebox
        xmax = PictureBox1.Width
        ymax = PictureBox1.Height
        aispeed = 2.5
        ' display the ball
        paper.FillEllipse(Brushes.White, bx, by, 20, 20)

        ' hide the Start button
        start.Visible = False
        ' Set timer to tick every 10 milliseconds
        Timer1.Interval = 1
        Timer2.Interval = 100
        ' start timer
        Timer1.Start()
        Timer2.Start()
    End Sub

    

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        ' update ball

        ' erase the previous circle
        paper.FillEllipse(Brushes.Blue, bx, by, 20, 20)

        ' change the position
        bx = bx + bdx
        by = by + bdy



        'see if the ball is hitting the paddle

        If bx <= px + paddlewidth And bx > px Then
            If (by + 20) >= py Then
                
                by = by - 2 * bdy
                bdy = -bdy
                youhit = True
            End If
        End If
        If youhit = True Then
            bx2 = bx
            by2 = by
            bdx2 = bdx
            bdy2 = bdy
top:
                bx2 = bx2 + bdx2
                by2 = by2 + bdy2
                If (bx2 + 20) > xmax Then
                    bx2 = bx2 - 2 * bdx2
                    bdx2 = -bdx2
                ElseIf bx < 0 Then
                    bx2 = -bx2
                    bdx2 = -bdx2
            End If
            If by2 > 50 Then
                GoTo top
            End If
            If bx2 - 10 < px2 Then
                paper.FillRectangle(Brushes.Blue, px2, py2, paddlewidth, paddleheight)
                px2 = px2 - aispeed
                paper.FillRectangle(Brushes.Green, px2, py2, paddlewidth, paddleheight)
            End If
            If bx2 - 10 > px2 Then
                paper.FillRectangle(Brushes.Blue, px2, py2, paddlewidth, paddleheight)
                px2 = px2 + aispeed
                paper.FillRectangle(Brushes.Green, px2, py2, paddlewidth, paddleheight)
            End If
        End If
        If youhit = True Then
            If by <= 50 Then
                If bx >= px2 And bx < px2 + paddlewidth Then
                    by = by - 2 * bdy
                    bdy = -bdy
                    youhit = False

                End If
            End If
        End If
        If youhit = False Then
            paper.FillRectangle(Brushes.Blue, px2, py2, paddlewidth, paddleheight)
            If px2 < (PictureBox1.Width / 2 - 3) Then
                px2 = px2 + 3
            End If
            If px2 > PictureBox1.Width / 2 + 3 Then
                px2 = px2 - 3
            End If
            paper.FillRectangle(Brushes.Green, px2, py2, paddlewidth, paddleheight)
        End If
        ' check for boundaries
        If (bx + 20) > xmax Then
            bx = bx - 2 * bdx
            bdx = -bdx
        ElseIf bx < 0 Then
            bx = -bx
            bdx = -bdx
        End If
        If (by + 20) > ymax Then
            x = x + 1
            If x = 1 Then
                MsgBox("you lost")
                Restart("pong")
                Me.Close()
            End If

        ElseIf by < 0 Then

            y = y + 1
            If y = 1 Then
                MsgBox("you won")
                Restart("pong")
                Me.Close()
            End If
        End If

        ' now draw the circle again
        paper.FillEllipse(Brushes.White, bx, by, 20, 20)

        ' now handle the paddle

        ' get current horizontal location of mouse pointer
        mousex = Control.MousePosition.X



        If mousex < oldmx And px > 0 Then
            ' erase paddle
            paper.FillRectangle(Brushes.Blue, px, py, paddlewidth, paddleheight)
            ' move paddle left
            px = px - (oldmx - mousex)
            ' draw new paddle
            paper.FillRectangle(Brushes.Red, px, py, paddlewidth, paddleheight)

        ElseIf mousex > oldmx And px < xmax - paddlewidth Then
            ' erase paddle
            paper.FillRectangle(Brushes.Blue, px, py, paddlewidth, paddleheight)
            ' move paddle right
            px = px + (mousex - oldmx)
            ' draw new paddle
            paper.FillRectangle(Brushes.Red, px, py, paddlewidth, paddleheight)
        End If

        ' save location of mouse pointer for comparison
        oldmx = mousex

    End Sub

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

    End Sub
    Sub Restart(ByVal program As String)
        For Each proc As Process In Process.GetProcesses
            If proc.ProcessName = program Then proc.Kill()
        Next
        Process.Start(program)
    End Sub
End Class

thnx
john
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/4/08 03:49PM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month