从3中随机选择1张图像

时间:2014-04-18 09:08:27

标签: vb.net image random picturebox

您好,我正在尝试制作卡片战争游戏,我想要做的是从总共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时,它将会很长。 我在互联网上看到你可以抓取一个随机文件,我想也许你也可以用这个

提前致谢!

2 个答案:

答案 0 :(得分:1)

你的代码做得很好很难。 但它很长,所以你可以做的就是将你的代码放在像这样的私有子中。

Private sub ChooseCard()

'Paste your code here

end sub

因此您可以将其添加到代码中以执行脚本,而不是将代码粘贴到您希望的任何位置。

ChooseCard()

这将在Private sub ChooseCard

中运行您的脚本

答案 1 :(得分:0)

您应该为卡片制作存储查找表。我将举一个例子(示例中的名称是德语,但应该是可以理解的)。作为查找,我将使用一个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函数迭代所有颜色和值,并按名称从ressources集合中选择相应的卡,并将其添加到相应的字典中。

之后,您可以非常轻松地选择特定卡或随机卡的图像。