Visual Basic Memory Game
Build a classic matching game with Visual Basic 6. This project is ideal for learning arrays of controls, timers, event handling, and game-state management.
Introduction to the Memory Game
The Memory Game is a classic matching game in which players uncover hidden symbols and try to find identical pairs. It is simple to understand, yet very useful for teaching important programming techniques in Visual Basic.
How It Works
Players click cards to reveal hidden symbols. If two consecutive cards match, the pair stays revealed. If they do not match, the cards are hidden again.
Educational Value
This project teaches important programming concepts including:
- Arrays of controls
- Event handling
- Conditional logic
- Timer usage
- Game-state management
Game Interface
The game interface consists of a grid of tiles that hide images or symbols. Players click tiles to reveal what is underneath and try to find all matching pairs.
Interactive Demo
Try to find all matching pairs in this simplified preview.
Video Demonstration
Watch how the memory game works in this video demonstration:
Note: The video shows the gameplay flow and the winning sequence of the memory game created in Visual Basic 6.
Implementation Details
Control Setup
Create an array of 12 Image controls to hold the pictures and 12 PictureBox controls to cover them.
Insert 6 pairs of images, and assign the same Tag value to both images in each pair.
Tagging System
Matching pairs are identified by comparing their Tag values rather than comparing the image content directly.
Game Logic
When a PictureBox is clicked, it becomes invisible and reveals the underlying image. The program then checks whether the two revealed images match.
Matching Process
If the two revealed images have the same tag, they remain uncovered. If not, the PictureBoxes are restored after a short delay.
VB6 Code Sample
The core game logic is implemented in these procedures. The check() subroutine verifies matches,
while the event handlers manage user interactions.
' Check whether the revealed pictures match
Sub check()
For i = 0 To 11
If Picture1(i).Visible = False Then
For j = 0 To 11
If Picture1(j).Visible = False Then
' Check if we have a matching pair
If i <> j And Image1(i).Tag = Image1(j).Tag Then
' Hide matched images and their covers
Image1(j).Visible = False
Image1(i).Visible = False
Picture1(j).Visible = False
Picture1(i).Visible = False
End If
' Handle non-matching pair
If i <> j And Image1(i).Tag <> Image1(j).Tag And Image1(i).Visible = True And Image1(j).Visible = True Then
' Restore the covers for non-matches
Picture1(j).Visible = True
Picture1(i).Visible = True
End If
End If
Next j
End If
Next i
End Sub
' Handle picture box click event
Private Sub picture1_Click(Index As Integer)
' Hide the clicked picture box to reveal image
Picture1(Index).Visible = False
' Enable timer to check for matches
Timer1.Enabled = True
End Sub
' Timer event to check for matches
Private Sub Timer1_Timer()
' Call the matching check routine
check
End Sub
Tip: The timer introduces a short delay so that players can see both revealed images before they are either removed as a match or covered again if they do not match.
Equivalent VB.NET Code
The following VB.NET example shows how the same memory game idea can be implemented in Windows Forms. This version uses buttons or picture boxes as cards, tracks the first and second selections, checks for matches, and uses a Timer to flip unmatched cards back over after a short delay.
Public Class Form1
Private cardValues() As String = {"π", "π", "π", "π", "π", "π", "π", "π"}
Private buttons() As Button
Private firstButton As Button = Nothing
Private secondButton As Button = Nothing
Private lockBoard As Boolean = False
Private pairsFound As Integer = 0
Private moves As Integer = 0
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
buttons = {btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7}
ShuffleCards()
ResetGame()
End Sub
Private Sub ShuffleCards()
Dim rnd As New Random()
For i As Integer = cardValues.Length - 1 To 1 Step -1
Dim j As Integer = rnd.Next(i + 1)
Dim temp As String = cardValues(i)
cardValues(i) = cardValues(j)
cardValues(j) = temp
Next
End Sub
Private Sub Card_Click(sender As Object, e As EventArgs) _
Handles btn0.Click, btn1.Click, btn2.Click, btn3.Click,
btn4.Click, btn5.Click, btn6.Click, btn7.Click
If lockBoard Then Exit Sub
Dim clickedButton As Button = CType(sender, Button)
Dim index As Integer = Convert.ToInt32(clickedButton.Tag)
If clickedButton.Text <> "?" Then Exit Sub
clickedButton.Text = cardValues(index)
If firstButton Is Nothing Then
firstButton = clickedButton
lblStatus.Text = "Select another card."
Exit Sub
End If
secondButton = clickedButton
moves += 1
lblMoves.Text = moves.ToString()
If firstButton.Text = secondButton.Text Then
firstButton.Enabled = False
secondButton.Enabled = False
firstButton.BackColor = Color.LightGreen
secondButton.BackColor = Color.LightGreen
pairsFound += 1
lblPairs.Text = pairsFound.ToString()
lblStatus.Text = "β
Match found!"
firstButton = Nothing
secondButton = Nothing
If pairsFound = 4 Then
lblStatus.Text = "π Congratulations! You found all pairs."
MessageBox.Show("You found all pairs!", "Memory Game")
End If
Else
lockBoard = True
lblStatus.Text = "β Not a match."
Timer1.Interval = 800
Timer1.Start()
End If
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Timer1.Stop()
If firstButton IsNot Nothing Then firstButton.Text = "?"
If secondButton IsNot Nothing Then secondButton.Text = "?"
firstButton = Nothing
secondButton = Nothing
lockBoard = False
lblStatus.Text = "Try again."
End Sub
Private Sub btnReset_Click(sender As Object, e As EventArgs) Handles btnReset.Click
ShuffleCards()
ResetGame()
End Sub
Private Sub ResetGame()
For Each btn As Button In buttons
btn.Text = "?"
btn.Enabled = True
btn.BackColor = SystemColors.Control
Next
firstButton = Nothing
secondButton = Nothing
lockBoard = False
pairsFound = 0
moves = 0
lblPairs.Text = "0"
lblMoves.Text = "0"
lblStatus.Text = "Click a card to begin."
End Sub
End Class
In this VB.NET version, each card is represented by a button. The buttonβs
Tag property stores the card index, and the button text is used to show
either a hidden card ("?") or the revealed symbol. A Timer is used to
pause briefly before unmatched cards are hidden again.
Suggested VB.NET controls:
8 Buttons: btn0 to btn7
1 Timer: Timer1
1 Reset Button: btnReset
3 Labels: lblStatus, lblPairs, lblMoves
Learning Resources
Extend your knowledge with these related topics:
VB6 Game Programming
Learn the fundamentals of game development in Visual Basic 6, including graphics handling and event-driven programming.
Control Arrays
Master control arrays in VB6 to efficiently manage multiple UI elements such as game tiles.
Timer Control
Explore advanced uses of the Timer control for animations, delayed actions, and game mechanics.