The Design Interface
The Run Time Interface
This program creates a dice which shows different color on different face. It can be used to play board games when you cannot find a physical dice. It can also be incorporated into VB games that require a dice. Some of the VB games that you can create in Visual Basic are snake chess, monopoly and other board games.
In this program, we use the Randomize function RND to generate random numbers. RND funciton will produce random numbers between 0 and 1. Multiplying RND with 6 will then generate random numbers between 0 and 6 , so 1+RND*6 will produce numbers between 0 and 7, and INT(1+RND*6) will then generate random integers from 1 to 6 because INT is a function that return the integer part of the number. For example, RND=0.4852, 6*RND=2.9112, 1+6*RND=3.9112, so INT(1+6*RND)=INT(3.9112)=3.
To produce different colors, we use the color codes in VB which you can get it from the color palette in the VB properties window. The syntax to assign certain color to an object is object.FillColor=color code.
Color codes in VB take the form of hexadecimal numbers such as HC010F.
By creating shape(n) control array and make them appear according to a generated random number you can then have a perfect virtual dice that is even better than a physical dice!
The Design Interface
The Run Time Interface
The Code
Private Sub Command1_Click()
Dim n as integer
Randomize Timer
n = Int(1 + Rnd * 6)
For i = 0 To 6
Shape1(i).Visible = False
Next
If n = 1 Then
Shape1(3).Visible = True
Shape2.FillColor = &HC0C0C0
End If
If n = 2 Then
Shape1(2).Visible = True
Shape1(4).Visible = True
Shape2.FillColor = &H8080FF
End If
If n = 3 Then
Shape1(2).Visible = True
Shape1(3).Visible = True
Shape1(4).Visible = True
Shape2.FillColor = &H80FF&
End IfIf n = 4 Then
End If
Shape1(0).Visible = True
Shape1(2).Visible = True
Shape1(4).Visible = True
Shape1(6).Visible = True
Shape2.FillColor = &HFFFF00
If n = 5 Then
Shape1(0).Visible = True
Shape1(2).Visible = True
Shape1(3).Visible = True
Shape1(4).Visible = True
Shape1(6).Visible = True
Shape2.FillColor = &HFFFF&
End If
If n = 6 Then
Shape1(0).Visible = True
Shape1(1).Visible = True
Shape1(2).Visible = True
Shape1(4).Visible = True
Shape1(5).Visible = True
Shape1(6).Visible = True
Shape2.FillColor = &HFF00FF
End If
End Sub
Copyright ® 2008 Dr.Liew Voon Kiong . All rights reserved |Contact