VB题目:使用定时器模拟交通红绿灯,红绿黄三种灯交替变换形成动画效果。高人帮我编编吧

2025年05月05日 23:56
有1个网友回答
网友(1):

Option Explicit

Dim nCount As Integer

Private Const T_Inteval As Integer = 500 '延时(1拍数)
Private Const R_DELAY As Integer = 20 '红延时拍数
Private Const Y_DELAY As Integer = 10 '黄延时拍数
Private Const G_DELAY1 As Integer = 10 '绿延时1拍数
Private Const G_DELAY2 As Integer = 10 '绿延时2拍数

Private Sub Form_Load()
'Shape1.FillColor = &HFF&
'Shape2.FillColor = &HFFFF&
'Shape3.FillColor = &HFF00&

Shape1.FillColor = &H40&
Shape2.FillColor = &H4040&
Shape3.FillColor = &H4000&

nCount = 0
Timer1.Interval = T_Inteval
End Sub

Private Sub Timer1_Timer()
nCount = nCount + 1
If nCount > 0 And nCount <= R_DELAY Then
'亮红灭黄绿
Shape1.FillColor = &HFF&
Shape2.FillColor = &H4040&
Shape3.FillColor = &H4000&
End If
If nCount > R_DELAY And nCount <= R_DELAY + Y_DELAY Then
'闪黄灭红绿
Shape1.FillColor = &H40&
If nCount Mod 2 = 0 Then
Shape2.FillColor = &H4040&
Else
Shape2.FillColor = &HFFFF&
End If
Shape3.FillColor = &H4000&
End If
If nCount > R_DELAY + Y_DELAY And nCount <= R_DELAY + Y_DELAY + G_DELAY1 Then
'亮绿灭红黄
Shape1.FillColor = &H40&
Shape2.FillColor = &H4040&
Shape3.FillColor = &HFF00&
End If
If nCount > R_DELAY + Y_DELAY + G_DELAY1 And nCount <= R_DELAY + Y_DELAY + G_DELAY1 + G_DELAY2 Then
'闪绿
If nCount Mod 2 = 0 Then
Shape3.FillColor = &H4000&
Else
Shape3.FillColor = &HFF00&
End If
If nCount = R_DELAY + Y_DELAY + G_DELAY1 + G_DELAY2 Then
nCount = 1
End If
End If

End Sub