Lucky Dice

Learn to make a Lucky Dice game using Visual Basic 6 with this Tutorial that Celebrates the CESPage 25th Anniversary

This is a simple game, which will introduce you to Control Arrays, Multiple Picture Boxes, Labels, the Circle function and more!

Step 1

Load Microsoft Visual Basic, then select Standard EXE Project if VB5/6, click on the option then click Open
New Project

Step 2

A Blank Form named Form1 should then appear
Blank Form

Step 3

Then from the Visual Basic Components Menu Select the Picture Box Control:
Picture Box Control

Step 4

Draw Two Picture Boxes on the Form
Form with Two Picture Boxes

Step 5

Change the Name of Picture1 to picDieOne and picture2 to picDieTwo and the Visible Properties of both Picture Boxes to False and the AutoRedraw Properties to True
Picture Box Visible and AutoRedraw Property

Step 6

Click on picDieOne (the Leftmost Picture Box) and Copy it
Copy
Then Click on the Form, then Click on Paste. with the dialog "You already have a Control named picDieOne. Do you want to create a control array?" click Yes
Paste
Continue to Paste, but Click on the Form between each Paste, so that there are five copies (Six "picDieOne"s)
Form with Six picDieOne Picture Boxes

Step 7

Then select picDieTwo and repeat Step 6 for this Picture Box so that there are also five copies (Six picDieTwo's) see below:
Form with Six picDieTwo Picture Boxes

Step 8

Then from the Visual Basic Components Menu Select the Command Button Control:
Command Button Control

Step 9

Draw Four Command Buttons on the Form
Form with Picture Boxes and Four Command Buttons

Step 10

Change the Caption Property of Command4 to Quit, Command3 to New Game, Command2 to Roll Two and Command1 to Roll One
Form with Picture Boxes and Four Captioned Command Buttons

Step 11

Then from the Visual Basic Components Menu Select the Label Control:
Label Control

Step 12

Draw a Label on the Form, and change the Name Property of the Label to lblTurn
Form with Picture Boxes, Command Buttons and Label Control

Step 13

Click on the Form and change its Name Property to frmMain then Double Click on the Form of frmMain and type in the Form_Load Sub:


'Initialise Dice
frmMain.Caption = "Lucky Dice"
'Definitions
Dim intTop As Integer
Dim intLeft As Integer
Dim intRadius As Integer
Dim intColour As Integer
Dim intCount As Integer
'Set Circle Settings
intRadius = 60
intColour = vbBlack
'Die One
'Set Die One Settings
intLeft = picDieOne(0).Left
intTop = picDieOne(0).Top
'Setup Picture Boxes (Move, Fill and Visbility)
For intCount = 0 To 5
    picDieOne(intCount).Left = intLeft
    picDieOne(intCount).Top = intTop
    picDieOne(intCount).FillStyle = vbFSSolid
    picDieOne(intCount).Visible = True
    picDieOne(intCount).BackColor = vbWhite
Next intCount
'Render Dice Patterns
'Number One
picDieOne(0).Circle (280, 280), intRadius, intColour
'Number Two
picDieOne(1).Circle (140, 120), intRadius, intColour
picDieOne(1).Circle (400, 400), intRadius, intColour
'Number Three
picDieOne(2).Circle (140, 120), intRadius, intColour
picDieOne(2).Circle (280, 280), intRadius, intColour
picDieOne(2).Circle (440, 440), intRadius, intColour
'Number Four
picDieOne(3).Circle (140, 120), intRadius, intColour
picDieOne(3).Circle (140, 440), intRadius, intColour
picDieOne(3).Circle (440, 120), intRadius, intColour
picDieOne(3).Circle (440, 440), intRadius, intColour
'Number Five
picDieOne(4).Circle (140, 120), intRadius, intColour
picDieOne(4).Circle (140, 440), intRadius, intColour
picDieOne(4).Circle (280, 280), intRadius, intColour
picDieOne(4).Circle (440, 120), intRadius, intColour
picDieOne(4).Circle (440, 440), intRadius, intColour
'Number Six
picDieOne(5).Circle (140, 120), intRadius, intColour
picDieOne(5).Circle (140, 270), intRadius, intColour
picDieOne(5).Circle (140, 440), intRadius, intColour
picDieOne(5).Circle (440, 120), intRadius, intColour
picDieOne(5).Circle (440, 270), intRadius, intColour
picDieOne(5).Circle (440, 440), intRadius, intColour
'Die Two
'Set Die Two Settings
intLeft = picDieTwo(0).Left
intTop = picDieTwo(0).Top
'Setup Picture Boxes (Move, Fill and Visbility)
For intCount = 0 To 5
    picDieTwo(intCount).Left = intLeft
    picDieTwo(intCount).Top = intTop
    picDieTwo(intCount).FillStyle = vbFSSolid
    picDieTwo(intCount).Visible = True
    picDieTwo(intCount).BackColor = vbWhite
Next intCount
'Render Dice Patterns
'Number One
picDieTwo(0).Circle (280, 280), intRadius, intColour
'Number Two
picDieTwo(1).Circle (140, 120), intRadius, intColour
picDieTwo(1).Circle (400, 400), intRadius, intColour
'Number Three
picDieTwo(2).Circle (140, 120), intRadius, intColour
picDieTwo(2).Circle (280, 280), intRadius, intColour
picDieTwo(2).Circle (440, 440), intRadius, intColour
'Number Four
picDieTwo(3).Circle (140, 120), intRadius, intColour
picDieTwo(3).Circle (140, 440), intRadius, intColour
picDieTwo(3).Circle (440, 120), intRadius, intColour
picDieTwo(3).Circle (440, 440), intRadius, intColour
'Number Five
picDieTwo(4).Circle (140, 120), intRadius, intColour
picDieTwo(4).Circle (140, 440), intRadius, intColour
picDieTwo(4).Circle (280, 280), intRadius, intColour
picDieTwo(4).Circle (440, 120), intRadius, intColour
picDieTwo(4).Circle (440, 440), intRadius, intColour
'Number Six
picDieTwo(5).Circle (140, 120), intRadius, intColour
picDieTwo(5).Circle (140, 270), intRadius, intColour
picDieTwo(5).Circle (140, 440), intRadius, intColour
picDieTwo(5).Circle (440, 120), intRadius, intColour
picDieTwo(5).Circle (440, 270), intRadius, intColour
picDieTwo(5).Circle (440, 440), intRadius, intColour
'Select Player One
lblTurn.Caption = "Turn:Player 1"
Command2.Enabled = False 
                                    

Step 14

Double Click on the Command Button of Quit or Command4 and type in the Command4_Click() Sub:


Unload Me 
                                    

Step 15

Double Click on the Command Button of New Game or Command3 and type in the Command3_Click() Sub:


'Ask for new Game
'Reset Counters
intPlayerOne = 0
intPlayerTwo = 0
'Definitions 
Dim intRes As Integer
intRes = MsgBox("Does Player One wish to go First?", vbYesNo + vbQuestion, "Player One First?")
If intRes = vbYes Then GoTo First Else GoTo Second
First:
    lblTurn.Caption = "Turn:Player 1"
    Command1.Enabled = True
    Command2.Enabled = False
    GoTo Fin
Second:
    lblTurn.Caption = "Turn:Player 2"
    Command1.Enabled = False
    Command2.Enabled = True
Fin: 
                                    

Step 16

Double Click on the Command Button of Roll Two or Command2 and type in the Command2_Click() Sub:


'Player Two (Die Two)
'Definitions
Dim intShow As Integer
Dim intCount As Integer
'Roll Dice
Randomize Timer
intShow = Int((5 * Rnd))
For intCount = 0 To intShow
    picDieTwo(intCount).ZOrder (0)
Next intCount
Command1.Enabled = True
Command2.Enabled = False
lblTurn.Caption = "Turn:Player 1"
                                    

Step 17

Double Click on the Command Button of Roll One or Command1 and type in the Command1_Click() Sub:


'Player One (Die One)
'Definitions
Dim intShow As Integer
Dim intCount As Integer
'Roll Dice
Randomize Timer
intShow = Int((5 * Rnd))
For intCount = 0 To intShow
    picDieOne(intCount).ZOrder (0)
Next intCount
Command1.Enabled = False
Command2.Enabled = True
lblTurn.Caption = "Turn:Player 2"
                                    

Step 18

Save the Project, for example prjLuckyDice, into a vacant folder as you have finished the application. Click on Start / Run
Start / Run
This should Start the application
Lucky Dice Running

Step 19

Click on Roll One to Roll the First Die and then Roll Two for the Second Die, this is a simple Lucky Dice game with no scoring or rules. To Exit the application, Click on Quit or End / Stop
End / Stop

You have just created a simple Lucky Dice game, but what about scoring and proper dice rules, you'll have to wait, try and make it have a scoring system/rules yourself and see what else you can achieve with this program.