This is a simple slot machine which does not resemble the real machines played in a casino. However, it does demonstrate the concept of randomness and probability in an actual slot machine. Slot machines is a game of chance , many different outcomes will appear when the player press the play button.
In this program, you draw an array of nine shapes ,Visual Basic will automatically label the shapes as shape1(0), shape1(1), shape1(2), shape1(3), shape1(4), shape1(5), shape1(6), shape1(7) and shape1(8) respectively. Arrange the shapes into three rows. Write the code so that only three types of shapes appear randomly. Here we want to show only square, oval and rectangle. The appearance can be achieved at runtime using the Shape properties. For example, Shape1(o).Shape=0 means it is a rectangle, Shape1(o).Shape=1 is a square and Shape1(o).Shape=2 is an oval shape. The color of the shapes are controlled by the FillColor property of the shapes. For example, Shape1(0).FillColor=vbRed will give the shape a red color. Besides, the default FillStyle property is transparent, therefore you need to set the FillStyle property to solid so that the colors can show up. The design interface is shown below:
The Design Interface
Randomness can be achieved by using the RND function. We also insert a timer to create the animated effect of a slot machine. The time interval is set to 10 so that the shapes change at a fast rate thus creates the illusion of animation. The program also using a variable x to control the timer so that it can be stopped when x achieve certain value, otherwise the program will loop forever.
The purpose of this program is just to show how different shapes can appear randomly, therefore many advanced features of a slot machine such as the amount of bet are not programmed here. Those features are available in the professional slot machine.
'To start the timer
Private Sub Timer1_Timer()
When you run the program, you will see the following runtime interface:
The Runtime Interface
Watch it on YouTube
Copyright ® 2008 Dr.Liew Voon Kiong . All rights reserved |Contact: email@example.com