Simple Slot Machine with Visual Basic
Learn how to create a simple slot machine in Visual Basic 6 using random shape generation, timer-based animation, and interactive gameplay. This project is beginner-friendly and demonstrates core game programming ideas in a fun way.
Introduction
Create your own slot machine game using Visual Basic 6 with this practical tutorial. A slot machine is a simple game of chance, making it a good beginner project for learning random number generation, animation, and event-driven programming.
Key Features
- Random shape generation using the VB6
Rndfunction - Animated slot-machine effect using a Timer control
- Customizable shape colors and properties
- A simple implementation that is ideal for beginners
- A visual introduction to basic game programming concepts
How It Works
In this project, we create an array of nine shapes. Visual Basic automatically labels them as
Shape1(0), Shape1(1), up to Shape1(8). These shapes are arranged in
three rows to simulate a slot machine interface.
The program displays three types of shapes randomly: rectangles, squares, and ovals.
Change shapes at runtime using the Shape property:
Shape1(0).Shape = 0→ RectangleShape1(0).Shape = 1→ SquareShape1(0).Shape = 2→ Oval
Modify shape colors using the FillColor property:
Shape1(0).FillColor = vbRed→ RedShape1(0).FillColor = vbGreen→ GreenShape1(0).FillColor = vbBlue→ Blue
Implementation Details
Randomness is achieved using the Rnd function. A Timer control is used to create the animated slot-machine
effect. The timer interval is set to 10 milliseconds so the shapes change quickly enough to appear animated. A variable
such as x is used to control how long the animation continues before the timer stops.
Note: This program focuses on showing how shapes can appear randomly. More advanced slot-machine features, such as betting systems and scoring, can be added later as an extension.
Visual Basic 6 Code
' Start the timer when the button is clicked Private Sub Command1_Click() Timer1.Enabled = True x = 0 End Sub ' Timer event to handle animation Private Sub Timer1_Timer() x = x + 10 Dim a, i As Integer For i = 0 To 8 ' Generate random integers 0, 1, and 2 a = Int(Rnd * 3) Shape1(i).Shape = a If a = 0 Then Shape1(i).FillColor = vbRed ElseIf a = 1 Then Shape1(i).FillColor = vbGreen Else Shape1(i).FillColor = vbBlue End If Next i ' Stop the timer after 500 milliseconds If x > 500 Then Timer1.Enabled = False End If End Sub
Equivalent VB.NET Code
The following VB.NET example shows a more complete slot-machine implementation using Windows Forms. It includes random symbol generation, a timer-based animation effect, score tracking, spin counting, and simple win detection.
Public Class Form1
Private rand As New Random()
Private spinTicks As Integer = 0
Private totalSpins As Integer = 0
Private totalWins As Integer = 0
' Use 9 labels to simulate the 3x3 slot display
Private slotLabels() As Label
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
slotLabels = {
lbl0, lbl1, lbl2,
lbl3, lbl4, lbl5,
lbl6, lbl7, lbl8
}
Timer1.Interval = 80
Timer1.Enabled = False
ResetBoard()
UpdateScore()
lblResult.Text = "Press SPIN to play."
End Sub
Private Sub btnSpin_Click(sender As Object, e As EventArgs) Handles btnSpin.Click
spinTicks = 0
Timer1.Enabled = True
btnSpin.Enabled = False
lblResult.Text = "Spinning..."
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
spinTicks += 1
For i As Integer = 0 To 8
SetRandomSymbol(slotLabels(i))
Next
' Stop after a short animation cycle
If spinTicks >= 10 Then
Timer1.Enabled = False
btnSpin.Enabled = True
totalSpins += 1
CheckWin()
UpdateScore()
End If
End Sub
Private Sub btnReset_Click(sender As Object, e As EventArgs) Handles btnReset.Click
ResetBoard()
totalSpins = 0
totalWins = 0
UpdateScore()
lblResult.Text = "Press SPIN to play."
End Sub
Private Sub SetRandomSymbol(lbl As Label)
Dim a As Integer = rand.Next(0, 3)
Select Case a
Case 0
lbl.Text = "■"
lbl.ForeColor = Color.Red
Case 1
lbl.Text = "■"
lbl.ForeColor = Color.Green
Case 2
lbl.Text = "●"
lbl.ForeColor = Color.Blue
End Select
lbl.Font = New Font("Arial", 24, FontStyle.Bold)
lbl.TextAlign = ContentAlignment.MiddleCenter
End Sub
Private Function IsMatch(a As Label, b As Label, c As Label) As Boolean
Return a.Text = b.Text AndAlso
b.Text = c.Text AndAlso
a.ForeColor = b.ForeColor AndAlso
b.ForeColor = c.ForeColor
End Function
Private Sub CheckWin()
Dim hasWin As Boolean = False
' Check rows
If IsMatch(lbl0, lbl1, lbl2) Then hasWin = True
If IsMatch(lbl3, lbl4, lbl5) Then hasWin = True
If IsMatch(lbl6, lbl7, lbl8) Then hasWin = True
If hasWin Then
totalWins += 1
lblResult.Text = "Congratulations! You win!"
picWinner.Visible = True
MessageBox.Show("You win!", "Slot Machine")
Else
lblResult.Text = "No matching row this time. Try again!"
picWinner.Visible = False
End If
End Sub
Private Sub UpdateScore()
lblSpinCount.Text = totalSpins.ToString()
lblWinCount.Text = totalWins.ToString()
If totalSpins > 0 Then
Dim rate As Double = (totalWins / totalSpins) * 100
lblWinRate.Text = Math.Round(rate, 0).ToString() & "%"
Else
lblWinRate.Text = "0%"
End If
End Sub
Private Sub ResetBoard()
lbl0.Text = "■" : lbl0.ForeColor = Color.Red
lbl1.Text = "■" : lbl1.ForeColor = Color.Green
lbl2.Text = "●" : lbl2.ForeColor = Color.Blue
lbl3.Text = "■" : lbl3.ForeColor = Color.Green
lbl4.Text = "●" : lbl4.ForeColor = Color.Blue
lbl5.Text = "■" : lbl5.ForeColor = Color.Red
lbl6.Text = "●" : lbl6.ForeColor = Color.Blue
lbl7.Text = "■" : lbl7.ForeColor = Color.Red
lbl8.Text = "■" : lbl8.ForeColor = Color.Green
For Each lbl As Label In slotLabels
lbl.Font = New Font("Arial", 24, FontStyle.Bold)
lbl.TextAlign = ContentAlignment.MiddleCenter
Next
picWinner.Visible = False
End Sub
End Class
This VB.NET version expands the original idea by adding a timer-driven spin animation, a simple winning rule based on matching rows, and score indicators for total spins, wins, and win rate. It is a more complete example for learners who want to move from simple random output to a more game-like program structure.
Suggested controls for the VB.NET form:
9 Labels: lbl0 to lbl8
1 Timer: Timer1
2 Buttons: btnSpin, btnReset
1 PictureBox: picWinner
4 Labels for status: lblResult, lblSpinCount, lblWinCount, lblWinRate
Interactive Preview
VB.NET Graphical Slot Machine Simulation
This version uses graphical symbols instead of numbers, making it feel closer to a real slot machine while still matching the Visual Basic theme of shapes and colors.