0

我正在创建一个石头剪刀布游戏。我需要代码循环,直到玩家选择退出。我怎样才能做到这一点。

Imports System.Random

Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Label3.Visible = False
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim a As New Random()
        Dim b As Integer = a.Next(1, 3)
        Dim played As Integer = 0
        Dim won As Integer = 0
        Dim lost As Integer = 0
        Dim draw As Integer = 0
        Dim percent As Single = 0.0

        If b = 1 Then
            Label3.Text = "Rock"
            Label3.Visible = True

            Select Case ComboBox1.SelectedIndex
                Case ComboBox1.SelectedIndex = 1
                    MsgBox("Draw.")
                    draw += 1
                Case ComboBox1.SelectedIndex = 2
                    MsgBox("Paper Covers Rock. You win!")
                    won += 1
                Case ComboBox1.SelectedIndex = 3
                    MsgBox("Rock Crushes Scissors. You lose.")
                    lost += 1
            End Select

        ElseIf b = 2 Then
            Label3.Text = "Paper"
            Label3.Visible = True

            Select Case ComboBox1.SelectedIndex
                Case ComboBox1.SelectedIndex = 1
                    MsgBox("Paper Covers Rock. You lose.")
                    lost += 1
                Case ComboBox1.SelectedIndex = 2
                    MsgBox("Draw.")
                    draw += 1
                Case ComboBox1.SelectedIndex = 3
                    MsgBox("Scissors Cuts Paper. You win!")
                    won += 1
             End Select

        ElseIf b = 3 Then
            Label3.Text = "Scissors"
            Label3.Visible = True

            Select Case ComboBox1.SelectedIndex
                Case ComboBox1.SelectedIndex = 1
                    MsgBox("Rock Crushes Scissors. You win!")
                    won += 1
                Case ComboBox1.SelectedIndex = 2
                    MsgBox("Scissors Cuts Paper. You lose.")
                    lost += 1
                Case ComboBox1.SelectedIndex = 3
                    MsgBox("Draw.")
                    draw += 1
            End Select

        End If

        played += 1
        percent = won / played

        PlayedText.Text = played.ToString
        WonText.Text = won.ToString
        LostText.Text = lost.ToString
        DrawText.Text = draw.ToString
        PercentText.Text = percent.ToString


    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Close()
    End Sub
End Class
4

2 回答 2

1

您的问题与变量范围有关。由于您在按钮单击处理程序中定义了played, won, lost, draw, &percent变量,因此每次单击按钮时这些变量都是新的。您需要移动这些变量,以便在Button1_Click.

MSDN 上的此页面描述了变量范围的工作原理:http: //msdn.microsoft.com/en-us/library/1t0wsc67.aspx

现在这些变量在“过程范围”中,但您需要将它们设为“模块范围”,以便它们在按钮单击时保留它们的值。

于 2014-03-11T04:21:56.980 回答
0

我不太确定这是否是您需要的。另外我假设您正在使用winforms。我已经用 c# 编写了代码,但将其转换为 vb.net 对您来说应该不是什么挑战。

您的OnMouseDown事件处理程序应该收到一个MouseEventArgs告诉您是否按下左按钮的消息。但是您还需要创建一个条件来终止它。例如,创建一个布尔变量 sayisMouseDown并在第一次将其设置为 true。然后,当MouseUp事件发生时,您将其设置为 false。里面为假的时候也设置为真mouseDownEventHandler。示例如下。

我没有测试过,所以请这样做。

bool isMouseDown = true;

private void mouseDownEventHandler(object sender, MouseEventArgs e)
{
   if(!isMouseDown)
       isMouseDown = true;

   if(e.Button == MouseButtons.Left)
   {
     //do left stuff
     if(isMouseDown)
     {
         //Call mouseDownEventHandler again
     }
   }
   else 
   {
     // do other stuff
   }
}

private void mouseUpEventHandler(object sender, MouseEventArgs e)
{
   isMouseDown = false;
}

希望这可以帮助。

于 2014-03-11T04:27:39.230 回答