如何为vb6形式的形状指定随机颜色

时间:2014-07-12 07:01:29

标签: random timer vb6

我正在使用VB6.0开发游戏,我想在窗体背景中生成随机移动的形状(圆形,正方形,矩形,椭圆形等),颜色不同。我正在使用计时器和形状来实现这一点。我的贡献在下面提供。

Private Sub Timer1_Timer()
    For i = 0 To 20
        s_left = Round(Rnd * 20050)
        s_top = Round(Rnd * 860)
        shape(i).Left = s_left
        shape(i).Top = s_top
        shape(i).Visible = True
    Next i
End Sub

现在的问题是,我只能使用一种形状和一种颜色,是否有可能在此Timer1_Timer()中选择随机颜色和形状?

1 个答案:

答案 0 :(得分:1)

'生成随机颜色的函数

Public Function RandomRGBColor() As Long
    RandomRGBColor = RGB( _
        Int(Rnd() * 256), _
        Int(Rnd() * 256), _
        Int(Rnd() * 256))
End Function

您的代码应按如下方式更改以获取请求的结果

dim shape_style as integer
Private Sub Timer1_Timer()
For i = 0 To 20
shape_style= Round(Rnd * 5)
s_left = Round(Rnd * 20050)
s_top = Round(Rnd * 860)
shape(i).Left = s_left
shape(i).Top = s_top
shape(i).Visible = True
shape(i).fillcolor=RandomRGBColor()
shape(i).shape=shape_style
Next i
End Sub