这是一种碰撞检测方法,下述属于简化的矩形碰撞检测,若是需要复杂碰撞可以用一个数组来记录大量需要碰撞检测的物体
image1里读入坦克的图片
image2里读入地雷的图片
然后用下面代码即可实现
Private
Sub
Form_KeyPress(KeyAscii
As
Integer)
'按键盘A和D键控制猫图片image1左右移动
If
KeyAscii
=
97
Then
Image1.Left
=
Image1.Left
-
10
If
KeyAscii
=
100
Then
Image1.Left
=
Image1.Left
+
10
'如果坦克图片与地雷图片相遇则提示碰撞到了
If
Image1.Left
+
Image1.Width
>
Image2.Left
Then
If
Image1.Left
<
Image2.Left
+
Image2.Width
Then
If
Image1.Top
+
Image1.Height
>
Image2.Top
Then
If
Image1.Top
<
Image2.Top
+
Image2.Height
Then
MsgBox
"坦克碰到地雷,已经被炸毁了"
End
If
End
If
End
If
End
If
End
Sub
窗体放两个Label控件,一个Timer控件:Dim n As Integer
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyUp
If Label1.Top >0 Then Label1.Top = Label1.Top - 50
Case vbKeyDown
If Label1.Top <ScaleHeight - Label1.Height Then Label1.Top = Label1.Top + 50
Case vbKeyLeft
If Label1.Left >0 Then Label1.Left = Label1.Left - 50
Case vbKeyRight
If Label1.Left <ScaleWidth - Label1.Width Then Label1.Left = Label1.Left + 50
End Select
Call check
End Sub
Private Sub check()
If Abs(Label1.Top - Label2.Top) <= 50 And Abs(Label1.Left - Label2.Left) <= 50 Then
n = n + 1
Label2.Move Rnd * ScaleWidth, Rnd * ScaleHeight
End If
End Sub
Private Sub Form_Load()
KeyPreview = True
Randomize
With Label1
.Caption = ""
.BackColor = vbWhite
.Move (ScaleWidth - .Width) / 2, (ScaleHeight - .Height) / 2, 500, 500
End With
With Label2
.Caption = ""
.BackColor = vbYellow
.Move Rnd * ScaleWidth, Rnd * ScaleHeight, 500, 500
End With
Timer1.Interval = 60000
Timer1.Enabled = True
End Sub
Private Sub Timer1_Timer()
MsgBox "这局对准了" &n &"次黄方块"
Unload Me
End Sub
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)