【问题标题】:How do I execute code until a button is pressed?在按下按钮之前如何执行代码?
【发布时间】:2014-03-11 03:57:11
【问题描述】:

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

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

【问题讨论】:

  • 什么需要循环?从您的代码中,用户不能一直单击Button1 吗?您的所有“玩家统计信息”变量都在按钮处理程序中声明,因此它们将在每次点击时重置。
  • 我是这么认为的,但是当我第二次按下它时,play 保持 1 并重置其他并在相应的框中添加 1 并根据一场比赛计算百分比。所以,代码可以工作,但它不能识别出第二场比赛,它只是假设它是 1。我想循环游戏本身。

标签: .net vb.net do-loops


【解决方案1】:

您的问题与变量范围有关。由于您在按钮单击处理程序中定义了playedwonlostdrawpercent 变量,因此每次单击按钮时这些变量都是新的。您需要移动这些变量,以便在 Button1_Click 之外定义它们。

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

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

【讨论】:

  • 谢谢,我刚上床睡觉,躺在那里,我想通了。每次我按下按钮它都会重置,因为我将它们定义为 0。Duh... 当你离开一分钟时会发生什么令人惊讶。
  • 好的,所以它可以工作......有点。现在的问题是我的组合框索引总是选择 1(摇滚)。 'Dim selection as integer ComboBox1.SelectedIndex = selection'(我也颠倒了这个顺序) If 语句和 case 选择是相同的,除了我插入了选择变量来代替 ComboBox1.SelectedIndex。无论哪种方式都是同样的问题。
【解决方案2】:

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

OnMouseDown 事件的事件处理程序应该收到一个MouseEventArgs,它应该告诉你是否按下了左键。但是您还需要创建一个条件来终止它。例如,创建一个布尔变量 isMouseDown 并在第一次将其设置为 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;
}

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-18
    • 1970-01-01
    • 2016-12-31
    相关资源
    最近更新 更多