Yes, it's yet another Pong Game.
I'm writing a pong program for a VB course I'm doing. I've got the two paddles moving up and down in a picture box but how do I keep them from disappearing off the screen?
Printable View
Yes, it's yet another Pong Game.
I'm writing a pong program for a VB course I'm doing. I've got the two paddles moving up and down in a picture box but how do I keep them from disappearing off the screen?
Are you moving the ball using vectors,
Or using angles?Code:X=X+A
Y=Y+B
The first is easier (for starting anyway), you'd do something like this (example is for the top paddle (has the lowest y value as the origin is at the top left):Code:X=X+(Sin(Angle)*Speed)
Y=Y+(Cos(Angle)*Speed)
X, Y is the position of the picbox (it's origin is also top left), and a and b are the speed vectors to add to them to create movement.
VB Code:
If Ball.Y < paddle.Y + paddle.Height And Ball.X + Ball.Width > paddle.X And Ball.X < paddle.X + paddle.Width Then 'Ball hit the paddle, so change it's y direction (a way from the paddle) Ball.B = -Abs(Ball.B) 'Could manipulate the Ball.A value here according to where the ball hit the pad. End If
Here is the code if you're using the angle & speed method:
VB Code:
If Ball.Y < paddle.Y + paddle.Height And Ball.X + Ball.Width > paddle.X And Ball.X < paddle.X + paddle.Width Then Ball.Angle = 90-(Ball.Angle-270) 'Note: This will be different for all other collisions End If
For a lot more info, and stuff that will get you the best grade ;) check out this thread:
http://www.vbforums.com/showthread.p...hreadid=163044
Thanks, I'll go check it out.
I have another little prob not covered by the other thread. I want to play a sound everytime the ball hits the walls. I've used this code:
If BallX < 0 Then
BallX = 0
BallXDir = 1
RtnValue = sndPlaySound(Bounce, SND_ASYNC Or SND_MEMORY)
The sound has been stored here:
Dim Buffer As String
Dim F As Integer
Dim SoundBuffer As String
On Error GoTo NoiseGet_Error
Buffer = Space(1024)
SoundBuffer = ""
F = FreeFile
Open FileName For Binary As F
Do While Not EOF(F)
Get #F, , Buffer
SoundBuffer = SoundBuffer & Buffer
Loop
Close F
StoreSound = Trim(SoundBuffer)
Exit Function
NoiseGet_Error:
SoundBuffer = ""
Exit Function
And in the form load:
Bounce = StoreSound(App.Path + "Bounce.wav")
I think I've declared everything I should:
Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
Private Const SND_ASYNC = &H1
Private Const SND_SYNC = &H0
Private Const SND_MEMORY = &H4
But the sound won't play. Can you see where I might have gone wrong? Maybe I left something out.
If the ball is changing direction correctly, then the problem is with your sound code, as i havn't used sound before i can't help you :(
OK, Thanks anyway