主题:圆形轨道2球相遇的程序改成方形轨道用VB咋做?
RT,就是把轨道改成方形或者椭圆的都行,改变后小球能按新的轨道运动。
代码为:
Dim c1 As Integer
Dim c2 As Integer
Dim v1 As Integer
Dim v2 As Integer
Dim d0 As Integer
Dim d1 As Integer
Dim t As Integer
Dim n As Integer
Private Sub Command1_Click()
Timer1.Enabled = True
End Sub
Private Sub Command2_Click()
Shape3.Visible = True
Shape4.Visible = True
d0 = Val(Text1.Text)
d1 = Val(Text2.Text)
v1 = Val(Text3.Text)
v2 = Val(Text4.Text)
c1 = 0
c2 = (d1 / d0) * 360
X1 = 5000 + 2250 * Sin(c1 * 3.14 / 180) - 250
Y1 = 5000 + 2250 * Cos(c1 * 3.14 / 180) - 250
Shape3.Move X1, Y1
X2 = 5000 + 2250 * Sin(c2 * 3.14 / 180) - 250
Y2 = 5000 + 2250 * Cos(c2 * 3.14 / 180) - 250
Shape4.Move X2, Y2
If Combo2.Text = "顺时针" Then
Image1.Picture = LoadPicture(App.Path + "\left.bmp")
Else
Image1.Picture = LoadPicture(App.Path + "\right.bmp")
End If
If Combo3.Text = "顺时针" Then
Image2.Picture = LoadPicture(App.Path + "\down.bmp")
Else
Image2.Picture = LoadPicture(App.Path + "\up.bmp")
End If
End Sub
Private Sub Command3_Click()
Call Command2_Click
Timer1.Enabled = False
t = 0
n = 0
Text5.Text = ""
Text6.Text = ""
End Sub
Private Sub Command4_Click()
End
End Sub
Private Sub Form_Load()
Combo2.AddItem "顺时针"
Combo2.AddItem "逆时针"
Combo3.AddItem "顺时针"
Combo3.AddItem "逆时针"
Combo2.Text = "逆时针"
Combo3.Text = "逆时针"
End Sub
Private Sub Timer1_Timer()
t = t + 1
s1 = t * v1
s2 = t * v2
If Combo2.Text = "逆时针" And Combo3.Text = "顺时针" Then
direc = -1 '反向
diff = d1 '相距d
End If
If Combo2.Text = "顺时针" And Combo3.Text = "逆时针" Then
direc = -1 '反向
diff = d0 - d1 '相距轨道长度-d
End If
If Combo2.Text = "顺时针" And Combo3.Text = "顺时针" Then
direc = 1 '同向
If Text3.Text >= Text4.Text Then '蓝球比红球快
diff = d0 - d1
Else
diff = d1
End If
End If
If Combo2.Text = "逆时针" And Combo3.Text = "逆时针" Then
direc = 1 '同向
If Text3.Text >= Text4.Text Then '蓝球比红球快
diff = d1
Else
diff = d0 - d1
End If
End If
If Combo2.Text = "逆时针" Then '蓝球
c1 = s1 / d0 * 360
Else
c1 = (-1) * s1 / d0 * 360
End If
If Combo3.Text = "顺时针" Then '红球
c2 = (d1 - s2) / d0 * 360
Else
c2 = (d1 + s2) / d0 * 360
End If
X1 = 5000 + 2250 * Sin(c1 * 3.14 / 180) - 250
Y1 = 5000 + 2250 * Cos(c1 * 3.14 / 180) - 250
Shape3.Move X1, Y1
X2 = 5000 + 2250 * Sin(c2 * 3.14 / 180) - 250
Y2 = 5000 + 2250 * Cos(c2 * 3.14 / 180) - 250
Shape4.Move X2, Y2
If direc = 1 Then '同向
x = Int(Abs(Abs(s1 - s2) - diff))
Else
x = Int(Abs(Abs(s1 + s2) - diff))
End If
If x Mod d0 = 0 Then
Shape3.FillColor = vbCyan
Shape4.FillColor = vbCyan
n = n + 1
Text6.Text = t
Text5.Text = n
yn = MsgBox("是否继续?", vbYesNo, "已相遇")
Shape3.FillColor = vbBlue
Shape4.FillColor = vbRed
If yn = 7 Then
Call Command2_Click
Timer1.Enabled = False
t = 0
n = 0
Text5.Text = ""
Text6.Text = ""
Exit Sub
End If
End If
End Sub
代码为:
Dim c1 As Integer
Dim c2 As Integer
Dim v1 As Integer
Dim v2 As Integer
Dim d0 As Integer
Dim d1 As Integer
Dim t As Integer
Dim n As Integer
Private Sub Command1_Click()
Timer1.Enabled = True
End Sub
Private Sub Command2_Click()
Shape3.Visible = True
Shape4.Visible = True
d0 = Val(Text1.Text)
d1 = Val(Text2.Text)
v1 = Val(Text3.Text)
v2 = Val(Text4.Text)
c1 = 0
c2 = (d1 / d0) * 360
X1 = 5000 + 2250 * Sin(c1 * 3.14 / 180) - 250
Y1 = 5000 + 2250 * Cos(c1 * 3.14 / 180) - 250
Shape3.Move X1, Y1
X2 = 5000 + 2250 * Sin(c2 * 3.14 / 180) - 250
Y2 = 5000 + 2250 * Cos(c2 * 3.14 / 180) - 250
Shape4.Move X2, Y2
If Combo2.Text = "顺时针" Then
Image1.Picture = LoadPicture(App.Path + "\left.bmp")
Else
Image1.Picture = LoadPicture(App.Path + "\right.bmp")
End If
If Combo3.Text = "顺时针" Then
Image2.Picture = LoadPicture(App.Path + "\down.bmp")
Else
Image2.Picture = LoadPicture(App.Path + "\up.bmp")
End If
End Sub
Private Sub Command3_Click()
Call Command2_Click
Timer1.Enabled = False
t = 0
n = 0
Text5.Text = ""
Text6.Text = ""
End Sub
Private Sub Command4_Click()
End
End Sub
Private Sub Form_Load()
Combo2.AddItem "顺时针"
Combo2.AddItem "逆时针"
Combo3.AddItem "顺时针"
Combo3.AddItem "逆时针"
Combo2.Text = "逆时针"
Combo3.Text = "逆时针"
End Sub
Private Sub Timer1_Timer()
t = t + 1
s1 = t * v1
s2 = t * v2
If Combo2.Text = "逆时针" And Combo3.Text = "顺时针" Then
direc = -1 '反向
diff = d1 '相距d
End If
If Combo2.Text = "顺时针" And Combo3.Text = "逆时针" Then
direc = -1 '反向
diff = d0 - d1 '相距轨道长度-d
End If
If Combo2.Text = "顺时针" And Combo3.Text = "顺时针" Then
direc = 1 '同向
If Text3.Text >= Text4.Text Then '蓝球比红球快
diff = d0 - d1
Else
diff = d1
End If
End If
If Combo2.Text = "逆时针" And Combo3.Text = "逆时针" Then
direc = 1 '同向
If Text3.Text >= Text4.Text Then '蓝球比红球快
diff = d1
Else
diff = d0 - d1
End If
End If
If Combo2.Text = "逆时针" Then '蓝球
c1 = s1 / d0 * 360
Else
c1 = (-1) * s1 / d0 * 360
End If
If Combo3.Text = "顺时针" Then '红球
c2 = (d1 - s2) / d0 * 360
Else
c2 = (d1 + s2) / d0 * 360
End If
X1 = 5000 + 2250 * Sin(c1 * 3.14 / 180) - 250
Y1 = 5000 + 2250 * Cos(c1 * 3.14 / 180) - 250
Shape3.Move X1, Y1
X2 = 5000 + 2250 * Sin(c2 * 3.14 / 180) - 250
Y2 = 5000 + 2250 * Cos(c2 * 3.14 / 180) - 250
Shape4.Move X2, Y2
If direc = 1 Then '同向
x = Int(Abs(Abs(s1 - s2) - diff))
Else
x = Int(Abs(Abs(s1 + s2) - diff))
End If
If x Mod d0 = 0 Then
Shape3.FillColor = vbCyan
Shape4.FillColor = vbCyan
n = n + 1
Text6.Text = t
Text5.Text = n
yn = MsgBox("是否继续?", vbYesNo, "已相遇")
Shape3.FillColor = vbBlue
Shape4.FillColor = vbRed
If yn = 7 Then
Call Command2_Click
Timer1.Enabled = False
t = 0
n = 0
Text5.Text = ""
Text6.Text = ""
Exit Sub
End If
End If
End Sub