Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Mallard8

New member
Joined
Feb 22, 2018
Messages
284
This is the start of a match two game. Zip attached so you can down load and run it, all code below.
I placed it in the code bank as beginners may find bit’s they can use in their own projects.
Game play click the labels and they roll up to reveal the image underneath, if they match stay open if not close.
If they do match reveal some of the main picture on the right.
Would welcome feedback, good or bad.
Future ideas,
• Add sound?
• A timer so you can see how long it took to complete?
• Top ten times?
• Smaller squares from 100 x 100 to 50 x 50 making a bigger grid?
• Different sets of images?
• Allow user to use images of their own?
And anything else anyone can think of.
View attachment 176581
Code:
Option Strict On
Option Explicit On
Imports System.Threading
Public Class Form1
    Dim pb As PictureBox
    Dim lbl, lblHide As Label
    Dim j, k, vy As Integer
    Private selectedDoor1 As Label = Nothing
    Private selectedDoor2 As Label = Nothing
    Private firstImage As String = Nothing
    Private secondImage As String = Nothing
    Private ReadOnly r As New Random
    Private lastClickedLabel As Label
    Dim numbers As List(Of Integer)
    Dim savedNames(32) As String

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        lastClickedLabel.Height -= vy
        If lastClickedLabel.Height = 0 Then
            Timer1.Stop()
            If selectedDoor2 IsNot Nothing Then WaitTime() 'If selectedDoor2 IsNot Nothing then two doors are open go check if they match
        End If
    End Sub
    Private Sub Clickme(ByVal sender As Object, ByVal e As EventArgs)
        If Timer1.Enabled = True Then Exit Sub 'Can not click if doors are opening
        If selectedDoor2 IsNot Nothing Then Exit Sub 'Can not click while checking for a matching pair
        lastClickedLabel = DirectCast(sender, Label)

        If selectedDoor1 Is Nothing Then
            selectedDoor1 = lastClickedLabel
            'Get the name from the array and place it in variable firstImage
            firstImage = savedNames(CInt(lastClickedLabel.Tag))
        Else
            selectedDoor2 = lastClickedLabel
            secondImage = savedNames(CInt(lastClickedLabel.Tag))
        End If
        Timer1.Start()
    End Sub
    Private Sub HideMainImage()
        'Cover the main picture with Labels
        Dim hiddenX As Integer = PictureBox1.Location.X
        Dim hiddenY As Integer = PictureBox1.Location.Y
        For i = 1 To 32
            lblHide = New Label With {
            .Size = New Drawing.Size(50, 44),
            .Location = New Point(hiddenX, hiddenY),
            .BackColor = Color.Beige,
            .BorderStyle = BorderStyle.FixedSingle,
            .Name = "lblHidden" & i,
            .Tag = i
                  }
            hiddenX += 50
            If hiddenX >= 648 Then 'If labels cover width of main picture
                hiddenY += 44 ' Drop down to next row
                hiddenX = PictureBox1.Location.X
            End If
            Me.Controls.Add(lblHide)
            lblHide.BringToFront()
        Next
    End Sub
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        vy = 10
        j = 10
        k = 10
        For i As Integer = 1 To 16 'Create 16 picture boxes
            pb = New PictureBox With {
                .BorderStyle = BorderStyle.Fixed3D,
                .Size = New Drawing.Size(100, 100),
                .Location = New Point(j, k),
                .Name = "pbxFrame" & i,
                .Tag = i
            }

            lbl = New Label With {
                .BorderStyle = BorderStyle.Fixed3D,
                .BackgroundImage = My.Resources.RedBack,
                .Size = New Drawing.Size(100, 100),
                .Location = New Point(j, k),
                .Name = "lblDoor" & i,
                .Tag = i
            }
            AddHandler lbl.Click, AddressOf Clickme

            Me.Controls.Add(pb) 'Add picture boxes to form
            Me.Controls.Add(lbl) 'Add labels to form

            j += 100
            If j >= 400 Then
                j = 10
                k += 100
            End If
            lbl.BringToFront() 'Place Labels on top of Picture Boxes
        Next

        numbers = Enumerable.Range(1, 32).OrderBy(Function(x) r.NextDouble).ToList()
        HideMainImage()
        AssignImagesToSquares()
    End Sub
    Private Sub btnRed_Click(sender As Object, e As EventArgs) Handles btnRed.Click
        For i = 1 To 16
            DirectCast(Controls("lblDoor" & i), Label).Image = My.Resources.RedBack
        Next
    End Sub
    Private Sub btnBlue_Click(sender As Object, e As EventArgs) Handles btnBlue.Click
        For i = 1 To 16
            DirectCast(Controls("lblDoor" & i), Label).Image = My.Resources.BlueBack
        Next
    End Sub
    Private Sub BtnStartGame_Click(sender As Object, e As EventArgs) Handles btnStartGame.Click
        Application.Restart()
    End Sub
    Private Sub AssignImagesToSquares()
        Dim animalName As String
        Dim indices() As Integer = Enumerable.Range(0, 16).OrderBy(Function(x) r.NextDouble).ToArray
        Dim i As Integer = 1
        For Each x As Integer In indices
            Dim b As Bitmap = DirectCast(ImageList1.Images(x), Bitmap) 'Get number in Imagelist
            DirectCast(Me.Controls("pbxFrame" & i), PictureBox).Image = b 'Place images in PictureBoxes
            animalName = ImageList1.Images.Keys(x) 'Get image name from ImageList
            savedNames(i) = animalName 'Place image names in an array
            i += 1
        Next
    End Sub
    Private Sub WaitTime()
        If firstImage = secondImage Then
            selectedDoor1 = Nothing
            selectedDoor2 = Nothing
            firstImage = Nothing
            secondImage = Nothing
            For i = 0 To 3
                DirectCast(Me.Controls("lblHidden" & numbers(0)), Label).Visible = False
                numbers.RemoveAt(0)
            Next
            Exit Sub 'Found a match Return
        End If
        Thread.Sleep(750)
        selectedDoor1.Height = 100 'No match shut doors
        selectedDoor2.Height = 100
        selectedDoor1 = Nothing 'Reset selectedDoor1 and Two
        selectedDoor2 = Nothing
        firstImage = Nothing
        secondImage = Nothing
    End Sub
End Class
View attachment 176583
 
Top