# VB making ball bounce off paddles



## ste2223 (Sep 2, 2004)

Hi I have got Visualbasic 6.0 and I am making a pong clone. I have the paddles moving up and down and they stop at the top and bottom of the form but I need to know how to make the ball bounce off things. I have used this to make the ball start moving:

picBall.Left = picBall.Left - 50

I will probably expand on that by making it random, but for now I need to know how to make it bounce off the paddles. Thanks!


----------



## biomorphiz (Jan 10, 2005)

You need to check the position of the ball when it hits the paddle. When the ball hits the paddle, the coordinate is the same. From this moment, you know how to bounce the ball. 

I prefer to use picBall.move x, y


----------



## coderitr (Oct 12, 2003)

I've constructed this small program containing a box consisting of four lines that intersect at the ends and a ball that bounces off of them. The idea is to reverse either the direction of the motion when the point reaches the wall. The VB shape control has a border that extends out past the left and right edges if it is configured as a circle so that's why the ball bounces before it reaches the left/right wall. You'll have to work with that to see what you can do about it.

Hope this helps


----------



## ste2223 (Sep 2, 2004)

Thanks coderitr. Your progam helped me in boncing the ball off the sides of the form. I can't seem to configure it to make the ball bounce off the paddles. I thought it would be:

```
If (picBall.Left + picBall.Width) = picPaddle2.Left then
HorizontalDir = HorizontalDir * -1
```
This does not work because the ball keeps bouncing just before it reaches the edge of the form. It will bounce along the line that the paddle can move.

Can anyone help me please?


----------



## coderitr (Oct 12, 2003)

You need to determine, when the ball comes to the point of the paddle, if it is vertically within the doman of the paddle. Sorry, but I don't have time right now to try and debug this one.


----------



## coderitr (Oct 12, 2003)

It's not perfect but then again, it's pong.


----------



## ste2223 (Sep 2, 2004)

Hi. I put my pong on hold for a while, but now Im back to finish it. I tried using the following code to make the ball bounce off the paddle but it does not work.

```
If picBall.Top >= picPaddle2.Top And _
    picBall.Top <= picPaddle2.Top + picPaddle2.Height And _
    picBall.Left + picBall.Width >= picPaddle2.Left Then
VerticalDir = VerticalDir * -1
End If
```
Does anyone know whats wrong with it?


----------

