Vb.net 我如何使图片盒显示随机图像,也不会重复?
我是VisualBasic新手,正在尝试创建一个游戏,在这个游戏中,用户将看到两个图像,并且需要像测验一样选择正确的一个位。但是,我希望它具有挑战性,因此希望在用户再次播放时,图像随机配对 为此,我有大约10幅图像,一次将呈现两幅。如果玩家单击正确的图像,他们可以继续并接收一个点,该点将使用标签记录,两个新图像将替换它们,并重复该过程,但如果它们不正确,则会出现一条消息,说明不正确!然后它们会失去一个标记,两个新的图像将取代它们 我使用一个TableLayoutPanel和两个图片框创建了界面。我已经生成了一些生成随机图片的代码,但它们最终会重复自己,这是我不想要的!但是,如果我把RemoveAt放在代码的末尾,它们不会重复,但是在玩家点击图片6次后会产生一个错误,说索引超出范围,这是在游戏结束之前发生的,所以我也不希望这样 以下是我目前掌握的代码: 第一:Vb.net 我如何使图片盒显示随机图像,也不会重复?,vb.net,image,random,picturebox,repeat,Vb.net,Image,Random,Picturebox,Repeat,我是VisualBasic新手,正在尝试创建一个游戏,在这个游戏中,用户将看到两个图像,并且需要像测验一样选择正确的一个位。但是,我希望它具有挑战性,因此希望在用户再次播放时,图像随机配对 为此,我有大约10幅图像,一次将呈现两幅。如果玩家单击正确的图像,他们可以继续并接收一个点,该点将使用标签记录,两个新图像将替换它们,并重复该过程,但如果它们不正确,则会出现一条消息,说明不正确!然后它们会失去一个标记,两个新的图像将取代它们 我使用一个TableLayoutPanel和两个图片框创建了界面。
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}
及
代码是按顺序编写的。使用Visual Basic 2010 Express。非常感谢您的帮助,如果您需要更多详细信息,请告诉我!我在这里绝望
基本上,我如何阻止图像重复并阻止错误出现。此外,不会发生错误,但如果单击蝴蝶图像,则它不会显示msgbox,那么有什么问题吗?创建您自己的对象继承自图像,但也包括一个指示“已使用”的标志。您也可以轻松地包含其他控制信息。将十个图像放入一个数组中,洗牌,一次选取两个,五次。一个数组或图像列表将起作用。只需使用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
您的索引越界错误可能是由于设置了images.Count旁边的random。计数总是比最高索引多一个。因此,如果随机数等于Count,则得到错误。使用images.Count-1应该可以解决这个问题。我已经完成了数组部分,但是如何将它们随机分配到两个图片框中的一个?非常感谢。不用担心!我明白了。谢谢你的想法-工作很好!
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