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

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




HELPP please ! Ball bounce off Paddle?

 
Reply to this topicStart new topic

HELPP please ! Ball bounce off Paddle?

wow7542
22 Feb, 2007 - 06:23 PM
Post #1

New D.I.C Head
*

Joined: 22 Feb, 2007
Posts: 5


My Contributions
How do I get the ball to bounce off of the paddle?
right now i have:

'see if the ball is hitting the paddle

If bx + 20 > bx And by <= 40 + paddleheight And by + 20 >= 40 Then
bx = bx - 20
bdx = -bdx



please help! i've been working forever




CODE

Public Class hits1


    ' Move a paddle by the mouse
    ' With a bouncing ball


    ' 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 btnstart.Click

        

        ' set paper = to the existing picture box
        ' (which we named picBox in the form)
        paper = PicBox.CreateGraphics()

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

        ' top left coordinates of paddle


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

        ' vertical is
        py = PicBox.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 = 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 = PicBox.Width
        ymax = PicBox.Height

        ' display the ball
        paper.FillEllipse(Brushes.Blue, bx, by, 20, 20)

        ' hide the Start button
        btnStart.Visible = False

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

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



        'see if the ball is hitting the paddle

        If bx + 20 > bx And by <= 40 + paddleheight And by + 20 >= 40 Then
            bx = bx - 20
            bdx = -bdx

        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
            by = by - 2 * bdy
            bdy = -bdy
        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 - 5
            ' 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 + 5
            ' draw new paddle
            paper.FillRectangle(Brushes.Green, px, py, paddlewidth, paddleheight)
        End If

        ' save location of mouse pointer for comparison
        oldmx = mousex

    End Sub
End Class


This post has been edited by wow7542: 22 Feb, 2007 - 06:39 PM
User is offlineProfile CardPM
+Quote Post

KeyWiz
RE: HELPP Please ! Ball Bounce Off Paddle?
22 Feb, 2007 - 08:38 PM
Post #2

D.I.C Regular
Group Icon

Joined: 26 Oct, 2006
Posts: 428


Dream Kudos: 125
My Contributions
From what I can see, you put everything in its own class?
You are trying to make a class, but there is no form
associated with it, nor are the objects within the form.
So, create a form programatically within your class
and add the objects, button, picbox, etc.
or
Seperate the portion which was supposed to be the
class and put the rest in your main form.
User is offlineProfile CardPM
+Quote Post

orangeslide8
RE: HELPP Please ! Ball Bounce Off Paddle?
10 Mar, 2007 - 12:52 PM
Post #3

D.I.C Head
Group Icon

Joined: 29 Dec, 2006
Posts: 190



Thanked: 1 times
Dream Kudos: 25
My Contributions
hey ive been working on it and i fianally got it pretty much working
you need to put 1 button on called start 1 picturebox called picturebox1 and that it here's the code
CODE

Public Class form1


    ' Move a paddle by the mouse
    ' With a bouncing ball


    ' 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



        ' 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 = 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

        ' display the ball
        paper.FillEllipse(Brushes.Blue, bx, by, 20, 20)

        ' hide the Start button
        start.Visible = False

        ' Set timer to tick every 10 milliseconds
        Timer1.Interval = 10
        ' start timer
        Timer1.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.White, 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
            End If
        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
            by = by - 2 * bdy
            bdy = -bdy
        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 - 5
            ' 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 + 5
            ' 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

there are a few glitches and if i were you i'd make it so when it hit the bottom it was game over but its ure game so any way here u go
User is offlineProfile CardPM
+Quote Post

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

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