簡體   English   中英

從3中隨機選擇1張圖像

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM