2013-03-21 38 views
1

這裏是我的代碼的一部分..它是一個骰子游戲,檢查所有3個骰子是否相同。用戶可以持有/不支持任何3和骰子使用計時器滾動。如果它們完全相同,則點數增加1.問題是,只有在再次按下滾動按鈕時,標籤纔會更新。我如何做到這一點,因爲有3個相同的骰子數量,標籤上升到?與所有的代碼EDITEDVisual Basic - 即時更新標籤中的數據?

Dim randomObject As New Random() 
Dim n, m, o, p As Integer 
Dim RollNumber = 1 
Dim TurnNumber As Integer 
Dim Points = 0 
Dim Restart As Boolean = True 


Private Sub btnRoll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRoll.Click 


    If btnHold.Enabled = True Then 
     Timer1.Enabled = True 
    End If 
    If btnHold2.Enabled = True Then 
     Timer2.Enabled = True 
    End If 
    If btnHold3.Enabled = True Then 
     Timer3.Enabled = True 
    End If 

    TurnNumber += 1 
    lblTurns.Text = TurnNumber 

    If TurnNumber = 3 Then 
     RollNumber += 1 
     TurnNumber = 0 
    End If 

    lblRolls.Text = RollNumber 


    If Not Restart Then 
     If (lblDice.Text = lblDice2.Text) And (lblDice2.Text = lblDice3.Text) Then 
      Points += 1 

     End If 
     lblPoints.Text = Points 
    End If 
    Restart = False 






End Sub 

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick 
    m = m + 1 
    If m < 10 Then 
     n = randomObject.Next(1, 7) 
     lblDice.Text = n 
     die1PictureBox.Image = ImageList1.Images(n - 1) 
    Else 
     Timer1.Enabled = False 
     m = 0 
    End If 
End Sub 

Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick 
    m = m + 1 
    If m < 10 Then 
     n = randomObject.Next(1, 7) 
     lblDice2.Text = n 
     die2PictureBox.Image = ImageList1.Images(n - 1) 
    Else 
     Timer2.Enabled = False 
     m = 0 
    End If 
End Sub 

Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick 
    m = m + 1 
    If m < 10 Then 
     n = randomObject.Next(1, 7) 
     lblDice3.Text = n 
     die3PictureBox.Image = ImageList1.Images(n - 1) 
    Else 
     Timer3.Enabled = False 
     m = 0 
    End If 
End Sub 

Private Sub btnHold_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHold.Click 
    btnHold.Enabled = False 
    btnUnhold.Enabled = True 
End Sub 

Private Sub btnUnhold_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUnhold.Click 
    btnHold.Enabled = True 
    btnUnhold.Enabled = False 
End Sub 

Private Sub btnHold2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHold2.Click 
    btnHold2.Enabled = False 
    btnUnhold2.Enabled = True 
End Sub 

Private Sub btnUnhold2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUnhold2.Click 
    btnHold2.Enabled = True 
    btnUnhold2.Enabled = False 
End Sub 

Private Sub btnHold3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHold3.Click 
    btnHold3.Enabled = False 
    btnUnhold3.Enabled = True 
End Sub 

Private Sub btnUnhold3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUnhold3.Click 
    btnHold3.Enabled = True 
    btnUnhold3.Enabled = False 
End Sub 

Private Sub btnRestart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRestart.Click 
    Dim msg As String 
    Dim title As String 
    Dim style As MsgBoxStyle 
    Dim response As MsgBoxResult 
    msg = "Are you sure you want to start a new game? If not, you can select 'No' to continue with your current game." 
    style = MsgBoxStyle.DefaultButton2 Or _ 
     MsgBoxStyle.Critical Or MsgBoxStyle.YesNo 
    title = "New Game" 

    response = MsgBox(msg, style, title) 
    If response = MsgBoxResult.Yes Then 
     Reset() 

    End If 

    Restart = True 

End Sub 


