[英]Randomly Select 1 image out of 3
您好,我正在尝试制作纸牌战争游戏,我想做的是从总共3张图像中选择1张图像。 图像存储在资源文件夹中的所有数字都在同一个文件夹中,例如所有2都是3,所以我可以在数字= 2的情况下随机挑选一张卡片,这样您就不会总是得到2张红心
我在想这个,但是代码会很长。
Dim RC As Integer
if randomNumber = 2 then
RC = ran.Next(1, 4)
If RC = 1 Then
PictureBox1.Image = My.Resources.schoppen2
Else If RC = 2 Then
PictureBox1.Image = My.Resources.harten2
Else If RC = 3 Then
PictureBox1.Image = My.Resources.ruiten2
Else
PictureBox1.Image = My.Resources.klaveren2
End If
End If
If randomNumber = 3 Then
RC = ran.Next(1, 4)
If RC = 1 Then
PictureBox1.Image = My.Resources.schoppen3
ElseIf RC = 2 Then
PictureBox1.Image = My.Resources.harten3
ElseIf RC = 3 Then
PictureBox1.Image = My.Resources.ruiten3
Else
PictureBox1.Image = My.Resources.klaveren3
End If
End If
If randomNumber = 4 Then
RC = ran.Next(1, 4)
If RC = 1 Then
PictureBox1.Image = My.Resources.schoppen4
ElseIf RC = 2 Then
PictureBox1.Image = My.Resources.harten4
ElseIf RC = 3 Then
PictureBox1.Image = My.Resources.ruiten4
Else
PictureBox1.Image = My.Resources.klaveren4
End If
End If
If randomNumber = 5 Then
RC = ran.Next(1, 4)
If RC = 1 Then
PictureBox1.Image = My.Resources.schoppen5
ElseIf RC = 2 Then
PictureBox1.Image = My.Resources.harten5
ElseIf RC = 3 Then
PictureBox1.Image = My.Resources.ruiten5
Else
PictureBox1.Image = My.Resources.klaveren5
End If
End If
If randomNumber = 6 Then
RC = ran.Next(1, 4)
If RC = 1 Then
PictureBox1.Image = My.Resources.schoppen6
ElseIf RC = 2 Then
PictureBox1.Image = My.Resources.harten6
ElseIf RC = 3 Then
PictureBox1.Image = My.Resources.ruiten6
Else
PictureBox1.Image = My.Resources.klaveren6
End If
End If
If randomNumber = 7 Then
RC = ran.Next(1, 4)
If RC = 1 Then
PictureBox1.Image = My.Resources.schoppen7
ElseIf RC = 2 Then
PictureBox1.Image = My.Resources.harten7
ElseIf RC = 3 Then
PictureBox1.Image = My.Resources.ruiten7
Else
PictureBox1.Image = My.Resources.klaveren7
End If
End If
If randomNumber = 8 Then
RC = ran.Next(1, 4)
If RC = 1 Then
PictureBox1.Image = My.Resources.schoppen8
ElseIf RC = 2 Then
PictureBox1.Image = My.Resources.harten8
ElseIf RC = 3 Then
PictureBox1.Image = My.Resources.ruiten8
Else
PictureBox1.Image = My.Resources.klaveren8
End If
End If
If randomNumber = 9 Then
RC = ran.Next(1, 4)
If RC = 1 Then
PictureBox1.Image = My.Resources.schoppen9
ElseIf RC = 2 Then
PictureBox1.Image = My.Resources.harten9
ElseIf RC = 3 Then
PictureBox1.Image = My.Resources.ruiten9
Else
PictureBox1.Image = My.Resources.klaveren9
End If
End If
If randomNumber = 10 Then
RC = ran.Next(1, 4)
If RC = 1 Then
PictureBox1.Image = My.Resources.schoppen10
ElseIf RC = 2 Then
PictureBox1.Image = My.Resources.harten10
ElseIf RC = 3 Then
PictureBox1.Image = My.Resources.ruiten10
Else
PictureBox1.Image = My.Resources.klaveren10
End If
End If
If randomNumber = 11 Then
RC = ran.Next(1, 4)
If RC = 1 Then
PictureBox1.Image = My.Resources.schoppen11
ElseIf RC = 2 Then
PictureBox1.Image = My.Resources.harten11
ElseIf RC = 3 Then
PictureBox1.Image = My.Resources.ruiten11
Else
PictureBox1.Image = My.Resources.klaveren11
End If
End If
If randomNumber = 12 Then
RC = ran.Next(1, 4)
If RC = 1 Then
PictureBox1.Image = My.Resources.schoppen12
ElseIf RC = 2 Then
PictureBox1.Image = My.Resources.harten12
ElseIf RC = 3 Then
PictureBox1.Image = My.Resources.ruiten12
Else
PictureBox1.Image = My.Resources.klaveren12
End If
End If
If randomNumber = 13 Then
RC = ran.Next(1, 4)
If RC = 1 Then
PictureBox1.Image = My.Resources.schoppen13
ElseIf RC = 2 Then
PictureBox1.Image = My.Resources.harten13
ElseIf RC = 3 Then
PictureBox1.Image = My.Resources.ruiten13
Else
PictureBox1.Image = My.Resources.klaveren13
End If
End If
但是当我不得不一直重复这段代码直到13时,那将是很长的时间。 我在互联网上看到您可以抓取随机文件,我想也许您也可以这样做
提前致谢!
您的代码确实可以正常工作。 但是它很长,所以您可以做的就是将您的代码放入这样的Private子目录中。
Private sub ChooseCard()
'Paste your code here
end sub
因此,您只需将其添加到代码中即可执行脚本,而不必将代码粘贴到您想要的任何位置。
ChooseCard()
这将在Private子ChooseCard中运行您的脚本
您应该为卡制作一个存储查找表。 我将给您一个示例(示例中的名称为德语,但应该可以理解)。 作为查找,我将使用一个Dictionary
对象,该对象本身包含词典的集合。 这样就可以使用两个不同的键进行查找。 一种用于卡片颜色,另一种用于卡片价值。 我使用Enums来引用特定的卡值,以使此后的代码更具可读性。 使用Enum类型的变量时,您可以分配整数值或条目名称。
一,完整的代码:
Public Class Form1
'Define the enums, example is for a 52 card game (Poker)
Private Enum CardColors
Herz = 1
Karo = 2
Pik = 3
Kreuz = 4
End Enum
Private Enum CardValues
Ass = 1
Zwei = 2
Drei = 3
Vier = 4
Fuenf = 5
Sechs = 6
Sieben = 7
Acht = 8
Neun = 9
Zehn = 10
Bube = 11
Dame = 12
Koenig = 13
End Enum
Private Sub MakeCards()
'Test function for creating a set of test cards, not escpecially relevant to this example
IO.Directory.CreateDirectory("C:\test\cards")
For i = 1 To 4
For y = 1 To 13
Dim bmp As New Bitmap(100, 100)
Using g As Graphics = Graphics.FromImage(bmp)
g.DrawString(CType(i, CardColors).ToString & " " & CType(y, CardValues).ToString, New Font("Arial", 8), Brushes.Black, New Point(10, 10))
End Using
bmp.Save("C:\test\cards\" & CType(i, CardColors).ToString & "_" & y.ToString & ".bmp")
bmp.Dispose()
Next
Next
End Sub
'This dictionary holds the cards
Private Cards As Dictionary(Of CardColors, Dictionary(Of CardValues, Bitmap))
Private Sub InitializeCards()
'The first dictionary contains 4 entries, assigned to the four colors
'The nested dictionaries hold the card values for each color
'We use the ResourceManager.GetObject() function to get the images from the resources
'You of course have to name them with a continuing index for this to work
'In this example: Herz_1, Herz_2, ..., Herz_13, Karo_1, Karo_2, ...
Cards = New Dictionary(Of CardColors, Dictionary(Of CardValues, Bitmap))
For CardColor = 1 To 4
Dim ThisCards As New Dictionary(Of CardValues, Bitmap)
Dim ThisColor As CardColors = CardColor 'This works because the enum is integer based
Dim ThisColorName As String = ThisColor.ToString
For Values = 1 To 13
ThisCards(Values) = My.Resources.ResourceManager.GetObject(ThisColorName & "_" & Values.ToString)
Next
Cards(CardColor) = ThisCards
Next
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Load the cards into the lookup dictionary on startup
InitializeCards()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Show a specific picture in a picturebox
PictureBox1.Image = Cards(CardColors.Herz)(CardValues.Acht)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'Select a random card from the range of available cards and show it
Dim rnd As New Random
PictureBox1.Image = Cards(rnd.Next(1, 4))(rnd.Next(1, 13))
End Sub
End Class
因此,您只需调用一次InitializeCards。 这会将卡片加载到词典中。 Initialize函数迭代所有颜色和值,并按名称从资源集合中选择相应的卡,并将其添加到相应的字典中。
之后,您可以非常轻松地选择特定卡或随机卡的图像。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.