挑战一下,发个没颜色的
Dim dx As Single
Dim dy As Single
Dim dz As Single
Dim dp As Single
Private Sub command1_Click()
If Timer1Enabled = True Then
Timer1Enabled = False
Else
Timer1Enabled = True
End If
If Command1Caption = "暂停" Then
Command1Caption = "继续"
Else
Command1Caption = "暂停"
End If
End Sub
Private Sub Command2_Click()
End
End Sub
Private Sub Form_Load()
Timer1Enabled = False
Timer1Interval = 100
dx = 100
dy = -100
dz = 150
dp = -150
End Sub
Private Sub Timer1_Timer()
If Touch Then '如果重叠
dx = -dx
dy = -dy
Shape1Top = Shape1Top + dy
Shape1Left = Shape1Left + dx
dz = -dz
dp = -dp
Shape2Top = Shape2Top + dp
Shape2Left = Shape2Left + dz
Else
If Shape1Left <= 0 Or Shape1Left >= Form1ScaleWidth - Shape1Width Then
dx = -dx
End If
If Shape1Top <= 0 Or Shape1Top >= Form1ScaleHeight - Shape1Height Then
dy = -dy
End If
Shape1Top = Shape1Top + dy
Shape1Left = Shape1Left + dx
If Shape2Left <= 0 Or Shape2Left >= Form1ScaleWidth - Shape2Width Then
dz = -dz
End If
If Shape2Top <= 0 Or Shape2Top >= Form1ScaleHeight - Shape2Height Then
dp = -dp
End If
Shape2Top = Shape2Top + dp
Shape2Left = Shape2Left + dz
End If
End Sub
Private Function Touch() As Boolean
Dim r1 As Double, r2 As Double 'Shape1, Shape2 的半径
r1 = Shape1Width / 2
r2 = Shape2Width / 2
Dim x1 As Double, y1 As Double 'Shape1 的圆心坐标
Dim x2 As Double, y2 As Double 'Shape2 的圆心坐标
x1 = Shape1Top + r1
y1 = Shape1Left + r1
x2 = Shape2Top + r2
y2 = Shape2Left + r2
Dim d As Double 'Shape1, Shape2 的圆心距离
d = Abs(Sqr((y2 - y1) ^ 2 + (x2 - x1) ^ 2))
If d - r1 - r2 <= 0 Then 'Shape1, Shape2 是否重叠
Touch = True
Else
Touch = False
End If
End Function
帮你改好了
出错的原因是shuiping,chuizhi=1不能放在timer里面,要不然每次timer后都会变回来
还有貌似你有几句话是没用的啊,我给你删掉了
Dim Shuiping As Integer, Chuizhi As Integer
Private Sub Form_Load()
Shuiping = 1
Chuizhi = 1
End Sub
Private Sub Timer1_Timer()
Shape1Left = Shape1Left + Shuiping 50
Shape1Top = Shape1Top + Chuizhi 50
If Shape1Left >= Form1ScaleWidth - Shape1Width Or Shape1Left <= 0 Then Shuiping = -Shuiping
If Shape1Top >= Form1ScaleHeight - Shape1Height Or Shape1Top <= 0 Then Chuizhi = -Chuizhi
End Sub
将
窗体
拉到宽7000,高6000添加1个Timer1
,添加1个Picture1控件,高6000,宽6000在Picture1控件里,添加1个Picture2控件代码如下:Option
ExplicitPrivate
VF
As
IntegerPrivate
Hf
As
IntegerPrivate
Vadd
As
IntegerPrivate
Hadd
As
Integer
Private
Sub
Form_Load()Timer1Interval
=
100Hf
=
1VF
=
1RandomizeVadd
=
Int(Rnd
3
+
1)Hadd
=
Int(Rnd
3
+
1)Picture1Width
=
6000Picture1Height
=
6000Picture2Width
=
400Picture2Height
=
400Picture2BackColor
=
&HFF&End
Sub
Private
Sub
Timer1_Timer()Picture2Top
=
Picture2Top
+
VF
Vadd
20Picture2Left
=
Picture2Left
+
Hf
Hadd
20If
Picture2Top
>=
Picture1Height
-
Picture2Height
Then
Picture2Top
=
Picture1Height
-
Picture2Height
VF
=
-VF
Vadd
=
Int(Rnd
3
+
1)
Hadd
=
Int(Rnd
3
+
1)End
If
If
Picture2Top
<=
0
Then
Picture2Top
=
0
VF
=
-VF
Vadd
=
Int(Rnd
3
+
1)
Hadd
=
Int(Rnd
3
+
1)End
If
If
Picture2Left
>=
Picture1Width
-
Picture2Width
Then
Picture2Left
=
Picture1Width
-
Picture2Width
Hf
=
-Hf
Vadd
=
Int(Rnd
3
+
1)
Hadd
=
Int(Rnd
3
+
1)End
If
If
Picture2Left
<=
0
Then
Picture2Left
=
0
Hf
=
-Hf
Vadd
=
Int(Rnd
3
+
1)
Hadd
=
Int(Rnd
3
+
1)End
IfEnd
Sub
启动看看效果,若要成为小球,在Picture2里设置Picture
属性,添加一个小球。
以上就是关于这个VB程序,想让两小球相撞后,互相d开继续运动,最后撞一次变一次色,怎么修改,求指导啊全部的内容,包括:这个VB程序,想让两小球相撞后,互相d开继续运动,最后撞一次变一次色,怎么修改,求指导啊、用vb设计小球碰撞之后反d的程序,运行时结果不对,不知道哪错了、需要做一个小球四处碰壁的VB程序代码等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)