0

Hopefully this will make sense, but I am creating this 'board' type game where there is a die and twenty-eight labels, which I have made the labels into small squares and next to each other.

I need to figure out code which will automatically light up (change the BackColor) of the labels, based on the number generated by the die, which the player clicks on.

For example, when the player clicks the die a number is generated (1 - 6) and this is how many labels light up, and continues till all labels BackColor has changed to a different colour, e.g. green.

Code for die:

 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

So, what could I do to make sure the correct number of labels BackColor are changed? Would I need a function? Also, as the die is clicked numerous amount of times, labels are going to light up, so how do I get it so that those that haven't changed, are until all twenty-eight labels BackColor has changed?

I hope that this makes some sense?

The labels are named:

lblSquareOne, lblSquareTwo, lblSquareThree right through to lblSquareTwentyeight
4

1 回答 1

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 类,它包含对标签的引用(它可以在新建时自行获取)和标志。

然后另一个类 - LabelItems - 来管理它们。LabelItems 可以将其中的 28 个存储在 List(Of LabelItem) 中。这将使管理单个标签变得容易(无需通过数组循环)以及“全局”事物,例如清除新游戏的程序、获取到目前为止的分数、添加计时器以用于计分或时钟节拍等等

于 2013-09-23T03:36:58.113 回答