Visual Basic > Graphics Games Programming
Bouncing ball
Bouncing ball 'make a new project 'add a fom: name = frmBouncingBall 'add a picturbox: name = picBall 'add the code below 'press F5 Public blnReady As Boolean Public Motion Dim r Dim G Dim B Private sub Form_Click() blnReady = True End Sub Private sub Form_Load() Me.Show Motion = Int(4 * Rnd + 1) blnReady = False Do While Not blnReady Call BouncingBall EvenWachten DoEvents Loop End Sub Public sub BouncingBall() r = 255 * Rnd G = 255 * Rnd B = 255 * Rnd With frmBouncingBall Select case Motion case 1 ' move the graphic left and up by 20 twips using the move method. .picBall.Move .picBall.Left - 20, .picBall.Top - 20 ' If the graphic reaches the left edge of the form, move it to the right and up. If .picBall.Left <= 0 Then Motion = 2 .picBall.FillColor = RGB(r, G, B) ' If the graphic reaches the top edge of the form, move it to the left and down. ElseIf .picBall.Top <= 0 Then Motion = 4 .picBall.FillColor = RGB(r, G, B) end If case 2 ' move the graphic right and up by 20 twips. .picBall.Move .picBall.Left + 20, .picBall.Top - 20 ' If the graphic reaches the right edge of the form, move it to the left and up. ' Routine determines the right edge of the form by subtracting the graphic ' width from the form width. If .picBall.Left >= (.Width - .picBall.Width) Then Motion = 1 .picBall.FillColor = RGB(r, G, B) ' If the graphic reaches the top edge of the form, move it to the right and down. ElseIf .picBall.Top <= 0 Then Motion = 3 .picBall.FillColor = RGB(r, G, B) end If case 3 ' move the graphic right and down by 20 twips. .picBall.Move .picBall.Left + 20, .picBall.Top + 20 ' If the graphic reaches the right edge of the form, move it to the left and down. If .picBall.Left >= (.Width - .picBall.Width) Then Motion = 4 .picBall.FillColor = RGB(r, G, B) ' If the graphic reaches the bottom edge of the form, move it to the right and up. ' Routine determines the bottom of the form by subtracting ' the graphic height from the form height less 680 twips for the height ' of title bar and menu bar. ElseIf .picBall.Top >= (.Height - .picBall.Height) - 80 Then Motion = 2 .picBall.FillColor = RGB(r, G, B) end If case 4 ' move the graphic left and down by 20 twips. .picBall.Move .picBall.Left - 20, .picBall.Top + 20 ' If the graphic reaches the left edge of the form, move it to the right and down. If .picBall.Left <= 0 Then Motion = 3 .picBall.FillColor = RGB(r, G, B) ' If the graphic reaches the bottom edge of the form, move it to the left and up. ElseIf .picBall.Top >= (.Height - .picBall.Height) - 80 Then Motion = 1 .picBall.FillColor = RGB(r, G, B) end If end Select end With End Sub Public sub EvenWachten() dim Start dim Check start = Timer Do Until check >= start + 0.005 check = Timer Loop End Sub Return
Visual Basic Codes
ActiveX
Miscellaneous
Applications
Code Snippets
Common Dialogs
Special Effects
Database Stuff
Date Time
Files Drives
Forms
Graphics Games
Internet Stuff
Multimedia
Other
Strings
Windows