我正在用视觉基础制作石头剪刀布游戏,其基础是玩家的选择是由石头纸还是剪刀的单选按钮确定的,而计算机的选择则基于1-3块石头之间的随机数3纸等于2剪刀是1。 单击播放按钮后,它会运行if语句,以检查是否已选中了摇滚按钮,并且随机int等于摇滚,在这种情况下它是平局,但是如果您选择摇滚并且随机int Val是剪刀,那么您赢了并它会将您的分数加1
问题是分数不超过1并且我不理解
此处提供代码:
Public Class gameForm
Private Sub btnPlay_Click(sender As Object, e As EventArgs) Handles btnPlay.Click
Try
Dim playerscore = 0
Dim comscore = 0
Dim rock As Integer = 3
Dim paper As Integer = 2
Dim scissors As Integer = 1
Dim number As Integer = CInt(Int((3 * Rnd()) + 1))
Randomize()
If rbRock.Checked And number = rock Then
lblPlayerChoice.Text = "You Chose Rock"
lblPcChoice.Text = "Computer Chose Rock"
lblGameOutcome.Text = "Draw"
ElseIf rbRock.Checked And number = paper Then
lblPlayerChoice.Text = "You Chose Rock"
lblPcChoice.Text = "Computer Chose Paper"
lblGameOutcome.Text = "Computer Wins"
comscore = comscore + 1
lblComScoreVal.Text = CStr(comscore)
ElseIf rbRock.Checked And number = scissors Then
lblPlayerChoice.Text = "You Chose Rock"
lblPcChoice.Text = "Computer Chose Scissors"
lblGameOutcome.Text = "You Win"
playerscore = playerscore + 1
lblPlayerScoreVal.Text = CStr(playerscore)
End If
Catch ex As Exception
MessageBox.Show("Unknown Error Occurred", "Error", MessageBoxButtons.OK)
End Try
End Sub
End Class
答案 0 :(得分:2)
单击按钮时,您是在将玩家的得分值初始化为零,而不是将其存储在其他位置。每次单击按钮时,分数将设置为零,然后应用计算。除了将其设置为零之外,您应该将其设置为lblComScoreVal.text的值,或者如果未将其设置为零,则将其设置为零。
答案 1 :(得分:0)
您必须将Playercore和comscore移出Sub btnPlay_Click ...
应该是这样
Public Class gameForm
Private playerscore as Integer = 0
Private comscore as Integer = 0
Private Sub btnPlay_Click(sender As Object, e As EventArgs) Handles btnPlay.Click
Try