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
Step 2
Step 3
Step 4
Step 5
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
Step 6
picDieOne
(the Leftmost Picture Box) and Copy it
picDieOne
"s)
Step 7
picDieTwo
and repeat Step 6 for this Picture Box so that there are
also five copies (Six picDieTwo
's) see below:
Step 8
Step 9
Step 10
Caption
Property of Command4
to Quit
, Command3
to New Game
,
Command2
to Roll Two
and Command1
to Roll One
Step 11
Step 12
Name
Property of the
Label to lblTurn
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
Step 19
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.