VB2022 VB2019 VB6 VB Sample Code About Us

Dice with Different Colors


This program creates a dice that displays different colors on each face. It can be used to play board games when you can't find a physical dice. You may incorporate it into any VB games that require dice, such as Snakes and Ladders, Monopoly, and more.

Virtual Dice Solution

Create a fully functional dice with different colors on each face for your board games.

Random Number Generation

Uses VB6's Rnd function to generate truly random dice rolls between 1-6.

Color Customization

Easily customize dice face colors using VB6's hexadecimal color codes.

Reusable Code

Simple, well-structured code that can be integrated into any VB6 game project.

How It Works

In this program, we use the Randomize function and Rnd to generate random numbers:

Example: If Rnd = 0.4852, then:
6 * Rnd = 2.9112
1 + 6 * Rnd = 3.9112
Int(1 + 6 * Rnd) = Int(3.9112) = 3

To produce different colors, we use VB6's color codes which you can get from the color palette in the VB properties window. The syntax to assign a color to an object is:

object.FillColor = color_code

Color codes in VB take the form of hexadecimal numbers such as &hc010f.

By creating a shape(n) control array and making them appear according to the generated random number, you can create a perfect virtual dice that's even better than a physical dice!

Design Interface

Dice with Colors Design Interface

The design layout in Visual Basic 6 showing the shape controls and button

Runtime Interface

Dice with Colors Runtime

The dice in action - rolling a 5 with yellow color

Source Code

Private Sub Command1_Click()
    Dim n As Integer
    Randomize Timer
    n = Int(1 + Rnd * 6)
    
    ' Hide all dice dots initially
    For i = 0 To 6
        Shape1(i).Visible = False
    Next
    
    ' Show dots based on the dice roll
    If n = 1 Then
        Shape1(3).Visible = True
        Shape2.FillColor = &hc0c0c0  ' Silver
        
    ElseIf n = 2 Then
        Shape1(2).Visible = True
        Shape1(4).Visible = True
        Shape2.FillColor = &h8080ff  ' Light Blue
        
    ElseIf n = 3 Then
        Shape1(2).Visible = True
        Shape1(3).Visible = True
        Shape1(4).Visible = True
        Shape2.FillColor = &h80ff&  ' Green
        
    ElseIf n = 4 Then
        Shape1(0).Visible = True
        Shape1(2).Visible = True
        Shape1(4).Visible = True
        Shape1(6).Visible = True
        Shape2.FillColor = &hffff00  ' Yellow
        
    ElseIf 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&  ' Bright Yellow
        
    ElseIf 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  ' Magenta
    End If
End Sub