Click the color box game

Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
1 post Page 1 of 1
Contributors
User avatar
CodenStuff
Site Admin
Site Admin
Posts: 4389
Joined: Tue Aug 04, 2009 1:47 am

Click the color box game
CodenStuff
How to make a color box game quick and easy

You may also find some interesting functions to use in other projects, useful for dealing with lists.

First add the following to a blank form
6 Picture Boxes
2 Labels
1 Timer - set to interval 2000


Image

Now in our code we need to declare 2 things, our color list and our random color
Code: Select all
    Public Colours As New List(Of Color)
    Public RandColor As String
Now in Form_Load add the following
Code: Select all
        Dim Cols() As Color = {Color.Red, Color.Green, Color.Blue, Color.Yellow, Color.Purple, Color.Black}
        Colours = New List(Of Color)(Cols)
        For Each pb As Control In Me.Controls
            If TypeOf pb Is PictureBox Then
                AddHandler pb.Click, AddressOf Pb_Click
            End If
        Next
        DoColours()
The above with create our list of colors and also assign a click event to all our picture boxes

Now add the DoColours sub
Code: Select all
    Public Sub DoColours()
        Dim i = 0
        Colours = ShuffleList(Colours)
        For Each col As Color In Colours
            i += 1
            Me.Controls("PictureBox" & i).BackColor = col
            Me.Controls("PictureBox" & i).Tag = col.Name
        Next
        RandColor = RandomItemFromList(Colours).Name
        Label1.Text = "Click the " & RandColor.ToUpper & " box"
    End Sub
That will randomize our color list and start the game on Form_Load

Now for our 2 list functions. The first ramdomizes all the colors in our list, the second will pick a single random color from the list
Code: Select all
    Function ShuffleList(Of T)(collection As IEnumerable(Of T)) As List(Of T)
        Dim r As Random = New Random()
        ShuffleList = collection.OrderBy(Function(a) r.Next()).ToList()
    End Function

    Public Function RandomItemFromList(ByVal lst As ICollection(Of Color)) As Color
        Dim r As New Random()
        Dim TheList As New List(Of Color)(lst)
        Return TheList(r.Next(0, lst.Count))
    End Function
Now for our picture box click event
Code: Select all
    Private Sub Pb_Click(sender As Object, e As EventArgs)
        If CType(sender, PictureBox).Tag = RandColor Then
            Label2.Text = "CORRECT"
            Label2.ForeColor = Color.Green
        Else
            Label2.Text = "WRONG"
            Label2.ForeColor = Color.Red
        End If
        Label2.Visible = True
        Timer1.Enabled = True
    End Sub
This will change the labels to indicate whether or not we got the answer correct or wrong and also start the timer to restart the game

Now the timer function
Code: Select all
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Label2.Visible = False
        DoColours()
        Timer1.Enabled = False
    End Sub
Now save and run your project and boom, you have a color box game :)

Image

I hope this helps and have some fun.
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
1 post Page 1 of 1
Return to “Tutorials”