我目前正在爲一個學校項目編寫一個二十一點遊戲。 (下面是我的代碼)如何在標籤中顯示某個值時顯示圖片框
Public Class frmBlackJack
Public Counter As Integer = 0
Private Sub btnDrawCard_Click(sender As Object, e As EventArgs) Handles btnDrawCard.Click
Randomize()
Counter = Counter + 1
Dim Card1 As Integer
Dim Card2 As Integer
Dim Card3 As Integer
Dim Card4 As Integer
Dim Card5 As Integer
If Counter >= 1 Then
btnCheckScore.Enabled = True
End If
If Counter = 1 Then
Card1 = (10 * Rnd() + 1)
lblCard1.Text = Card1
lblCard1.Visible = True
End If
If Counter = 2 Then
Card2 = (10 * Rnd() + 1)
lblCard2.Text = Card2
lblCard2.Visible = True
End If
If Counter = 3 Then
Card3 = (10 * Rnd() + 1)
lblCard3.Text = Card3
lblCard3.Visible = True
End If
If Counter = 4 Then
Card4 = (10 * Rnd() + 1)
lblCard4.Text = Card4
lblCard4.Visible = True
End If
If Counter = 5 Then
Card5 = (10 * Rnd() + 1)
lblCard5.Text = Card5
lblCard5.Visible = True
End If
If Counter > 5 Then
MsgBox("You can't draw anymore cards!")
End If
End Sub
Private Sub frmBlackJack_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim ComputerCard1 As Integer
Dim ComputerCard2 As Integer
Dim ComputerCard3 As Integer
ComputerCard1 = (10 * Rnd() + 1)
ComputerCard2 = (10 * Rnd() + 1)
ComputerCard3 = (10 * Rnd() + 1)
lblComputerCard1.Text = ComputerCard1
lblComputerCard2.Text = ComputerCard2
lblComputerCard3.Text = ComputerCard3
lblComputerScore.Visible = False
lblYourScore.Visible = False
btnCheckScore.Enabled = False
End Sub
Private Sub btnCheckScore_Click(sender As Object, e As EventArgs) Handles btnCheckScore.Click
Dim PlayerSum As Integer
Dim ComputerSum As Integer
Dim youWonTitle As String = "You Won!"
Dim youWonMsg As String = "Great work! Play again?"
Dim youLostTitle As String = "You Lost!"
Dim youLostMsg As String = "Better luck next time! Play again?"
Dim youDrewTitle As String = "You Drew"
Dim youDrewMsg As String = "That was close! Play again?"
Dim style As MsgBoxStyle = MsgBoxStyle.YesNo
lblComputerCard1.Visible = True
lblComputerCard2.Visible = True
lblComputerCard3.Visible = True
btnDrawCard.Enabled = False
If Counter < 1 Then
PlayerSum = Val(lblCard1.Text)
lblPLayerScore.Text = PlayerSum
lblPLayerScore.Visible = True
lblYourScore.Visible = True
End If
If Counter > 1 Then
PlayerSum = Val(lblCard1.Text) + Val(lblCard2.Text)
lblPLayerScore.Text = PlayerSum
lblPLayerScore.Visible = True
lblYourScore.Visible = True
End If
If Counter > 2 Then
PlayerSum = Val(lblCard1.Text) + Val(lblCard2.Text) + Val(lblCard3.Text)
lblPLayerScore.Text = PlayerSum
lblPLayerScore.Visible = True
lblYourScore.Visible = True
End If
If Counter > 3 Then
PlayerSum = Val(lblCard1.Text) + Val(lblCard2.Text) + Val(lblCard3.Text) + Val(lblCard4.Text)
lblPLayerScore.Text = PlayerSum
lblPLayerScore.Visible = True
lblYourScore.Visible = True
End If
If Counter > 4 Then
PlayerSum = Val(lblCard1.Text) + Val(lblCard2.Text) + Val(lblCard3.Text) + Val(lblCard4.Text) + Val(lblCard5.Text)
lblPLayerScore.Text = PlayerSum
lblPLayerScore.Visible = True
lblYourScore.Visible = True
End If
ComputerSum = Val(lblComputerCard1.Text) + Val(lblComputerCard2.Text) + Val(lblComputerCard3.Text)
lblComputerTotal.Text = ComputerSum
lblComputerScore.Visible = True
lblComputerTotal.Visible = True
If lblCard1.Text = 1 Then
End If
If PlayerSum < 21 And PlayerSum > ComputerSum Then
Dim responseYouWon = MsgBox(youWonMsg, style, youWonTitle)
If responseYouWon = MsgBoxResult.Yes Then
btnPlayAgain.PerformClick()
Else
btnQuit.PerformClick()
End If
ElseIf ComputerSum > 21 And PlayerSum < 21 Then
Dim responseYouWon = MsgBox(youWonMsg, style, youWonTitle)
If responseYouWon = MsgBoxResult.Yes Then
btnPlayAgain.PerformClick()
Else
btnQuit.PerformClick()
End If
ElseIf ComputerSum > 21 And ComputerSum < 21 Then
Dim responseYouWon = MsgBox(youWonMsg, style, youWonTitle)
If responseYouWon = MsgBoxResult.Yes Then
btnPlayAgain.PerformClick()
Else
btnQuit.PerformClick()
End If
ElseIf PlayerSum = 21 And ComputerSum > 21 Then
Dim responseYouWon = MsgBox(youWonMsg, style, youWonTitle)
If responseYouWon = MsgBoxResult.Yes Then
btnPlayAgain.PerformClick()
Else
btnQuit.PerformClick()
End If
ElseIf PlayerSum = 21 And ComputerSum < 21 Then
Dim responseYouWon = MsgBox(youWonMsg, style, youWonTitle)
If responseYouWon = MsgBoxResult.Yes Then
btnPlayAgain.PerformClick()
Else
btnQuit.PerformClick()
End If
ElseIf ComputerSum < 21 And ComputerSum > PlayerSum Then
Dim responseYouLost = MsgBox(youLostMsg, style, youLostTitle)
If responseYouLost = MsgBoxResult.Yes Then
btnPlayAgain.PerformClick()
Else
btnQuit.PerformClick()
End If
ElseIf PlayerSum > 21 And ComputerSum < 21 Then
Dim responseYouLost = MsgBox(youLostMsg, style, youLostTitle)
If responseYouLost = MsgBoxResult.Yes Then
btnPlayAgain.PerformClick()
Else
btnQuit.PerformClick()
End If
ElseIf ComputerSum = 21 And PlayerSum <> 21 Then
Dim responseYouLost = MsgBox(youLostMsg, style, youLostTitle)
If responseYouLost = MsgBoxResult.Yes Then
btnPlayAgain.PerformClick()
Else
btnQuit.PerformClick()
End If
ElseIf ComputerSum = PlayerSum Then
Dim responseYouDrew = MsgBox(youDrewMsg, style, youDrewTitle)
If responseYouDrew = MsgBoxResult.Yes Then
btnPlayAgain.PerformClick()
Else
btnQuit.PerformClick()
End If
ElseIf PlayerSum > 21 And ComputerSum > 21 Then
Dim responseYouDrew = MsgBox(youDrewMsg, style, youDrewTitle)
If responseYouDrew = MsgBoxResult.Yes Then
btnPlayAgain.PerformClick()
Else
btnQuit.PerformClick()
End If
End If
End Sub
Private Sub btnPlayAgain_Click(sender As Object, e As EventArgs) Handles btnPlayAgain.Click
lblComputerCard1.Visible = False
lblComputerCard2.Visible = False
lblComputerCard3.Visible = False
lblComputerCard1.Text = ""
lblComputerCard2.Text = ""
lblComputerCard3.Text = ""
lblCard1.Visible = False
lblCard2.Visible = False
lblCard3.Visible = False
lblCard4.Visible = False
lblCard5.Visible = False
lblCard1.Text = ""
lblCard2.Text = ""
lblCard3.Text = ""
lblCard4.Text = ""
lblCard5.Text = ""
Counter = 0
Dim ComputerCard1 As Integer
Dim ComputerCard2 As Integer
Dim ComputerCard3 As Integer
ComputerCard1 = (10 * Rnd() + 1)
ComputerCard2 = (10 * Rnd() + 1)
ComputerCard3 = (10 * Rnd() + 1)
lblComputerCard1.Text = ComputerCard1
lblComputerCard2.Text = ComputerCard2
lblComputerCard3.Text = ComputerCard3
btnDrawCard.Enabled = True
btnCheckScore.Enabled = True
lblComputerTotal.Text = ""
lblPLayerScore.Text = ""
lblComputerTotal.Visible = False
lblPLayerScore.Visible = False
lblComputerScore.Visible = False
lblYourScore.Visible = False
End Sub
Private Sub btnQuit_Click(sender As Object, e As EventArgs) Handles btnQuit.Click
Me.Close()
End Sub
Private Sub StartGameToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles StartGameToolStripMenuItem.Click
btnDrawCard.PerformClick()
End Sub
Private Sub QuitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles QuitToolStripMenuItem.Click
Me.Close()
End Sub
末級
我希望發生什麼,是一個卡(圖片)來顯示,如果某個號碼也顯示在標籤。例如,如果值爲1顯示在lblCard1中,則表格中會彈出一張ace圖片。
(免責聲明)不確定這是否實際可行。我一直在網上尋找解決辦法,但我找不到任何東西。如果你知道如何做到這一點,請回復。
感謝
您的交易-A卡的方法是非常錯誤的。你不能隨便選一個隨機數來模擬一副牌。您的代碼中沒有任何內容可以阻止挑選更多4個值。還有那些'Card#'變量沒有被使用 – Plutonix