Private Sub ColorChangeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ColorChangeToolStripMenuItem.Click 

    'Notice the ... in the menu...that means a dialog box will come 

    Dim dialog As New ColorDialog ' Color Dialog 
    Dim result As DialogResult ' stores Button clicked 

    dialog.FullOpen = True ' show all colors 
    result = dialog.ShowDialog 

    ' do nothing if user clicked dialog's Cancel Button 
    If result <> Windows.Forms.DialogResult.Cancel Then 
     ' assign new color to Paint object 
     Label1.ForeColor = dialog.Color 
    End If 


End Sub 

Private Sub FontChangeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FontChangeToolStripMenuItem.Click 
    'Notice the ... in the menu...that means a dialog box will come 

    Dim dialog As New FontDialog ' Font Dialog 
    Dim result As DialogResult ' stores Button clicked 

    ' show dialog and get result 
    result = dialog.ShowDialog() 

    ' do nothing if user clicked dialog's Cancel Button 
    If result <> Windows.Forms.DialogResult.Cancel Then 
     ' assign new font value to TextBox 
     If Windows.Forms.DialogResult.OK = MessageBox.Show("Changing Font. Click Ok to Change,\n Cancel for no change to occur.", _ 
           "Warning", MessageBoxButtons.OKCancel) Then 
      Label1.Font = dialog.Font 
     End If 


    End If 


End Sub 

Private Sub SelectAColorToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SelectAColorToolStripMenuItem.Click 

    Dim dialog As New ColorDialog ' Color Dialog 
    Dim result As DialogResult ' stores Button clicked 

    dialog.FullOpen = True ' show all colors 
    result = dialog.ShowDialog() 

    ' do nothing if user clicked dialog's Cancel Button 
    If result <> Windows.Forms.DialogResult.Cancel Then 
     Me.BackColor = dialog.Color 
    End If 

End Sub 

Private Sub SelectAnImageToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SelectAnImageToolStripMenuItem.Click 
    Dim msg As String 
    Dim title As String 
    Dim style As MsgBoxStyle 
    Dim response As MsgBoxResult 
    msg = "Are you sure you want to see what happens when you get a perfect score without actually playing? That's cheating.. and will reset the game!" 
    style = MsgBoxStyle.DefaultButton2 Or _ 
     MsgBoxStyle.Critical Or MsgBoxStyle.YesNo 
    title = "New Game" 

    response = MsgBox(msg, style, title) 
    If response = MsgBoxResult.Yes Then 
     Dim FileChooser As New OpenFileDialog() 
     Dim result As DialogResult 

     result = DialogResult = FileChooser.ShowDialog() 

     If result <> Windows.Forms.DialogResult.Cancel Then 
      If FileChooser.FileName <> "" Then 
       Me.BackgroundImage = System.Drawing.Image.FromFile(FileChooser.FileName) 
       Reset() 
      Else 
       MessageBox.Show("No Change Made. File Not Selected!", "Warning", _ 
           System.Windows.Forms.MessageBoxButtons.OK, _ 
           System.Windows.Forms.MessageBoxIcon.Exclamation) 
      End If 
     End If 
    End If 

End Sub 

Private Sub btnHow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHow.Click 
    MessageBox.Show(" You will have 5 rolls. In each roll you have up to 3 turns. Your object in each of the turns is to get three of a kind on the dice. You can hold some die and roll only some of the die, or you can reroll all the dice. If in a turn you get three of a kind, you get a point. At the end of 5 turns you could have a maximum of 5 points and you will get a special prize.") 
End Sub 

Sub Reset() 
    RollNumber = 1 
    lblRolls.Text = RollNumber 
    TurnNumber = 0 
    lblTurns.Text = TurnNumber 
    Points = 0 
    lblPoints.Text = Points 
    lblPoints2.Text = "" 

    btnRoll.Enabled = True 
    btnHold.Enabled = True 
    btnHold2.Enabled = True 
    btnHold3.Enabled = True 
    btnUnhold.Enabled = False 
    btnUnhold2.Enabled = False 
    btnUnhold3.Enabled = False 

    die1PictureBox.Image = Nothing 
    die2PictureBox.Image = Nothing 
    die3PictureBox.Image = Nothing 
    lblDice.Text = "" 
    lblDice2.Text = "" 
    lblDice3.Text = "" 

