In this activity, we will design an application that simulates the rolling of a die.  (one die, two dice!)

Design the Interface

On the interface we will have a picture of a die.  When the user clicks on it, the image will change to the new picture.

Set up your interface like this: 

Here is a set of die images that you can save to your home drive as die1.jpg, die2.jpg, die3.jpg etc. Save them into a folder on your home drive -  in this case it is h:\vb\dieroll (You can save them elsewhere if you want, but make sure you know the path to where they are)

Write the code

First we will get a random number from 1 to 6.  We will store it in a variable that we will call DIeRoll. You can think of a variable as a container to store information that you might want to use later.  Then depending on the value of the random number, we will select an image to display.

Here is the code we need, followed by an explanation of each line: (Double click the image to add the code - you can then copy and paste it)

Save your project and form as dieroll

Private Sub imgDie_Click()

Dim DieRoll as Integer

Randomize

DieRoll = Int (Rnd*6)+1

Select Case DieRoll

Case 1

imgDie.picture=LoadPicture("H:\vb\dieroll\die1.jpg")

Case 2

imgDie.picture=LoadPicture("H:\vb\dieroll\die2.jpg")

Case 3

imgDie.picture=LoadPicture("H:\vb\dieroll\die3.jpg")

Case 4

imgDie.picture=LoadPicture("H:\vb\dieroll\die4.jpg")

Case 5

imgDie.picture=LoadPicture("H:\vb\dieroll\die5.jpg")

Case 6

imgDie.picture=LoadPicture("H:\vb\dieroll\die6.jpg")

End Select

End Sub

Note how the code is indented in blocks - this is a programming convention that makes your code much easier to understand and correct errors. 
Private Sub imgDie_Click() The procedure runs when the user clicks on imgDie
Dim DieRoll as Integer When using a variable, you need to declare it first.  Declaring tells the application about the variables you will be using.
Randomize Initializes the random-number generator
DieRoll = Int (Rnd*6)+1 Rnd gives a random number greater than or equal to 0 but less than 1
* means multiply
Multiplying rnd by 6 gives a value between 0 and 5.99999
Int means to chop off the decimal part, so we have a whole number between 0 and 5
Adding 1 gives the random number between 1 and 6
DieRoll=  means to assign (or store) the value of Int(Rnd*6)+1 in DieRoll  
Select Case RollDie Visual Basic then compares value in RollDie with the values for each Case in the structure. If there is a match, it executes the statements associated with that Case.  Once it has done that it jumps to the End Select statement and moves on.
Case 1 If DieRoll contains 1, the next statement will be executed.  Then it will jump to End Select
imgDie.picture=
LoadPicture("H:\vb\dieroll\die1.jpg")
Loads the picture for die1 into ImgDie
H:\vb\dieroll\die1.jpg is where it is stored - you will need to alter the path if this is not where you saved your images.
Case 2 If DieRoll contains 2, the next statement will be executed, then End Select
imgDie.picture=
LoadPicture("H:\vb\dieroll\die2.jpg")
 
  etc
End Select End of select case statements
End Sub End of procedure

Test your program

Sometimes it appears that the die has not rolled. 

Why is this?  How might the problem be overcome?


Design a coin flipping simulation. 

The problem of the die not appearing to always roll will be greater in the coin flipping simulation as there are only 2 possible outcomes.  Find a way to show the user that the flipping has occurred.

Here are two coin images you can use (copy and paste them to your application folder):

Modify your program so that it simulates the tossing of 2 coins.