Home
Return to Home

Games and UtilitiesGames and Utilities
Text ReaderText Reader
Numerical Fill-In Generator
Numerical Fill-In Generator
Yahtzee!
Yahtzee!

Explorers Post 725 - ComputersExplorers Post 725
Memory ProjectMemory
Hangman Project
Hangman
Color Dialog Project
Color Dialog
Paint ProjectPaint
Quadratic Formula ProjectQuadratic Formula
Dice Roller ProjectDice Roller

Dice Roller

OVERVIEW:

In this Explorers session we created a Dice Roller program that utilizes Visual Basic's Random commands and drawing commands. The number of dice can vary from 1 to 5 and the number of sides on the die can vary from 4 to 9 (yes there are more than just 6-sided dice).

Dice Roller

Due to the drawing methods utilized, this program is designed to be made in Visual Basic 5.0 or 6.0 only.

CONTROLS:

Open Visual Basic and select Standard EXE. Name the form frmDiceRoller and its Caption to Dice Roller.

Label1 - Label control, Caption set to "Number of Dice"
Label2 - Label control, Caption set to "Sides on Die"
scrNumberDice - HScrollBar, Min value of 1, Max value of 5
scrNumberSides - HScrollBar, Min value of 4, Max value of 9
picDice - PictureBox, Set the default properties since it will be the core for the control array.

AutoRedrawTrue
BackColor&H00FFFFFF& (white)
FillColor&H00C0C000& (teal)
ForeColor&H00C00000& (dark blue)
Index0 (additional dice will be created automatically)
Height855
Left240
Top1440
Width855

CODE:

Dim myDice() As Integer

Private Sub cmdRollDice_Click()
Dim tmpI As Integer

    'create dice that are used
    For tmpI = 1 To scrNumberDice.Value
        myDice(tmpI) = Int(Rnd * scrNumberSides.Value) + 1
    Next tmpI
   
    For tmpI = 0 To scrNumberDice.Value - 1
        picDice(tmpI).Scale (0, 100)-(100, 0) 'upper left to lower right
        picDice(tmpI).Cls 'erase the previous die value
        Call DrawCircles(tmpI, myDice(tmpI + 1), 10)
    Next tmpI
End Sub

Private Sub Form_Load()
Dim tmpI As Integer
    For tmpI = 1 To scrNumberDice.Max - 1 'dynamically load additional dice in row
        Load picDice(tmpI)
        picDice(tmpI).Left = picDice(0).Left + tmpI * (picDice(0).Width + 120)
    Next tmpI
    Label2.Caption = "Sides on Die = " & scrNumberSides.Value
    ReDim myDice(scrNumberDice) As Integer 'more dynamic
    Call ShowDice(scrNumberDice.Value)
    Randomize 'random dice rolls
End Sub

Private Sub ShowDice(NumberOfDice As Integer)
Dim tmpI As Integer

    'show desired number of dice
    For tmpI = 1 To NumberOfDice - 1
        picDice(tmpI).Visible = True
        picDice(tmpI).Cls
    Next tmpI
   
    'hide the rest
    For tmpI = NumberOfDice To scrNumberDice.Max - 1
        picDice(tmpI).Cls
        picDice(tmpI).Visible = False
    Next tmpI
End Sub

Private Sub scrNumberDice_Change()
    ReDim myDice(scrNumberDice) As Integer 'more dynamic
    Call ShowDice(scrNumberDice.Value)
End Sub

Private Sub DrawCircles(picDiceIndex As Integer, NumberOfCircles As Integer, myRadius As Integer)
Dim DrawCenter As Boolean
    Select Case NumberOfCircles
        Case 1 'center dot only
            DrawCenter = True
        Case 2, 3 'upper left corner and lower right corner
            If NumberOfCircles = 3 Then DrawCenter = True
            picDice(picDiceIndex).Circle (20, 80), myRadius
            picDice(picDiceIndex).Circle (80, 20), myRadius
        Case 4, 5 'four corners
            If NumberOfCircles = 5 Then DrawCenter = True
            picDice(picDiceIndex).Circle (20, 80), myRadius
            picDice(picDiceIndex).Circle (20, 20), myRadius
            picDice(picDiceIndex).Circle (80, 80), myRadius
            picDice(picDiceIndex).Circle (80, 20), myRadius
        Case 6, 7 'three on each side
            If NumberOfCircles = 7 Then DrawCenter = True
            picDice(picDiceIndex).Circle (20, 80), myRadius
            picDice(picDiceIndex).Circle (20, 50), myRadius
            picDice(picDiceIndex).Circle (20, 20), myRadius
            picDice(picDiceIndex).Circle (80, 80), myRadius
            picDice(picDiceIndex).Circle (80, 50), myRadius
            picDice(picDiceIndex).Circle (80, 20), myRadius
        Case 8, 9 'three on each side, one at center top, one at center bottom
            If NumberOfCircles = 9 Then DrawCenter = True
            picDice(picDiceIndex).Circle (20, 80), myRadius
            picDice(picDiceIndex).Circle (20, 50), myRadius
            picDice(picDiceIndex).Circle (20, 20), myRadius
            picDice(picDiceIndex).Circle (80, 80), myRadius
            picDice(picDiceIndex).Circle (80, 50), myRadius
            picDice(picDiceIndex).Circle (80, 20), myRadius
            picDice(picDiceIndex).Circle (50, 20), myRadius
            picDice(picDiceIndex).Circle (50, 80), myRadius
    End Select
   
    If DrawCenter Then 'called in many circumstances
        picDice(picDiceIndex).Circle (50, 50), myRadius
    End If
End Sub

Private Sub scrNumberSides_Change()
    Label2.Caption = "Sides on Die = " & scrNumberSides.Value
End Sub




Hosted by www.Geocities.ws

1