Search code examples
vb.netimagerandompictureboxrepeat

How do I make pictureboxes show random images that also don't repeat?


I am new to Visual Basic and am trying to create a game where the user will be presented with two images and will need to select the correct one (bit like a quiz). However, I want it to be challenging, so therefore want the images to be randomly paired whenever the user plays it again.

For this, I have around 10 images, which will be presented two at a time. If the player clicks on the correct image they can proceed and receive a point which will be recorded using a label and two new images will replace them and the process is repeated, but if they're incorrect then a message will appear saying "Incorrect!" and they will then lose a mark and two new images will replace them.

I have created the interface, using a TableLayoutPanel and two picture boxes. I have produced some code which generates random pictures, but they end up repeating themselves, which I don't want! But, if I put RemoveAt at the end of the code they don't repeat, but will then produce an error saying "index out of range" after the player clicks the pictures 6 times, which happens before the game is finished, so don't want that either!

Here is the code I have so far:

First:

Public Sub New()
    ' This call is required by Windows Form Designer
    InitializeComponent()
    ' Add any initialization after the InitializeComponent() call
    AssignImagesToSquares()
End Sub

Private random As New Random

Private images =
    New List(Of Image) From {My.Resources.Aeroplane, My.Resources.Bicycle, My.Resources.Beginner_button, My.Resources.Bird, My.Resources.Butterfly,
                             My.Resources.Cartoon_Background_Wallpaper_719574, My.Resources.cartoon_farm, My.Resources.Clock, My.Resources.Egg_Timer,
                             My.Resources.Moderate_background, My.Resources.Tree, My.Resources.Umbrella, My.Resources.Woman}

and

Private Sub AssignImagesToSquares()

    For Each Control In TableLayoutPanel1.Controls
        Dim imageLabel = TryCast(Control, PictureBox)
        If imageLabel IsNot Nothing Then
            Dim randomNumber = random.Next(images.Count)
            imageLabel.Image = images(randomNumber)
            images.RemoveAt(randomNumber)
        End If
    Next
End Sub

 Private Sub picturebox_clicked(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox2.Click,
    PictureBox1.Click

    Dim clickedLabel = TryCast(sender, PictureBox)

    If clickedLabel.Image Is My.Resources.Butterfly Then
        MsgBox("This is incorrect")
        AssignImagesToSquares()
    Else
        AssignImagesToSquares()
    End If
End Sub

The code is written in the order. Using Visual Basic 2010 Express. Any help is greatly appreciated and if you need more detail just let me know! I am desperate here!!

Basically, how do I stop the images from repeating and stop the error from appearing. Also, no errors occur, but if the butterfly images is clicked on then it doesn't show the msgbox, so what's wrong there?


Solution

  • Put your ten images in an array, shuffle them, pick two at a time, five times.