Lucky Dice

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

Printer Friendly Download Tutorial (63.5KB) Download Source Code (7KB)

Step 1

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

New Project

Step 2

A Blank Form named Form1 should then appear, see below:

Form 1

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 as shown below:

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, see below:

Picture Name and AutoRedraw

Step 6

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

Six picDieOnes

Step 7

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

Dice Picture Boxes on Form

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 as shown below:

Form with Dice and Command Buttons

Step 10

Change the Caption Property of Command4 to Quit, Command3 to New Game, Command2 to Roll Two and Command1 Roll One, as shown below:

Form with Dice and Labeled 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, position shown below:

Dice, Label and Buttons

Step 13

Click on the Form and change its Name Property to frmMain then Double Click on the Form (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 "Quit" (Command4) and type in the Command4_Click() Sub:

Unload Me 

Step 15

Double Click on the Command Button "New Game (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 "Roll Two" (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 "Roll One" (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 or Press F5 and the below will appear:

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 Dice Game with no scoring or rules. To Quit click on the Button Labeled Quit.

You have just created a simple 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.

Copyright Comentsys © 1997 - 2009, All rights reserved. About | Contact | Link to Page
Valid XHTML 1.1! Valid CSS Level Double-A conformance icon, W3C-WAI Web Content Accessibility Guidelines 1.0 This website is ICRA rated