If you've ever wanted to do a little game programming in vb, there's a simple way to do collision detection between two objects using the IntersectRect API Function and the RECT type...
Below is a form that contains a break-out style game, demonstrating bouncing a 'ball' into 'bricks'.
Let me first describe how IntersectRect works:
You need three RECTangles, defined as TYPEs as follows:
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
You'll also declare the IntersectRect API function using:
Private Declare Function IntersectRect Lib "user32" _
(lpDestRect As RECT, lpSrc1Rect As RECT, lpSrc2Rect As RECT) As Long
This function requires 3 RECTangles. The first 'lbDestRect' is the resulting rectange created frome the 'intersection' of two others. lpSrc1Rect and lpSrc2Rect are the two boxes you are testing for a collision of.
The code below is a form. Copy the code below and paste it into notepad and save it as Form1.frm. Open the form in VB 5 or 6 and run it. I've commented the code to make things easier to understand. Enjoy!
VERSION 5.00
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 4830
ClientLeft = 60
ClientTop = 450
ClientWidth = 6540
LinkTopic = "Form1"
ScaleHeight = 4830
ScaleWidth = 6540
StartUpPosition = 3 'Windows Default
Begin VB.Timer timBallBounce
Interval = 10
Left = 4215
Top = 3960
End
Begin VB.Timer timCollisions
Interval = 10
Left = 4680
Top = 3960
End
Begin VB.PictureBox picBox
Appearance = 0 'Flat
BackColor = &H00404040&
ForeColor = &H80000008&
Height = 4395
Left = 180
ScaleHeight = 4365
ScaleWidth = 6105
TabIndex = 0
Top = 210
Width = 6135
Begin VB.PictureBox picPaddle
Appearance = 0 'Flat
BackColor = &H80000005&
ForeColor = &H80000008&
Height = 195
Left = 2310
ScaleHeight = 165
ScaleWidth = 975
TabIndex = 2
Top = 3585
Width = 1005
End
Begin VB.PictureBox picBall
Appearance = 0 'Flat
BackColor = &H80000005&
ForeColor = &H80000008&
Height = 165
Left = 705
ScaleHeight = 135
ScaleWidth = 120
TabIndex = 1
Top = 2055
Width = 150
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillStyle = 0 'Solid
Height = 300
Index = 49
Left = 615
Top = 885
Width = 615Private Declare Function IntersectRect Lib "user32" _
(lpDestRect As RECT, lpSrc1Rect As RECT, lpSrc2Rect As RECT) As Long
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillColor = &H000000FF&
FillStyle = 0 'Solid
Height = 300
Index = 48
Left = 1230
Top = 885
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillStyle = 0 'Solid
Height = 300
Index = 47
Left = 1830
Top = 885
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillColor = &H000000FF&
FillStyle = 0 'Solid
Height = 300
Index = 46
Left = 2445
Top = 885
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillStyle = 0 'Solid
Height = 300
Index = 45
Left = 3060
Top = 885
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillColor = &H000000FF&
FillStyle = 0 'Solid
Height = 300
Index = 44
Left = 3675
Top = 885
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillStyle = 0 'Solid
Height = 300
Index = 43
Left = 4275
Top = 885
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillColor = &H000000FF&
FillStyle = 0 'Solid
Height = 300
Index = 42
Left = 0
Top = 885
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillColor = &H000000FF&
FillStyle = 0 'Solid
Height = 300
Index = 41
Left = 4890
Top = 885
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillStyle = 0 'Solid
Height = 300
Index = 40
Left = 5505
Top = 885
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillColor = &H000000FF&
FillStyle = 0 'Solid
Height = 300
Index = 39
Left = 5520
Top = 1185
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillStyle = 0 'Solid
Height = 300
Index = 38
Left = 4905
Top = 1185
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillColor = &H000000FF&
FillStyle = 0 'Solid
Height = 300
Index = 37
Left = 4290
Top = 1185
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillStyle = 0 'Solid
Height = 300
Index = 36
Left = 3675
Top = 1185
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillColor = &H000000FF&
FillStyle = 0 'Solid
Height = 300
Index = 35
Left = 3075
Top = 1185
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillStyle = 0 'Solid
Height = 300
Index = 34
Left = 2460
Top = 1185
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillColor = &H000000FF&
FillStyle = 0 'Solid
Height = 300
Index = 33
Left = 1860
Top = 1185
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillStyle = 0 'Solid
Height = 300
Index = 32
Left = 1245
Top = 1185
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillColor = &H000000FF&
FillStyle = 0 'Solid
Height = 300
Index = 31
Left = 630
Top = 1185
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillStyle = 0 'Solid
Height = 300
Index = 30
Left = 15
Top = 1185
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillStyle = 0 'Solid
Height = 300
Index = 29
Left = 0
Top = 600
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillColor = &H000000FF&
FillStyle = 0 'Solid
Height = 300
Index = 28
Left = 615
Top = 600
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillStyle = 0 'Solid
Height = 300
Index = 27
Left = 1230
Top = 600
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillColor = &H000000FF&
FillStyle = 0 'Solid
Height = 300
Index = 26
Left = 1845
Top = 600
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillStyle = 0 'Solid
Height = 300
Index = 25
Left = 2445
Top = 600
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillColor = &H000000FF&
FillStyle = 0 'Solid
Height = 300
Index = 24
Left = 3060
Top = 600
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillStyle = 0 'Solid
Height = 300
Index = 23
Left = 3660
Top = 600
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillColor = &H000000FF&
FillStyle = 0 'Solid
Height = 300
Index = 22
Left = 4275
Top = 600
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillStyle = 0 'Solid
Height = 300
Index = 21
Left = 4890
Top = 600
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillColor = &H000000FF&
FillStyle = 0 'Solid
Height = 300
Index = 20
Left = 5505
Top = 600
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillColor = &H000000FF&
FillStyle = 0 'Solid
Height = 300
Index = 19
Left = 5490
Top = 0
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillStyle = 0 'Solid
Height = 300
Index = 18
Left = 5490
Top = 300
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillStyle = 0 'Solid
Height = 300
Index = 17
Left = 4875
Top = 0
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillColor = &H000000FF&
FillStyle = 0 'Solid
Height = 300
Index = 16
Left = 4875
Top = 300
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillColor = &H000000FF&
FillStyle = 0 'Solid
Height = 300
Index = 15
Left = -15
Top = 300
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillStyle = 0 'Solid
Height = 300
Index = 14
Left = 4260
Top = 300
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillColor = &H000000FF&
FillStyle = 0 'Solid
Height = 300
Index = 13
Left = 3660
Top = 300
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillStyle = 0 'Solid
Height = 300
Index = 12
Left = 3045
Top = 300
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillColor = &H000000FF&
FillStyle = 0 'Solid
Height = 300
Index = 11
Left = 2430
Top = 300
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillStyle = 0 'Solid
Height = 300
Index = 10
Left = 1815
Top = 300
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillColor = &H000000FF&
FillStyle = 0 'Solid
Height = 300
Index = 9
Left = 1215
Top = 300
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillStyle = 0 'Solid
Height = 300
Index = 8
Left = 600
Top = 300
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillColor = &H000000FF&
FillStyle = 0 'Solid
Height = 300
Index = 7
Left = 4260
Top = 0
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillStyle = 0 'Solid
Height = 300
Index = 6
Left = 3645
Top = 0
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillColor = &H000000FF&
FillStyle = 0 'Solid
Height = 300
Index = 5
Left = 3045
Top = 0
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillStyle = 0 'Solid
Height = 300
Index = 4
Left = 2430
Top = 0
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillColor = &H000000FF&
FillStyle = 0 'Solid
Height = 300
Index = 3
Left = 1830
Top = 0
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillStyle = 0 'Solid
Height = 300
Index = 2
Left = 1215
Top = 0
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillColor = &H000000FF&
FillStyle = 0 'Solid
Height = 300
Index = 1
Left = 600
Top = 0
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillStyle = 0 'Solid
Height = 300
Index = 0
Left = -15
Top = 0
Width = 615
End
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Dim Speed As Integer
Dim YY As Integer ' The Up/Down Direction Variable for the 'BALL'
Dim XX As Integer ' The Left/Right Direction Variable for the 'Ball'
'''''''''''' The IntersectRect API call provides functional Collision Detection
''' lpDestRect receives the RESULTING rectangle where lpSrc1Rect and lpSrc2Rect meet.
''' the function returns 0 if no collision exists, and 1 if there is a collision
''' this is our 'test', in that we simply test if the result = 1 or 0
Private Type RECT ' The RECT type is used to identify borders of bricks and balls
Left As Long ' NOTE That you CANNOT Use the IntersectRect function without RECT!
Top As Long
Right As Long
Bottom As Long
End Type
Private Sub Form_Initialize()
Randomize ' make random truly random
Speed = 14 ' the starting speed (ie. points to move the ball per interval)
YY = Speed ' Assign the speed to the YY (up/down) variable
XX = Speed ' and the Left/Right variable
picBall.Left = Int(Rnd * picBox.Width - picBall.Width) + 4 ' randomly place the ball left to right
End Sub
Private Sub picBox_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
picPaddle.Left = X ' this moves the paddle left and right when you move the mouse (in the box)
End Sub
Private Sub timBallBounce_Timer()
If Speed > 50 Then Speed = 50 ' keep the speed to a semi reasonable value
If YY < 0 Then YY = -Speed ' if the ball is moving UP, re-assign the speed going UP
If YY > 0 Then YY = Speed ' if the ball is moving DOWN, re-assign the speed going DOWN
If XX < 0 Then XX = -Speed ' if the ball is moving LEFT, re-assign the speed going LEFT
If XX > 0 Then XX = Speed ' if the ball is moving right, re-assign the speed going RIGHT
If picBall.Top >= picBox.Height - picBall.Height Then YY = YY * -1 ' bounce off the Bottom
If picBall.Top < 1 Then YY = YY * -1: picBall.Top = 1 ' bounce off the TOP
If picBall.Left <= 1 Then XX = XX * -1 ' bounce off the LEFT
If picBall.Left >= picBox.Width - picBall.Width Then ' bounce off the RIGHT
picBall.Left = picBox.Width - picBall.Width ' sometimes the ball
XX = XX * -1 ' gets stuck on the right wall
End If ' without this little snippit
picBall.Left = picBall.Left + XX ' Actually MOVE THE BALL LEFT or RIGHT
picBall.Top = picBall.Top + YY ' Actually MOVE THE BALL Up or DOWN
End Sub
Private Sub timCollisions_Timer()
Dim Ball As RECT ' The Ball's RECTangle
Dim Brick(55) As RECT ' the Bricks RECTangles
Dim ResRect As RECT ' the Resulting RECTangle of a collision (we dont test this)
Dim Paddle As RECT ' the Paddle's RECTange
Dim bars As Integer
With Paddle ' Make the Paddle RECTangle stay positioned with the
.Top = picPaddle.Top ' actual paddle 'picPaddle'
.Left = picPaddle.Left
.Right = picPaddle.Left + picPaddle.Width
.Bottom = picPaddle.Top + picPaddle.Height
End With
With Ball ' Make the Ball RECTangle stay positioned witht the
.Top = picBall.Top ' actual Ball 'picBall'
.Left = picBall.Left
.Right = picBall.Left + picBall.Width
.Bottom = picBall.Top + picBall.Height
End With
If IntersectRect(ResRect, Ball, Paddle) Then ' A booleanesque 1 fires if the ball and paddle
Speed = Speed + 1 ' intersect. We reverse the UP/DOWN in that case
YY = -Speed
picBall.Left = picBall.Left + Int(Rnd * 28) + 14 ' this helps prevent getting stuck bouncing in the same places
Exit Sub
End If
For bars = 0 To ShapeBar.UBound ' Start a loop... one for each brick (i called them bars here for some reason)
With Brick(bars) ' draw the RECTangle location for each brick
.Top = ShapeBar(bars).Top
.Left = ShapeBar(bars).Left
.Right = ShapeBar(bars).Left + ShapeBar(bars).Width
.Bottom = ShapeBar(bars).Top + ShapeBar(bars).Height
End With
If ShapeBar(bars).Visible = True Then ' if a brick is visible, test for Collision with BALL!
If IntersectRect(ResRect, Ball, Brick(bars)) Then 'A booleanesque 1 fires and we hit a brick
ShapeBar(bars).Visible = False 'Hide that brick we hit
Speed = Speed + 1 'Go faster!
If YY > 0 Then Exit Sub ' Ball moving down? Then let it keep going!
YY = YY * -1 ' Reverse the ball's UP/DOWN direction
End If
End If
DoEvents ' it's a fast loop, but still, lets free windows for giggles
Next bars
End Sub
No Dolphins were harmed in the posting of this message... Dolphin Friendly Tuna!
Below is a form that contains a break-out style game, demonstrating bouncing a 'ball' into 'bricks'.
Let me first describe how IntersectRect works:
You need three RECTangles, defined as TYPEs as follows:
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
You'll also declare the IntersectRect API function using:
Private Declare Function IntersectRect Lib "user32" _
(lpDestRect As RECT, lpSrc1Rect As RECT, lpSrc2Rect As RECT) As Long
This function requires 3 RECTangles. The first 'lbDestRect' is the resulting rectange created frome the 'intersection' of two others. lpSrc1Rect and lpSrc2Rect are the two boxes you are testing for a collision of.
The code below is a form. Copy the code below and paste it into notepad and save it as Form1.frm. Open the form in VB 5 or 6 and run it. I've commented the code to make things easier to understand. Enjoy!
VERSION 5.00
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 4830
ClientLeft = 60
ClientTop = 450
ClientWidth = 6540
LinkTopic = "Form1"
ScaleHeight = 4830
ScaleWidth = 6540
StartUpPosition = 3 'Windows Default
Begin VB.Timer timBallBounce
Interval = 10
Left = 4215
Top = 3960
End
Begin VB.Timer timCollisions
Interval = 10
Left = 4680
Top = 3960
End
Begin VB.PictureBox picBox
Appearance = 0 'Flat
BackColor = &H00404040&
ForeColor = &H80000008&
Height = 4395
Left = 180
ScaleHeight = 4365
ScaleWidth = 6105
TabIndex = 0
Top = 210
Width = 6135
Begin VB.PictureBox picPaddle
Appearance = 0 'Flat
BackColor = &H80000005&
ForeColor = &H80000008&
Height = 195
Left = 2310
ScaleHeight = 165
ScaleWidth = 975
TabIndex = 2
Top = 3585
Width = 1005
End
Begin VB.PictureBox picBall
Appearance = 0 'Flat
BackColor = &H80000005&
ForeColor = &H80000008&
Height = 165
Left = 705
ScaleHeight = 135
ScaleWidth = 120
TabIndex = 1
Top = 2055
Width = 150
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillStyle = 0 'Solid
Height = 300
Index = 49
Left = 615
Top = 885
Width = 615Private Declare Function IntersectRect Lib "user32" _
(lpDestRect As RECT, lpSrc1Rect As RECT, lpSrc2Rect As RECT) As Long
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillColor = &H000000FF&
FillStyle = 0 'Solid
Height = 300
Index = 48
Left = 1230
Top = 885
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillStyle = 0 'Solid
Height = 300
Index = 47
Left = 1830
Top = 885
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillColor = &H000000FF&
FillStyle = 0 'Solid
Height = 300
Index = 46
Left = 2445
Top = 885
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillStyle = 0 'Solid
Height = 300
Index = 45
Left = 3060
Top = 885
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillColor = &H000000FF&
FillStyle = 0 'Solid
Height = 300
Index = 44
Left = 3675
Top = 885
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillStyle = 0 'Solid
Height = 300
Index = 43
Left = 4275
Top = 885
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillColor = &H000000FF&
FillStyle = 0 'Solid
Height = 300
Index = 42
Left = 0
Top = 885
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillColor = &H000000FF&
FillStyle = 0 'Solid
Height = 300
Index = 41
Left = 4890
Top = 885
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillStyle = 0 'Solid
Height = 300
Index = 40
Left = 5505
Top = 885
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillColor = &H000000FF&
FillStyle = 0 'Solid
Height = 300
Index = 39
Left = 5520
Top = 1185
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillStyle = 0 'Solid
Height = 300
Index = 38
Left = 4905
Top = 1185
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillColor = &H000000FF&
FillStyle = 0 'Solid
Height = 300
Index = 37
Left = 4290
Top = 1185
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillStyle = 0 'Solid
Height = 300
Index = 36
Left = 3675
Top = 1185
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillColor = &H000000FF&
FillStyle = 0 'Solid
Height = 300
Index = 35
Left = 3075
Top = 1185
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillStyle = 0 'Solid
Height = 300
Index = 34
Left = 2460
Top = 1185
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillColor = &H000000FF&
FillStyle = 0 'Solid
Height = 300
Index = 33
Left = 1860
Top = 1185
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillStyle = 0 'Solid
Height = 300
Index = 32
Left = 1245
Top = 1185
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillColor = &H000000FF&
FillStyle = 0 'Solid
Height = 300
Index = 31
Left = 630
Top = 1185
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillStyle = 0 'Solid
Height = 300
Index = 30
Left = 15
Top = 1185
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillStyle = 0 'Solid
Height = 300
Index = 29
Left = 0
Top = 600
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillColor = &H000000FF&
FillStyle = 0 'Solid
Height = 300
Index = 28
Left = 615
Top = 600
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillStyle = 0 'Solid
Height = 300
Index = 27
Left = 1230
Top = 600
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillColor = &H000000FF&
FillStyle = 0 'Solid
Height = 300
Index = 26
Left = 1845
Top = 600
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillStyle = 0 'Solid
Height = 300
Index = 25
Left = 2445
Top = 600
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillColor = &H000000FF&
FillStyle = 0 'Solid
Height = 300
Index = 24
Left = 3060
Top = 600
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillStyle = 0 'Solid
Height = 300
Index = 23
Left = 3660
Top = 600
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillColor = &H000000FF&
FillStyle = 0 'Solid
Height = 300
Index = 22
Left = 4275
Top = 600
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillStyle = 0 'Solid
Height = 300
Index = 21
Left = 4890
Top = 600
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillColor = &H000000FF&
FillStyle = 0 'Solid
Height = 300
Index = 20
Left = 5505
Top = 600
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillColor = &H000000FF&
FillStyle = 0 'Solid
Height = 300
Index = 19
Left = 5490
Top = 0
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillStyle = 0 'Solid
Height = 300
Index = 18
Left = 5490
Top = 300
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillStyle = 0 'Solid
Height = 300
Index = 17
Left = 4875
Top = 0
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillColor = &H000000FF&
FillStyle = 0 'Solid
Height = 300
Index = 16
Left = 4875
Top = 300
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillColor = &H000000FF&
FillStyle = 0 'Solid
Height = 300
Index = 15
Left = -15
Top = 300
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillStyle = 0 'Solid
Height = 300
Index = 14
Left = 4260
Top = 300
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillColor = &H000000FF&
FillStyle = 0 'Solid
Height = 300
Index = 13
Left = 3660
Top = 300
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillStyle = 0 'Solid
Height = 300
Index = 12
Left = 3045
Top = 300
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillColor = &H000000FF&
FillStyle = 0 'Solid
Height = 300
Index = 11
Left = 2430
Top = 300
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillStyle = 0 'Solid
Height = 300
Index = 10
Left = 1815
Top = 300
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillColor = &H000000FF&
FillStyle = 0 'Solid
Height = 300
Index = 9
Left = 1215
Top = 300
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillStyle = 0 'Solid
Height = 300
Index = 8
Left = 600
Top = 300
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillColor = &H000000FF&
FillStyle = 0 'Solid
Height = 300
Index = 7
Left = 4260
Top = 0
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillStyle = 0 'Solid
Height = 300
Index = 6
Left = 3645
Top = 0
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillColor = &H000000FF&
FillStyle = 0 'Solid
Height = 300
Index = 5
Left = 3045
Top = 0
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillStyle = 0 'Solid
Height = 300
Index = 4
Left = 2430
Top = 0
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillColor = &H000000FF&
FillStyle = 0 'Solid
Height = 300
Index = 3
Left = 1830
Top = 0
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillStyle = 0 'Solid
Height = 300
Index = 2
Left = 1215
Top = 0
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillColor = &H000000FF&
FillStyle = 0 'Solid
Height = 300
Index = 1
Left = 600
Top = 0
Width = 615
End
Begin VB.Shape ShapeBar
BackStyle = 1 'Opaque
FillStyle = 0 'Solid
Height = 300
Index = 0
Left = -15
Top = 0
Width = 615
End
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Dim Speed As Integer
Dim YY As Integer ' The Up/Down Direction Variable for the 'BALL'
Dim XX As Integer ' The Left/Right Direction Variable for the 'Ball'
'''''''''''' The IntersectRect API call provides functional Collision Detection
''' lpDestRect receives the RESULTING rectangle where lpSrc1Rect and lpSrc2Rect meet.
''' the function returns 0 if no collision exists, and 1 if there is a collision
''' this is our 'test', in that we simply test if the result = 1 or 0
Private Type RECT ' The RECT type is used to identify borders of bricks and balls
Left As Long ' NOTE That you CANNOT Use the IntersectRect function without RECT!
Top As Long
Right As Long
Bottom As Long
End Type
Private Sub Form_Initialize()
Randomize ' make random truly random
Speed = 14 ' the starting speed (ie. points to move the ball per interval)
YY = Speed ' Assign the speed to the YY (up/down) variable
XX = Speed ' and the Left/Right variable
picBall.Left = Int(Rnd * picBox.Width - picBall.Width) + 4 ' randomly place the ball left to right
End Sub
Private Sub picBox_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
picPaddle.Left = X ' this moves the paddle left and right when you move the mouse (in the box)
End Sub
Private Sub timBallBounce_Timer()
If Speed > 50 Then Speed = 50 ' keep the speed to a semi reasonable value
If YY < 0 Then YY = -Speed ' if the ball is moving UP, re-assign the speed going UP
If YY > 0 Then YY = Speed ' if the ball is moving DOWN, re-assign the speed going DOWN
If XX < 0 Then XX = -Speed ' if the ball is moving LEFT, re-assign the speed going LEFT
If XX > 0 Then XX = Speed ' if the ball is moving right, re-assign the speed going RIGHT
If picBall.Top >= picBox.Height - picBall.Height Then YY = YY * -1 ' bounce off the Bottom
If picBall.Top < 1 Then YY = YY * -1: picBall.Top = 1 ' bounce off the TOP
If picBall.Left <= 1 Then XX = XX * -1 ' bounce off the LEFT
If picBall.Left >= picBox.Width - picBall.Width Then ' bounce off the RIGHT
picBall.Left = picBox.Width - picBall.Width ' sometimes the ball
XX = XX * -1 ' gets stuck on the right wall
End If ' without this little snippit
picBall.Left = picBall.Left + XX ' Actually MOVE THE BALL LEFT or RIGHT
picBall.Top = picBall.Top + YY ' Actually MOVE THE BALL Up or DOWN
End Sub
Private Sub timCollisions_Timer()
Dim Ball As RECT ' The Ball's RECTangle
Dim Brick(55) As RECT ' the Bricks RECTangles
Dim ResRect As RECT ' the Resulting RECTangle of a collision (we dont test this)
Dim Paddle As RECT ' the Paddle's RECTange
Dim bars As Integer
With Paddle ' Make the Paddle RECTangle stay positioned with the
.Top = picPaddle.Top ' actual paddle 'picPaddle'
.Left = picPaddle.Left
.Right = picPaddle.Left + picPaddle.Width
.Bottom = picPaddle.Top + picPaddle.Height
End With
With Ball ' Make the Ball RECTangle stay positioned witht the
.Top = picBall.Top ' actual Ball 'picBall'
.Left = picBall.Left
.Right = picBall.Left + picBall.Width
.Bottom = picBall.Top + picBall.Height
End With
If IntersectRect(ResRect, Ball, Paddle) Then ' A booleanesque 1 fires if the ball and paddle
Speed = Speed + 1 ' intersect. We reverse the UP/DOWN in that case
YY = -Speed
picBall.Left = picBall.Left + Int(Rnd * 28) + 14 ' this helps prevent getting stuck bouncing in the same places
Exit Sub
End If
For bars = 0 To ShapeBar.UBound ' Start a loop... one for each brick (i called them bars here for some reason)
With Brick(bars) ' draw the RECTangle location for each brick
.Top = ShapeBar(bars).Top
.Left = ShapeBar(bars).Left
.Right = ShapeBar(bars).Left + ShapeBar(bars).Width
.Bottom = ShapeBar(bars).Top + ShapeBar(bars).Height
End With
If ShapeBar(bars).Visible = True Then ' if a brick is visible, test for Collision with BALL!
If IntersectRect(ResRect, Ball, Brick(bars)) Then 'A booleanesque 1 fires and we hit a brick
ShapeBar(bars).Visible = False 'Hide that brick we hit
Speed = Speed + 1 'Go faster!
If YY > 0 Then Exit Sub ' Ball moving down? Then let it keep going!
YY = YY * -1 ' Reverse the ball's UP/DOWN direction
End If
End If
DoEvents ' it's a fast loop, but still, lets free windows for giggles
Next bars
End Sub
![[fish] [fish] [fish]](/data/assets/smilies/fish.gif)