所以我有一个名为SquareNumber的变量,这决定了你角色的正方形(我正在制作一个蛇和梯子游戏进行作业。)每次按下按钮,它会创建两个随机数,然后添加它们一起作为一个名为Total的新变量。 SquareNumber获取添加的总数。然后,我有一长串代码,应该使之前标签的颜色变为黑色,新标签的颜色会根据SquareNumber的值而改变。但是,每次单击按钮时,方形编号始终等于总数。例如,当前总数是5,然后SquareNumber被添加5.接下来假设下一个总数是7,然后SquareNumber应该得到7并且是12(5 + 7 = 12)但它只是说7: (感谢任何帮助。谢谢:)。
'Creating random numbers and displaying them as labels.
Dim SquareNumber1 As Integer
Dim Total As Integer
Dim RandomGen As New Random
Dim RandomInt1 As Integer
RandomInt1 = RandomGen.Next(2, 7)
Player1DiceRoll1.Text = CStr(RandomInt1)
Dim RandomInt2 As Integer
RandomInt2 = RandomGen.Next(2, 7)
Player1DiceRoll2.Text = CStr(RandomInt2)
Total = RandomInt2 + RandomInt1
Player1OutputLabel.Text = CStr(Total)
SquareNumber1 = SquareNumber1 + Total
'Change a the last label back to white.
If Label1.ForeColor = Color.Lime Then
Label1.ForeColor = Color.Black
ElseIf Label2.Forecolor = Color.Lime Then
Label2.ForeColor = Color.Black
ElseIf Label3.ForeColor = Color.Lime Then
Label3.ForeColor = Color.Black
ElseIf Label4.ForeColor = Color.Lime Then
Label4.ForeColor = Color.Black
ElseIf Label5.ForeColor = Color.Lime Then
Label5.ForeColor = Color.Black
ElseIf Label6.ForeColor = Color.Lime Then
Label6.ForeColor = Color.Black
ElseIf Label7.ForeColor = Color.Lime Then
Label7.ForeColor = Color.Black
ElseIf Label8.ForeColor = Color.Lime Then
Label8.ForeColor = Color.Black
ElseIf Label9.ForeColor = Color.Lime Then
Label9.ForeColor = Color.Black
ElseIf Label10.ForeColor = Color.Lime Then
Label10.ForeColor = Color.Black
ElseIf Label11.ForeColor = Color.Lime Then
Label11.ForeColor = Color.Black
ElseIf Label12.ForeColor = Color.Lime Then
Label12.ForeColor = Color.Black
ElseIf Label13.ForeColor = Color.Lime Then
Label13.ForeColor = Color.Black
ElseIf Label14.ForeColor = Color.Lime Then
Label14.ForeColor = Color.Black
ElseIf Label15.ForeColor = Color.Lime Then
Label15.ForeColor = Color.Black
ElseIf Label16.ForeColor = Color.Lime Then
Label16.ForeColor = Color.Black
ElseIf Label17.ForeColor = Color.Lime Then
Label17.ForeColor = Color.Black
ElseIf Label18.ForeColor = Color.Lime Then
Label18.ForeColor = Color.Black
ElseIf Label19.ForeColor = Color.Lime Then
Label19.ForeColor = Color.Black
ElseIf Label20.ForeColor = Color.Lime Then
Label20.ForeColor = Color.Black
ElseIf Label21.ForeColor = Color.Lime Then
Label21.ForeColor = Color.Black
ElseIf Label22.ForeColor = Color.Lime Then
Label22.ForeColor = Color.Black
ElseIf Label23.ForeColor = Color.Lime Then
Label23.ForeColor = Color.Black
ElseIf Label24.ForeColor = Color.Lime Then
Label24.ForeColor = Color.Black
ElseIf Label25.ForeColor = Color.Lime Then
Label25.ForeColor = Color.Black
ElseIf Label26.ForeColor = Color.Lime Then
Label26.ForeColor = Color.Black
ElseIf Label27.ForeColor = Color.Lime Then
Label27.ForeColor = Color.Black
ElseIf Label28.ForeColor = Color.Lime Then
Label28.ForeColor = Color.Black
ElseIf Label29.ForeColor = Color.Lime Then
Label29.ForeColor = Color.Black
ElseIf Label30.ForeColor = Color.Lime Then
Label30.ForeColor = Color.Black
ElseIf Label31.ForeColor = Color.Lime Then
Label31.ForeColor = Color.Black
ElseIf Label32.ForeColor = Color.Lime Then
Label32.ForeColor = Color.Black
ElseIf Label33.ForeColor = Color.Lime Then
Label33.ForeColor = Color.Black
ElseIf Label34.ForeColor = Color.Lime Then
Label34.ForeColor = Color.Black
ElseIf Label35.ForeColor = Color.Lime Then
Label35.ForeColor = Color.Black
ElseIf Label36.ForeColor = Color.Lime Then
Label36.ForeColor = Color.Black
ElseIf Label37.ForeColor = Color.Lime Then
Label37.ForeColor = Color.Black
ElseIf Label38.ForeColor = Color.Lime Then
Label38.ForeColor = Color.Black
ElseIf Label39.ForeColor = Color.Lime Then
Label39.ForeColor = Color.Black
ElseIf Label40.ForeColor = Color.Lime Then
Label40.ForeColor = Color.Black
ElseIf Label41.ForeColor = Color.Lime Then
Label41.ForeColor = Color.Black
ElseIf Label42.ForeColor = Color.Lime Then
Label42.ForeColor = Color.Black
ElseIf Label43.ForeColor = Color.Lime Then
Label43.ForeColor = Color.Black
ElseIf Label44.ForeColor = Color.Lime Then
Label44.ForeColor = Color.Black
ElseIf Label45.ForeColor = Color.Lime Then
Label45.ForeColor = Color.Black
ElseIf Label46.ForeColor = Color.Lime Then
Label46.ForeColor = Color.Black
ElseIf Label47.ForeColor = Color.Lime Then
Label47.ForeColor = Color.Black
ElseIf Label48.ForeColor = Color.Lime Then
Label48.ForeColor = Color.Black
ElseIf Label49.ForeColor = Color.Lime Then
Label49.ForeColor = Color.Black
End If
'Changing the new label to the right coloUr.
If SquareNumber1 = 1 Then
Label1.ForeColor = Color.Lime
ElseIf SquareNumber1 = 2 Then
Label2.ForeColor = Color.Lime
ElseIf SquareNumber1 = 3 Then
Label3.ForeColor = Color.Lime
ElseIf SquareNumber1 = 4 Then
Label4.ForeColor = Color.Lime
ElseIf SquareNumber1 = 5 Then
Label5.ForeColor = Color.Lime
ElseIf SquareNumber1 = 6 Then
Label6.ForeColor = Color.Lime
ElseIf SquareNumber1 = 7 Then
Label7.ForeColor = Color.Lime
ElseIf SquareNumber1 = 8 Then
Label8.ForeColor = Color.Lime
ElseIf SquareNumber1 = 9 Then
Label9.ForeColor = Color.Lime
ElseIf SquareNumber1 = 10 Then
Label10.ForeColor = Color.Lime
ElseIf SquareNumber1 = 11 Then
Label11.ForeColor = Color.Lime
ElseIf SquareNumber1 = 12 Then
Label12.ForeColor = Color.Lime
ElseIf SquareNumber1 = 13 Then
Label13.ForeColor = Color.Lime
ElseIf SquareNumber1 = 14 Then
Label14.ForeColor = Color.Lime
ElseIf SquareNumber1 = 15 Then
Label15.ForeColor = Color.Lime
ElseIf SquareNumber1 = 16 Then
Label16.ForeColor = Color.Lime
ElseIf SquareNumber1 = 17 Then
Label17.ForeColor = Color.Lime
ElseIf SquareNumber1 = 18 Then
Label18.ForeColor = Color.Lime
ElseIf SquareNumber1 = 19 Then
Label19.ForeColor = Color.Lime
ElseIf SquareNumber1 = 20 Then
Label20.ForeColor = Color.Lime
ElseIf SquareNumber1 = 21 Then
Label21.ForeColor = Color.Lime
ElseIf SquareNumber1 = 22 Then
Label22.ForeColor = Color.Lime
ElseIf SquareNumber1 = 23 Then
Label23.ForeColor = Color.Lime
ElseIf SquareNumber1 = 24 Then
Label24.ForeColor = Color.Lime
ElseIf SquareNumber1 = 25 Then
Label25.ForeColor = Color.Lime
ElseIf SquareNumber1 = 26 Then
Label26.ForeColor = Color.Lime
ElseIf SquareNumber1 = 27 Then
Label27.ForeColor = Color.Lime
ElseIf SquareNumber1 = 28 Then
Label28.ForeColor = Color.Lime
ElseIf SquareNumber1 = 29 Then
Label29.ForeColor = Color.Lime
ElseIf SquareNumber1 = 30 Then
Label30.ForeColor = Color.Lime
ElseIf SquareNumber1 = 31 Then
Label31.ForeColor = Color.Lime
ElseIf SquareNumber1 = 32 Then
Label32.ForeColor = Color.Lime
ElseIf SquareNumber1 = 33 Then
Label33.ForeColor = Color.Lime
ElseIf SquareNumber1 = 34 Then
Label34.ForeColor = Color.Lime
ElseIf SquareNumber1 = 35 Then
Label35.ForeColor = Color.Lime
ElseIf SquareNumber1 = 36 Then
Label36.ForeColor = Color.Lime
ElseIf SquareNumber1 = 37 Then
Label37.ForeColor = Color.Lime
ElseIf SquareNumber1 = 38 Then
Label38.ForeColor = Color.Lime
ElseIf SquareNumber1 = 39 Then
Label39.ForeColor = Color.Lime
ElseIf SquareNumber1 = 40 Then
Label40.ForeColor = Color.Lime
ElseIf SquareNumber1 = 41 Then
Label41.ForeColor = Color.Lime
ElseIf SquareNumber1 = 42 Then
Label42.ForeColor = Color.Lime
ElseIf SquareNumber1 = 43 Then
Label43.ForeColor = Color.Lime
ElseIf SquareNumber1 = 44 Then
Label44.ForeColor = Color.Lime
ElseIf SquareNumber1 = 45 Then
Label45.ForeColor = Color.Lime
ElseIf SquareNumber1 = 46 Then
Label46.ForeColor = Color.Lime
ElseIf SquareNumber1 = 47 Then
Label47.ForeColor = Color.Lime
ElseIf SquareNumber1 = 48 Then
Label48.ForeColor = Color.Lime
ElseIf SquareNumber1 >= 49 Then
Label49.ForeColor = Color.Lime
Dim Win As Integer = MsgBox("Well done, Mordor (Player1) won the game!", MessageBoxButtons.OK)
If Win = DialogResult.OK Then
End
Else
Button1.Enabled = False
Button2.Enabled = False
End If
ElseIf SquareNumber1 <= 0 Then
SquareNumber1 = 1
Label1.ForeColor = Color.Lime
End If
答案 0 :(得分:0)
有几种方法可以给这只猫(或蛇)上皮 正如@GEnGEr所提到的,您可以将值存储在工作表中,并在子数据开头读取它,然后在结尾处写入。
或者在比赛结束前你不能退出该分区。
使用do while或do until循环来检查赢或输条件,并仅在满足一个或另一个条件时退出sub。可能会投入&#34;我退出&#34;按钮也可以提前退出。
还有其他方法,但这两个方面起点很简单。
答案 1 :(得分:0)
这是偏离主题;但我想告诉你一个更简单的方法。
Sub Example()
Static x As Integer ' Static retains values between calls.
Dim Total As Integer
' Update old square.
Me.Controls("Label" & CStr(SquareNumber1)).ForeColor = Color.Black
' Roll dice.
SquareNumber1 = SquareNumber1 + Total
' Update new square.
Me.Controls("Label" & CStr(SquareNumber1)).ForeColor = Color.Lime
End Sub
此代码使用名为Controls的form属性。控件是一个数组。数组中的每个元素都是表单上的控件。
使用此方法,您可以直接访问您想要更新的控件(按名称),而无需检查游戏中的每个方块。
如果您在游戏中添加了10个新方块,您的代码将继续工作,而不会进行任何更改(只要它们遵循相同的命名约定)。
<强> ANSWER 强>
请参阅@RicardoPontual&amp; @EnGEr的工作答案。
答案 2 :(得分:0)
不太确定你使用哪种语言,因为它与VBA非常相似,但有些东西我无法实际掌握
以下是VBA环境假设:
Random
是你的一类Color
是你的一些类/枚举但我想你应该不是VBA,仍然可以应用一些主要的编码解决方案
这里是要放在用户窗体代码窗格中的代码:
Dim SquareNumber1 As Integer '<-- userform scoped variable
Dim iLastLabel As Long '<-- userform scoped variable
Sub Button_Click()
'Creating random numbers and displaying them as labels.
Dim Total As Integer
Dim RandomGen As New Random
Dim RandomInt1 As Integer
RandomInt1 = RandomGen.Next(2, 7)
Player1DiceRoll1.Text = CStr(RandomInt1)
Dim RandomInt2 As Integer
RandomInt2 = RandomGen.Next(2, 7)
Player1DiceRoll2.Text = CStr(RandomInt2)
Total = RandomInt2 + RandomInt1
Player1OutputLabel.Text = CStr(Total)
SquareNumber1 = SquareNumber1 + Total
With Me '<-- reference to Userform
'Change a the last label back to white.
If iLastLabel > 0 Then .Controls("Label" & iLbl).ForeColor = Color.Black
'Changing the new label to the right colour.
Select Case SquareNumber1
Case Is <= 0
SquareNumber1 = 1
Label1.ForeColor = Color.Lime
iLastLabel = 1
Case Is <= 48
.Controls("Label" & SquareNumber1).ForeColor = Color.Lime
iLastLabel = SquareNumber1
Case Else
Label49.ForeColor = Color.Lime
Dim Win As Integer = MsgBox("Well done, Mordor (Player1) won the game!", MessageBoxButtons.OK)
If Win = DialogResult.OK Then Exit Sub
Button1.Enabled = False
Button2.Enabled = False
End Select
End With
End Sub