Reputation: 81
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?
Upvotes: 1
Views: 9060
Reputation: 6948
An array or list of images will work. Just use the tag property to indicate used. Perhaps something like this:
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}
Private Sub AssignImagesToSquares()
For Each Control In TableLayoutPanel1.Controls
Dim imageLabel = TryCast(Control, PictureBox)
If imageLabel IsNot Nothing Then
Dim Done as Boolean = False
'Loop until it finds an image that hasn't been used. Then set Done
'to true to exit the while loop
While Not Done
Dim randomNumber = random.Next(images.Count-1)
If images(randomNumber).Tag.ToString <> "Used" Then
imageLabel.Image = images(randomNumber)
images(randomNumber).Tag = "Used"
Done = True
End If
End While
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
Your index out of bounds error might have been coming from setting the upper limit of random.Next to images.Count. Count is always one more than the highest index. so if the random number equals Count you get the error. Using images.Count-1
should fix that.
Upvotes: 1
Reputation: 20320
Put your ten images in an array, shuffle them, pick two at a time, five times.
Upvotes: 1
Reputation: 1641
Create your own object inheriting from Image, but also including a flag to indicate 'used'. You can easily include other control information too.
Upvotes: 1