End Sub 

Private Sub btnTriple_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTriple.Click 
    Points += 1 
    lblPoints.Text = Points 

    btnTriple.Visible = False 
End Sub 

末級

+0

哪裏是上面的代碼?這是用於滾動按鈕嗎?或者它是一個子程序?如果上面的代碼不是它,滾動按鈕的代碼在哪裏? – Zeddy 2013-03-21 00:15:15

+0

@ZafKhan這是滾動按鈕 – 2013-03-21 00:20:32

+0

@ Zaf Khan感謝您的關注 – 2013-03-21 00:22:55

回答

2

此答案將刪除我在另一個問題上給出的答案的必要性。由於您使用計時器來生成骰子編號,因此請在創建禁用每個計時器後調用的方法後,此方法將檢查所有計時器是否已禁用,然後檢查是否相等。看看它是否適合你。您可能希望將更多來自您的EventHandler的代碼移至此方法。

檢查相等性的方法。

Private Sub CheckForMatch() 
    If Not Timer1.Enabled And Not Timer2.Enabled And Not Timer3.Enabled Then 
     If (lblDice.Text = lblDice2.Text) And (lblDice2.Text = lblDice3.Text) Then 
      Points += 1 
      lblPoints.Text = Points 
     End If 
    End If 
End Sub 

體改定時器方法例如添加到所有你的三個定時器的

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick 
    m = m + 1 
    If m < 10 Then 
     n = randomObject.Next(1, 7) 
     lblDice.Text = n 
     die1PictureBox.Image = ImageList1.Images(n - 1) 
    Else 
     Timer1.Enabled = False 
     CheckForMatch() 
     m = 0 
    End If 
End Sub 
+0

謝謝,您是救生員。我永遠不可能自己做到這一點。 – 2013-03-21 01:58:02

+0

@BillStack歡迎您,很高興有幫助 – 2013-03-21 02:28:26

0

嘗試調用DoEvents設置lblRolls.Text後:

.... 

lblRolls.Text = RollNumber 
Application.DoEvents() 

.... 

編輯

Private Sub SetTimers(Enable As Boolean) 
    Timer1.Enabled = Enable 
    Timer2.Enabled = Enable 
    Timer3.Enabled = Enable 
End Sub 

'''' 
SetTImers(True) 
.... 

If TurnNumber = 3 Then 
    RollNumber += 1 
    TurnNumber = 0 
    SetTimers(False) 
    Application.DoEvents() 
    lblRolls.Text = RollNumber   
    Application.DoEvents() 
    SetTimers(True) 
Else 
    lblRolls.Text = RollNumber 
End If 

.... 
+0

我曾嘗試過,沒有運氣。不知道爲什麼。 – 2013-03-21 00:21:39

+0

如何在調用DoEvents之前禁用定時器,然後重新啓用它? – 2013-03-21 00:39:58

+0

我該怎麼做呢? – 2013-03-21 00:51:14

0

結束後將標籤線向下移動,看看是否有幫助。 我看不出你在哪裏增加點 - 所以很難說。

If (lblDice.Text = lblDice2.Text) And (lblDice2.Text = lblDice3.Text) Then 
    btnTriple.Visible = True 
End If 

甚至將它放在restart = false行之前,而不是它現在的位置。

lblPoints.Text = Points 
Restart = False 
+0

它不能與展示位置有關,因爲我試圖將它移到任何地方。此外,我在這一行增加它如果(lblDice.Text = lblDice2.Text)和(lblDice2.Text = lblDice3.Text)然後 – 2013-03-21 00:30:33

+0

哦,這很奇怪,因爲我不記得在那裏看到它,而且沒有理由因爲它在這種情況下不更新。 – Zeddy 2013-03-21 00:37:35

+0

是的,抱歉,這是我的錯誤。我一直在編輯這部分代碼,我忘了將這部分放回去。它可以工作,但是你必須再次敲打它才能顯示,它不會立即更新 – 2013-03-21 00:47:54