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,746 people online right now. Registration is fast and FREE... Join Now!




problem with brick breaker

 
Reply to this topicStart new topic

problem with brick breaker, can't make the ball bounce off the bricks

orangeslide8
11 Mar, 2007 - 04:37 AM
Post #1

D.I.C Head
Group Icon

Joined: 29 Dec, 2006
Posts: 190



Thanked: 1 times
Dream Kudos: 25
My Contributions
what i have is a brick breaker code(only 1 layer of bricks so far) but when the ball is coming at the paddle if you get it right on the edge it like goes right through the paddle. also when the ball goes to hit the bricks it gets rid of them but doesn't bounce off of them it goes to the top then bounces off making it so that you almost always get 2 bricks at a time heres my full code
CODE

Public Class form1

    Dim x As Integer
    ' Move a paddle by the mouse
    ' With a bouncing ball
    ' bricks
    Dim z(16) As Boolean
    ' 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, n As Integer

    ' horizontal mouse position
    Dim mousex, oldmx As Integer

    ' maximum of picture box
    Dim xmax, ymax 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
        Me.Cursor.Dispose()
        setstuff()
    End Sub
    Private Sub setstuff()
        z(1) = False
        z(2) = False
        z(3) = False
        z(4) = False
        z(5) = False
        z(6) = False
        z(7) = False
        z(8) = False
        z(9) = False
        z(10) = False
        z(11) = False
        z(12) = False
        z(13) = False
        z(14) = False
        z(15) = False
        z(16) = False
        ' set paper = to the existing picture box
        ' (which we named picBox in the form)
        paper = PictureBox1.CreateGraphics()

        ' make the picture box all white
        paper.Clear(Color.White)

        ' top left coordinates of paddle


        ' horiz is center - 20
        px = PictureBox1.Width / 2 - 20

        ' vertical is
        py = PictureBox1.Height - 40
        paper.FillRectangle(Brushes.Green, px, py, paddlewidth, paddleheight)
        ' initial values for ball
        bx = 35   ' x,y is coordinates of top left corner
        by = 20
        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

        ' display the ball
        paper.FillEllipse(Brushes.Blue, bx, by, 20, 20)
        paper.FillRectangle(Brushes.Red, 0, 0, 40, 10)
        paper.FillRectangle(Brushes.Red, 42, 0, 40, 10)
        paper.FillRectangle(Brushes.Red, 84, 0, 40, 10)
        paper.FillRectangle(Brushes.Red, 126, 0, 40, 10)
        paper.FillRectangle(Brushes.Red, 168, 0, 40, 10)
        paper.FillRectangle(Brushes.Red, 210, 0, 40, 10)
        paper.FillRectangle(Brushes.Red, 252, 0, 40, 10)
        paper.FillRectangle(Brushes.Red, 294, 0, 40, 10)
        paper.FillRectangle(Brushes.Red, 336, 0, 40, 10)
        paper.FillRectangle(Brushes.Red, 378, 0, 40, 10)
        paper.FillRectangle(Brushes.Red, 420, 0, 40, 10)
        paper.FillRectangle(Brushes.Red, 462, 0, 40, 10)
        paper.FillRectangle(Brushes.Red, 504, 0, 40, 10)
        paper.FillRectangle(Brushes.Red, 546, 0, 40, 10)
        paper.FillRectangle(Brushes.Red, 588, 0, 40, 10)
        paper.FillRectangle(Brushes.Red, 630, 0, 40, 10)
        ' hide the Start button
        start.Visible = False

        ' Set timer to tick every 10 milliseconds
        Timer1.Interval = 10
        ' start timer
        Timer1.Start()
    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
    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.White, bx, by, 20, 20)

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



        'see if the ball is hitting the paddle
        If by <= 10 Then
            If bx >= 0 And bx <= 42 Then
                z(1) = True
                by = -by
                bdy = -bdy
                paper.FillRectangle(Brushes.White, 0, 0, 40, 20)
            End If
            If bx >= 42 And bx <= 84 Then
                z(2) = True
                by = -by
                bdy = -bdy
                paper.FillRectangle(Brushes.White, 42, 0, 40, 20)
            End If
            If bx >= 84 And bx <= 126 Then
                z(3) = True
                by = -by
                bdy = -bdy
                paper.FillRectangle(Brushes.White, 84, 0, 40, 20)
            End If
            If bx >= 126 And bx <= 168 Then
                z(4) = True
                by = -by
                bdy = -bdy
                paper.FillRectangle(Brushes.White, 126, 0, 40, 20)
            End If
            If bx >= 168 And bx <= 210 Then
                z(5) = True
                by = -by
                bdy = -bdy
                paper.FillRectangle(Brushes.White, 168, 0, 40, 20)
            End If
            If bx >= 210 And bx <= 252 Then
                z(6) = True
                by = -by
                bdy = -bdy
                paper.FillRectangle(Brushes.White, 210, 0, 40, 20)
            End If
            If bx >= 252 And bx <= 294 Then
                z(7) = True
                by = -by
                bdy = -bdy
                paper.FillRectangle(Brushes.White, 252, 0, 40, 20)
            End If
            If bx >= 294 And bx <= 336 Then
                z(8) = True
                by = -by
                bdy = -bdy
                paper.FillRectangle(Brushes.White, 294, 0, 40, 20)
            End If
            If bx >= 336 And bx <= 378 Then
                z(9) = True
                by = -by
                bdy = -bdy
                paper.FillRectangle(Brushes.White, 336, 0, 40, 20)
            End If
            If bx >= 378 And bx <= 420 Then
                z(10) = True
                by = -by
                bdy = -bdy
                paper.FillRectangle(Brushes.White, 378, 0, 40, 20)
            End If
            If bx >= 420 And bx <= 462 Then
                z(11) = True
                by = -by
                bdy = -bdy
                paper.FillRectangle(Brushes.White, 420, 0, 40, 20)
            End If
            If bx >= 462 And bx <= 504 Then
                z(12) = True
                by = -by
                bdy = -bdy
                paper.FillRectangle(Brushes.White, 462, 0, 40, 20)
            End If
            If bx >= 504 And bx <= 546 Then
                z(13) = True
                paper.FillRectangle(Brushes.White, 504, 0, 40, 20)
            End If
            If bx >= 546 And bx <= 588 Then
                z(14) = True
                by = -by
                bdy = -bdy
                paper.FillRectangle(Brushes.White, 546, 0, 40, 20)
            End If
            If bx >= 588 And bx <= 630 Then
                z(15) = True
                by = -by
                bdy = -bdy
                paper.FillRectangle(Brushes.White, 588, 0, 40, 20)
            End If
            If bx >= 630 And bx <= 672 Then
                z(16) = True
                by = -by
                bdy = -bdy
                paper.FillRectangle(Brushes.White, 630, 0, 40, 20)
            End If
        End If
        If z(1) = True And z(2) = True And z(3) = True And z(4) = True And z(5) = True And z(6) = True And z(7) = True And z(8) = True And z(9) = True And z(10) = True And z(11) = True And z(12) = True And z(13) = True And z(14) = True And z(15) = True And z(16) = True Then
            MsgBox("you won")
            Close()
        End If
        ' check for hit paddle
        If bx <= px + paddlewidth And bx > px Then
            If (by + 20) >= py Then
                by = by - 2 * bdy
                bdy = -bdy
            End If
        End If
        ' If bx Then
        ' 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")
            End If
            Timer1.Stop()
            PictureBox1.BackColor = Color.Blue
            System.Threading.Thread.Sleep(300)
            setstuff()
            ' gatta change stuff
            PictureBox1.BackColor = Color.Blue
        ElseIf by < 0 Then
            by = -by
            bdy = -bdy
        End If

        ' now draw the circle again
        paper.FillEllipse(Brushes.Blue, 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.White, px, py, paddlewidth, paddleheight)
            ' move paddle left
            px = px - (oldmx - mousex)
            ' draw new paddle
            paper.FillRectangle(Brushes.Green, px, py, paddlewidth, paddleheight)

        ElseIf mousex > oldmx And px < xmax - paddlewidth Then
            ' erase paddle
            paper.FillRectangle(Brushes.White, px, py, paddlewidth, paddleheight)
            ' move paddle right
            px = px + (mousex - oldmx)
            ' draw new paddle
            paper.FillRectangle(Brushes.Green, 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
End Class

thnx
john
User is offlineProfile CardPM
+Quote Post

orangeslide8
RE: Problem With Brick Breaker
12 Mar, 2007 - 01:00 PM
Post #2

D.I.C Head
Group Icon

Joined: 29 Dec, 2006
Posts: 190



Thanked: 1 times
Dream Kudos: 25
My Contributions
can anyone help me?

User is offlineProfile CardPM
+Quote Post

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

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