希望这是有道理的,但我正在制作这种'板'型游戏,其中有一个模具和二十八个标签,我已经将标签制成小方块并且彼此相邻。
我需要找出代码,这些代码会根据玩家点击的骰子生成的数字自动点亮(更改BackColor)标签。
例如,当玩家点击骰子时,会生成一个数字(1-6),这是多少个标签点亮,并持续到所有标签BackColor已更改为不同的颜色,例如绿色。
死亡代码:
Private Sub imgDie_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles imgDie.Click
My.Computer.Audio.Play(My.Resources.Dice, AudioPlayMode.Background)
randomNumber = rand.Next(1, 7)
If randomNumber = 1 Then
imgDie.Image = My.Resources.Die_One
ElseIf randomNumber = 2 Then
imgDie.Image = My.Resources.Die_Two
ElseIf randomNumber = 3 Then
imgDie.Image = My.Resources.Die_Three
ElseIf randomNumber = 4 Then
imgDie.Image = My.Resources.Die_Four
ElseIf randomNumber = 5 Then
imgDie.Image = My.Resources.Die_Five
ElseIf randomNumber = 6 Then
imgDie.Image = My.Resources.Die_Six
End If
End Sub
那么,我该怎样做才能确保BackColor标签的正确数量发生变化?我需要一个功能吗?此外,由于骰子被点击了很多次,标签会点亮,所以我如何才能获得它,以便那些没有改变的,直到所有二十八个标签BackColor都改变了?
我希望这有道理吗?
标签名称为:
lblSquareOne, lblSquareTwo, lblSquareThree right through to lblSquareTwentyeight
答案 0 :(得分:1)
这样的事情可能会让你开始:
Private Function ToggleLabels(ByVal NumberToDo As Integer) As Boolean
Dim R As New Random
Dim n As Integer
Dim count As Integer = 0
Dim lbl As Label
' in MY app, the labels would all be grouped (ALONE) on a panel
' so I could find them easily in its Controls Array
' I am also using the Label.Tag property to track it's state
' could also go by its BackColor
' do until we match the die count passed OR
' the new AllLitUp function tells us we are Done
Do Until (count = NumberToDo)
n = R.Next(0, 28) ' maxValue is exclusive
lbl = LabelsPanel.Controls(n)
' is this one already Lit?
If lbl.Tag = "FALSE" Then
' change the color
lbl.BackColor = TheLitColor
lbl.Tag = "TRUE" ' set indicator
count += 1 ' increase the count for this round
' dont need this here AND in the loop control
If AllLitUp() Then ' check for game over
Exit Do
End If
End If
Loop
' Return T/F is it Game Over
Return AllLitUp()
End Function
当您通过标签循环以重置新游戏的颜色时,请务必将.Tag
设置为“FALSE”(带引号)。
更好的是LabelItem类,它包含对标签的引用(它可以在New时获得)和一个标志。
然后另一个类 - LabelItems - 来管理它们。 LabelItems可以将其中的28个存储在List(Of LabelItem)中。这将使管理单个标签变得容易(无需通过阵列循环)以及“全局”事物,例如清除新游戏的程序,获得目前的分数,添加计时器用于评分或用于节拍时钟等