Computer Science Canada

pong game help

Author:  tupac [ Sun Feb 19, 2006 9:03 am ]
Post subject:  pong game help

k, so i got the ball to travel in a path I directed it, but now i want it 2 bounce off the walls (imade somethin like that on turing, but it was a lot easier). so my code is: <form includes a timer and a ball>
code:
Private Sub Timer1_Timer()
Dim dx As Integer
Dim dy As Integer
Dim shape1Right As Integer
Dim shape1Down As Integer
shape1Right = Shape1.Left + Shape1.Width
shape1Down = Shape1.Top - Shape1.Height
dx = 5
dy = 5
If Shape1.Top >= 4800 Then
    dx = -dx
End If
If shape1Down <= 0 Then
    dx = -dx
End If
If shape1Right >= 7800 Then
    dy = -dy
End If
If Shape1.Left <= 0 Then
    dy = -dy
End If
Shape1.Top = Shape1.Top + dx
Shape1.Left = Shape1.Left + dy
End Sub

thnx for any help:)

Author:  pavol [ Sun Feb 19, 2006 9:58 am ]
Post subject: 

just curious, why are you using .Top to test if the ball is >= 4800 and then make dx negative? shouldn't you be using shape1.Left or shape1Right if you want to change dx, and shape1.Left and shape1Down if you want to change dy.

Author:  tupac [ Tue Feb 21, 2006 9:15 pm ]
Post subject: 

i used that just cuz 4 some reason thats wat works... iduno, its weird...

Author:  cool dude [ Wed Feb 22, 2006 6:41 pm ]
Post subject: 

try something like this it should work. put the following code in your timer

code:

   'sets the boundary for the maximum left the ball can go to
    If ball1.Left <= Form.Left Then
        leftDir = 100
    End If
   
    'sets the boundary for the maximum right the ball can go to
    If ball1.Left + ball1.Width >= Form.Left + Form.Width Then
        leftDir = -100
    End If
   
    'sets the boundary for the maximum height the ball can go to
    If ball1.Top <= Form.Top Then
        topDir = 100
    End If


note i did not use the same variables as u so just change the variable names Smile


: