How To Make A Slot Machine In Visual Basic 5,9/10 591 reviews

This game includes a graphical slot machine that is created entirely by VB graphics methods such as Circle and Line. The source code and files included in this project are listed in the project files section, please make sure whether the listed source code meet your needs there.

[Lesson 32] << [Contents] >> [Lesson 34]

While Nescio's answer (DoEvents) will work, it will cause your application to use 100% of one CPU. Sleep will make the UI unresponsive. What you need is a combination of the two, and the magic combination that seems to work best is. This is a slot machine that resembles the real slot machines in the casinos. To create the project, you need to insert three image boxes into the form and program them so that they will display a set of three different pictures randomly when the user presses on the spin button. It involves a randomization process. If you have just taken your first course on Visual Basic, I do NOT suggest that you dive right in to 3D game programming. 3D Game programming can be very complicated, and I do not think beginners should start with that. You should start by making some normal Windows Forms applications, until you get used to the language and environment.

Although Visual Basic 2015 is a programming language designed for creating business and other industrial applications, it can be used to create animation. In the preceding lesson, we have actually learned how to create animation using the timer. In fact, the programs we have created in the previous lesson such as the stopwatch and the digital dice are animated programs.

We can create a continuously moving object using the timer. The motion can be from left to right or from top to bottom motion or diagonal.

33.1 Creating Motion

First, insert a picture box into the form. In the picture box properties window, select the image property and click to import an image file from your storage devices such as your hard drive, your pen drive or DVD drive. We have inserted an image of a bunch of grapes.Next, insert a Timer control into the form and set its Interval property to 100, which is equivalent to 0.1 seconds. Finally, add two buttons to the form, name one of them as AnimateBtn and the other one as StopBtn, and change to caption to Animate and Stop respectively.

Basic


We make use of the Left property of the picture box to create the motion. PictureBox.Left means the distance of the PictureBox from the left border of the Form. Now click on the Timer control and type in the following code:

In the code above, Me.Width represents the width of the Form. If the distance of the PictureBox from the left is less than the width of the Form, a value of 10 is added to the distance of the PictureBox from the left border each time the Timer tick, or every 0.1 seconds in this example. When the distance of the PictureBox from the left border is equal to the width of the form, the distance from the left border is set to 0, which move the PictureBox object to the left border and then move left again, thus creates an oscillating motion from left to right. We need to insert a button to stop motion. The code is:

To animate the PictureBox object, we insert a button and enter the following code:

The runtime interface

Figure 33.1


33.2 Creating a Graphical Dice

In preceding lessons, we have learned how to create graphics and draw objects on the form. Now we shall use the previous knowledge to create an animated graphical dice using the timer.

In this program, we need to insert a timer and set its interval to 100, which means the drawings will refresh every 0.1 seconds. Next, insert a picture box which is used as the surface of a dice. Finally, add a button and change its text to Roll. Under the Timer sub procedure, we create the Graphics object and the Pen object following the procedures we have learned in preceding lessons. Next, we use a Do loop and the Select Case structure to cycle through all six surfaces of the dice. To create six random cases, we use the syntax n = Int(6 * Rnd()) + 1. We can stop the loop by introducing a variable t and the loop until condition. The condition we set here is t>1000, you can use any figure you wish.

The code

The runtime interface is as shown in Figure 33.2

Figure 33.2

The Video Demo


33.3 Creating a Slot Machine

In this program, we add three picture boxes, a timer, a button and a label. Set the timer interval to 10, which means the images will refresh every 0.01 second. In the code, we shall introduce four variables m, a, b and c, where m is used to stop the timer and a,b,c are used to generate random images using the syntax Int(1 + Rnd() * 3). To load the images, we use the following syntax:

PictureBox.Image = Image.FromFile(Path of the image file)

How To Make A Slot Machine In Visual Basic

We employ the If…Then structure to control the timer and the Select Case…..End Select structure to generate the random images. The label is used to display the message of the outcomes.

How To Make A Slot Machine In Visual Basic Programming

The Code

The run-time interface is shown in Figure 33.3

Figure 33.3

How To Make A Slot Machine In Visual Basic Language

View the slot machine in action on the following video clip.


How To Make A Slot Machine In Visual Basic C++

[Lesson 32] << [Contents] >> [Lesson 34]