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
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
Now in our code we need to declare 2 things, our color list and our random color
Now add the DoColours sub
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
Now the timer function
I hope this helps and have some fun.
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

Now in our code we need to declare 2 things, our color list and our random color
Code: Select all
Now in Form_Load add the following Public Colours As New List(Of Color)
Public RandColor As String
Code: Select all
The above with create our list of colors and also assign a click event to all our picture boxes 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()
Now add the DoColours sub
Code: Select all
That will randomize our color list and start the game on Form_Load 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
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
Now for our picture box click event 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
Code: Select all
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 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
Now the timer function
Code: Select all
Now save and run your project and boom, you have a color box game Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Label2.Visible = False
DoColours()
Timer1.Enabled = False
End Sub


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
Copyright Information
Copyright © Codenstuff.com 2020 - 2023