Rolling Dice Program

In this post, I will teach you to create a rolling dice program in visual basic 6. The program will consist of a single dice with six dots representing numbers between 1 to 6 and button to roll the dice. After you have clicked the rolling button, the dice will give you a random number between 1 to 6.

Advertisements

Prerequisites

To create this program, you need to have a computer that runs at least Windows XP Sp3 or above and Visual basic 6.0. I am running a virtual machine with Windows XP Sp3 which will also work, in case, you have a higher version of windows system.

Design Form for Rolling Dice program

The first step is to open the Visual basic 6 program and create a new standard.exe form. In this form we will create shapes and buttons. Assuming that you know about toolbar, click on the Shape object from the toolbar and create a rectangle shape.

Figure 1 - Click Shape object and draw a rectangle shape
Figure 1 – Click Shape object and draw a rectangle shape

Form Properties

Name: frmRollDIce
Caption: Rolling Dice
Height: 7350
Width:9750

Now, you can set the properties of the above shape as given below.

Name: Dice
Fillcolor: &H00FFFFFF&
Fillstyle: 0-Solid
Shape: 4-Rounded Rectangle
Height: 4215
Width: 4215

Now, you can create black round shapes to simulate an actual dice. Since, we cannot show all the face of the dice, show only one face with 6 dots, that is, the maximum number that the dice can get when you roll it.

Click on the shape object again and create a single small rectangle inside dice shape you created earlier. Set the properties of this object as follows.

Name: Shape1
Fillcolor: &H00000000&
Fillstyle: 0-Solid
Shape: 3-Circle
Height:735
Width:735

Copy the black round shape and paste in the dice shape 5 more times. When you try to paste the shape object, you should receive a warning shown below.

Advertisements
Figure 2 - Warning when try to copy the existing shape
Figure 2 – Warning when try to copy the existing shape

Click on Yes and continue to copy the black round shape 5 more times and arrange them in the following order. You should end up with following form design.

Figure 3- Positions for black circles for rolling dice program
Figure 3- Positions for black circles for rolling dice program

The arrangement of dice is up to you, and you may use the Format tab on Visual basic 6 for align the back circles.

Button to Roll Dice

Now it is time to add button for rolling the dice and getting a random number. Add a button object to the form and set the following properties.

Name: cmdRoll
Caption: Roll Dice
Backcolor: &H0000FFFF&
Style: 1-Graphical
Font: MS Sans Serief 14 Bold

Your final form should look like the following.

Figure 4 - Final form design
Figure 4 – Final form design

I have added a background color to the main form, you can also change the look and feel of your form design or leave it as it is.

Code for Rolling Dice Program

The only code required here is for the button – Roll Dice because all action is going to happen when you click on the button only.

Add the following code to the button control.

Private Sub cmdRoll_Click()
n = Int(1 + Rnd * 6)
For i = 0 To 6
Shape1(i).Visible = False
Next

If n = 1 Then
Shape1(6).Visible = True
End If

If n = 2 Then
Shape1(1).Visible = True
Shape1(4).Visible = True
End If

If n = 3 Then
Shape1(0).Visible = True
Shape1(6).Visible = True
Shape1(5).Visible = True
End If
                        
If n = 4 Then
Shape1(0).Visible = True
Shape1(2).Visible = True
Shape1(3).Visible = True
Shape1(5).Visible = True
End If

If n = 5 Then
Shape1(0).Visible = True
Shape1(2).Visible = True
Shape1(3).Visible = True
Shape1(5).Visible = True
Shape1(6).Visible = True
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(3).Visible = True
End If
End Sub

You program is completed and now you can try to roll the dice and see how it works. If there is a problem, recheck the code.

Advertisements

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.