Creating a Slot Machine Program in VB.NET

This is my another tutorial in VB.NET. In this tutorial 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).

Getting Started

First thing you need to do is Download and Install Microsoft Visual Studio.

Then, download or save the images below in your vb.net program.

This is used as your default image in your three picture boxes.

question

These are the three images for our slot machines. The apple, grapes, and strawberry image.

strawberry
grapes
apple

To load the images, we use the following syntax:

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

Your design of the program must be like this image below.

designer

Setting Integer Variables

We need to declare the variables of the integers for Image values and the timer interval. Below code is a declared variables for, m = Timer Interval and a,b, and c are the images values variables.

  1. Public Class Form1
  2.     Dim m, a, b, c As Integer
  3. End Class

Creating a Click Event

Below code is a script to spin the slot machine.

  1.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  2.         'Removing the text value of the label
  3.         lblMsg.Text = ""
  4.         'Starting the timer
  5.         Timer1.Enabled = True
  6.         'Disable the spin button
  7.         Button1.Enabled = False
  8.     End Sub

Creating a Random Value for the Picturebox

Below code is the script to randomize the picture boxes images every the 0.01 seconds. Each picture box will stop spinning every 3 seconds which means the slot machine will be done randomizing after 9 seconds and display the text result in the label field provided.

  1. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
  2.         m = m + 10
  3.         If m < 3000 Then
  4.             If m < 1000 Then
  5.  
  6.                 a = Int(1 + Rnd() * 3)
  7.  
  8.                 b = Int(1 + Rnd() * 3)
  9.  
  10.                 c = Int(1 + Rnd() * 3)
  11.  
  12.                 Select Case a
  13.                     Case 1
  14.                         PictureBox1.Image = Image.FromFile("C:\Users\Personal-01\Desktop\sample images\slot machine\apple.jpg")
  15.                     Case 2
  16.                         PictureBox1.Image = Image.FromFile("C:\Users\Personal-01\Desktop\sample images\slot machine\grapes.jpg")
  17.                     Case 3
  18.                         PictureBox1.Image = Image.FromFile("C:\Users\Personal-01\Desktop\sample images\slot machine\strawberry.jpg")
  19.  
  20.                 End Select
  21.             End If
  22.             If m < 2000 Then
  23.  
  24.  
  25.                 b = Int(1 + Rnd() * 3)
  26.  
  27.                 c = Int(1 + Rnd() * 3)
  28.  
  29.                 Select Case b
  30.                     Case 1
  31.                         PictureBox2.Image = Image.FromFile("C:\Users\Personal-01\Desktop\sample images\slot machine\apple.jpg")
  32.                     Case 2
  33.                         PictureBox2.Image = Image.FromFile("C:\Users\Personal-01\Desktop\sample images\slot machine\grapes.jpg")
  34.                     Case 3
  35.                         PictureBox2.Image = Image.FromFile("C:\Users\Personal-01\Desktop\sample images\slot machine\strawberry.jpg")
  36.  
  37.                 End Select
  38.             End If
  39.             If m < 3000 Then
  40.  
  41.                 c = Int(1 + Rnd() * 3)
  42.  
  43.                 Select Case c
  44.                     Case 1
  45.                         PictureBox3.Image = Image.FromFile("C:\Users\Personal-01\Desktop\sample images\slot machine\apple.jpg")
  46.                     Case 2
  47.                         PictureBox3.Image = Image.FromFile("C:\Users\Personal-01\Desktop\sample images\slot machine\grapes.jpg")
  48.                     Case 3
  49.                         PictureBox3.Image = Image.FromFile("C:\Users\Personal-01\Desktop\sample images\slot machine\strawberry.jpg")
  50.  
  51.                 End Select
  52.             End If
  53.  
  54.         Else
  55.             Timer1.Enabled = False
  56.             Button1.Enabled = True
  57.             m = 0
  58.             If a = b And a = c And b = c Then
  59.                 lblMsg.Text = "Jackpot! You won P1,000,000″"
  60.                 lblMsg.ForeColor = Color.Green
  61.                 'MessageBox.Show($"{a} {b} {c} Equal")
  62.  
  63.             Else
  64.                 lblMsg.Text = "No luck, try again"
  65.                 lblMsg.ForeColor = Color.Red
  66.                 'MessageBox.Show($"{a} {b} {c} Unequal")
  67.             End If
  68.         End If
  69.     End Sub

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.

