我想设计一个打飞机的小游戏,但出现了问题,请先看代码:
Private Sub Form_Activate()
Dim k, k1, n As Integer, i, i1, j As Double
k = 1: k1 = 1Do While k1 > 0
Command2.Top = Command2.Top - 100
If Command2.Top = 0 Then
Command2.Top = 9000: Command2.Left = Command1.Left + 590
End If
w = DoEvents
For i = 1 To 5000000
Next i
n = Int(Rnd * 8)
Command3(n).Visible = True
Command3(n).Top = Command3(n).Top + 100
w = DoEventsLoop
For i1 = 0 To 7If (Command2.Top >= Command3(i1).Top And Command2.Top <= Command3(i1).Top + 490) And (Command2.Left > Command3(i1).Left And Command2.Left <= Command3(i1).Left + 1210) Then
Command2.Top = 9000: Command2.Left = Command1.Left + 590
Command3(i1).Visible = False
Command3(i1).Top = 0
ElseIf Command3(i1).Top >= 9000 Then
Command3(i1).Top = 0
End If
Next i1
End SubPrivate Sub Form_Load()form1.Height = 11000
form1.Width = 10000
Picture1.Height = form1.Height
Picture1.Width = form1.Width
Picture1.Top = 0
Picture1.Left = 0
End SubPrivate Sub picture1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 65 And Command1.Left >= -400 Then
Command1.Left = Command1.Left - 300ElseIf KeyCode = 68 And Command1.Left <= 9040 Then
Command1.Left = Command1.Left + 300End If
End Sub
其中command为我控制的飞机,command2为炮弹,command3为控件数组,代表的是敌机,程序运行后,敌机将从顶端向下坠落,然后会被炮弹击落.请注意程序中的这段代码:
For i1 = 0 To 7If (Command2.Top >= Command3(i1).Top And Command2.Top <= Command3(i1).Top + 490) And (Command2.Left > Command3(i1).Left And Command2.Left <= Command3(i1).Left + 1210) Then
Command2.Top = 9000: Command2.Left = Command1.Left + 590
Command3(i1).Visible = False
Command3(i1).Top = 0
其中command3的height和width分别为495,1215
我设计它的意思是当任何一个command3(即任何一架敌机)与command2(即炮弹)向遇是,也就command3和command重合,敌机和炮弹均消失,即visible=false,但为何程序运行起来两者都不消失呢?
望大家一定帮忙啊!本人万分着急!
Private Sub Form_Activate()
Dim k, k1, n As Integer, i, i1, j As Double
k = 1: k1 = 1Do While k1 > 0
Command2.Top = Command2.Top - 100
If Command2.Top = 0 Then
Command2.Top = 9000: Command2.Left = Command1.Left + 590
End If
w = DoEvents
For i = 1 To 5000000
Next i
n = Int(Rnd * 8)
Command3(n).Visible = True
Command3(n).Top = Command3(n).Top + 100
w = DoEventsLoop
For i1 = 0 To 7If (Command2.Top >= Command3(i1).Top And Command2.Top <= Command3(i1).Top + 490) And (Command2.Left > Command3(i1).Left And Command2.Left <= Command3(i1).Left + 1210) Then
Command2.Top = 9000: Command2.Left = Command1.Left + 590
Command3(i1).Visible = False
Command3(i1).Top = 0
ElseIf Command3(i1).Top >= 9000 Then
Command3(i1).Top = 0
End If
Next i1
End SubPrivate Sub Form_Load()form1.Height = 11000
form1.Width = 10000
Picture1.Height = form1.Height
Picture1.Width = form1.Width
Picture1.Top = 0
Picture1.Left = 0
End SubPrivate Sub picture1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 65 And Command1.Left >= -400 Then
Command1.Left = Command1.Left - 300ElseIf KeyCode = 68 And Command1.Left <= 9040 Then
Command1.Left = Command1.Left + 300End If
End Sub
其中command为我控制的飞机,command2为炮弹,command3为控件数组,代表的是敌机,程序运行后,敌机将从顶端向下坠落,然后会被炮弹击落.请注意程序中的这段代码:
For i1 = 0 To 7If (Command2.Top >= Command3(i1).Top And Command2.Top <= Command3(i1).Top + 490) And (Command2.Left > Command3(i1).Left And Command2.Left <= Command3(i1).Left + 1210) Then
Command2.Top = 9000: Command2.Left = Command1.Left + 590
Command3(i1).Visible = False
Command3(i1).Top = 0
其中command3的height和width分别为495,1215
我设计它的意思是当任何一个command3(即任何一架敌机)与command2(即炮弹)向遇是,也就command3和command重合,敌机和炮弹均消失,即visible=false,但为何程序运行起来两者都不消失呢?
望大家一定帮忙啊!本人万分着急!
一:使用Pixel坐标,不要用VB默认的TWIN坐标。这样比较直观,毕竟你的“飞机”不可能移动1/10个点。
二:将“飞机”和“炮弹”的坐标改成中心坐标:
Plane.X=Plane.left+Plane.Width\2; Plane.Y=Plane.Top+Plane.Height\2
Shoot.X=Shoot.left+Shoot.Width\2; Shoot.Y=Shoot.Top+Shoot.Height\2
三:判断的时候要考虑“物体”的大小:
If Abd(Plane.X-Shoot.X)<=(Plane.Width+Shoot.Width)\2 And Abd(Plane.Y-Shoot.Y)<=(Plane.Height+Shoot.Height)\2 Then
'表示打中了,下面写你打中后的代码
...
End If
其实主要是一个框架的问题,在写代码前要考虑清楚
还有你的PICTUREBOX的SCALEMODE属性也改为PIXEL。
这样,如果你的窗体是满屏的话,宽度和长度就是你显示器的分辨率了。如:1024x768