vb.net 如何让图片框显示不重复的随机图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17511713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:11:19  来源:igfitidea点击:

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

vb.netimagerandompictureboxrepeat

提问by KSR5

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.

我是 Visual Basic 的新手,正在尝试创建一个游戏,在该游戏中,用户将看到两个图像,并且需要选择正确的一个(有点像测验)。但是,我希望它具有挑战性,因此希望在用户再次播放时随机配对图像。

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.

为此,我有大约 10 张图片,每次展示两张。如果玩家点击正确的图像,他们可以继续并获得一个点,该点将使用标签记录,两个新图像将替换它们并重复该过程,但如果它们不正确,则会出现一条消息,说“不正确! ” 然后他们将失去一个标记,两个新图像将取代它们。

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!

我已经使用 TableLayoutPanel 和两个图片框创建了界面。我已经生成了一些生成随机图片的代码,但它们最终会重复自己,这是我不想要的!但是,如果我将 RemoveAt 放在代码的末尾,它们就不会重复,但是在玩家点击图片 6 次后会产生一个错误,说“索引超出范围”,这发生在游戏完成之前,所以不要也不想要!

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!!

代码是按顺序写的。使用 Visual Basic 2010 Express。非常感谢任何帮助,如果您需要更多详细信息,请告诉我!我在这里绝望了!!

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?

基本上,我如何阻止图像重复并阻止错误出现。此外,没有发生错误,但是如果单击蝴蝶图像,则它不会显示 msgbox,那么有什么问题呢?

采纳答案by Tony Hopkinson

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

将您的十张图像放在一个数组中,将它们打乱,一次选择两张,五次。

回答by peterG

Create your own object inheriting from Image, but also including a flag to indicate 'used'. You can easily include other control information too.

创建您自己的从 Image 继承的对象,但还包括一个标志来指示“已使用”。您也可以轻松包含其他控制信息。

回答by tinstaafl

An array or list of images will work. Just use the tag property to indicate used. Perhaps something like this:

图像数组或列表将起作用。只需使用 tag 属性来指示已使用。也许是这样的:

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-1should fix that.

您的索引越界错误可能来自设置 random.Next 到 images.Count 的上限。计数总是比最高索引多一。所以如果随机数等于 Count 你会得到错误。使用images.Count-1应该可以解决这个问题。