click

Result

First Run

That's it you have now a Simple Slot Machine using Vb.NET. Take note that you have to configure the images location according to path where you have saved the images. "Image.FromFile("C:\Users\Personal-01\Desktop\sample images\slot machine\strawberry.jpg")"

Here's the full code:

  1.     Public Class Form1
  2.     Dim m, a, b, c As Integer
  3.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  4.         lblMsg.Text = ""
  5.         lblMsg.ForeColor = Color.Blue
  6.         Timer1.Enabled = True
  7.         Button1.Enabled = False
  8.     End Sub
  9.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
  10.         m = m + 10
  11.         If m < 3000 Then
  12.             If m < 1000 Then
  13.  
  14.                 a = Int(1 + Rnd() * 3)
  15.  
  16.                 b = Int(1 + Rnd() * 3)
  17.  
  18.                 c = Int(1 + Rnd() * 3)
  19.  
  20.                 Select Case a
  21.                     Case 1
  22.                         PictureBox1.Image = Image.FromFile("C:\Users\Personal-01\Desktop\sample images\slot machine\apple.jpg")
  23.                     Case 2
  24.                         PictureBox1.Image = Image.FromFile("C:\Users\Personal-01\Desktop\sample images\slot machine\grapes.jpg")
  25.                     Case 3
  26.                         PictureBox1.Image = Image.FromFile("C:\Users\Personal-01\Desktop\sample images\slot machine\strawberry.jpg")
  27.  
  28.                 End Select
  29.             End If
  30.             If m < 2000 Then
  31.  
  32.  
  33.                 b = Int(1 + Rnd() * 3)
  34.  
  35.                 c = Int(1 + Rnd() * 3)
  36.  
  37.                 Select Case b
  38.                     Case 1
  39.                         PictureBox2.Image = Image.FromFile("C:\Users\Personal-01\Desktop\sample images\slot machine\apple.jpg")
  40.                     Case 2
  41.                         PictureBox2.Image = Image.FromFile("C:\Users\Personal-01\Desktop\sample images\slot machine\grapes.jpg")
  42.                     Case 3
  43.                         PictureBox2.Image = Image.FromFile("C:\Users\Personal-01\Desktop\sample images\slot machine\strawberry.jpg")
  44.  
  45.                 End Select
  46.             End If
  47.             If m < 3000 Then
  48.  
  49.                 c = Int(1 + Rnd() * 3)
  50.  
  51.                 Select Case c
  52.                     Case 1
  53.                         PictureBox3.Image = Image.FromFile("C:\Users\Personal-01\Desktop\sample images\slot machine\apple.jpg")
  54.                     Case 2
  55.                         PictureBox3.Image = Image.FromFile("C:\Users\Personal-01\Desktop\sample images\slot machine\grapes.jpg")
  56.                     Case 3
  57.                         PictureBox3.Image = Image.FromFile("C:\Users\Personal-01\Desktop\sample images\slot machine\strawberry.jpg")
  58.  
  59.                 End Select
  60.             End If
  61.  
  62.         Else
  63.             Timer1.Enabled = False
  64.             Button1.Enabled = True
  65.             m = 0
  66.             If a = b And a = c And b = c Then
  67.                 lblMsg.Text = "Jackpot! You won P1,000,000″"
  68.                 lblMsg.ForeColor = Color.Green
  69.                 'MessageBox.Show($"{a} {b} {c} Equal")
  70.  
  71.             Else
  72.                 lblMsg.Text = "No luck, try again"
  73.                 lblMsg.ForeColor = Color.Red
  74.                 'MessageBox.Show($"{a} {b} {c} Unequal")
  75.             End If
  76.         End If
  77.     End Sub
  78. End Class

Demo

Download the source code below and try it! :)

For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below.

Best Regards,

Engr. Lyndon R. Bermoy
IT Instructor/System Developer/Android Developer
Mobile: 09079373999
Telephone: 826-9296
E-mail:[email protected]

Visit and like my page on Facebook at: Bermz ISware Solutions

Subscribe at my YouTube Channel at: SerBermz

Comments

thank you for the code

Source code is missing, the ZIP file is lacking the code

Add new comment