Match Colours (colours, numeric-up-downs, listbox, textbox, backcolor)


This is a game where the user make estimates of the RGB parameters ranging from 0 to 255 to match the colour shown in the right panel. If the parameters are incorrect, a message to that effect will be displayed, including the total error (for the three colours). The "I Give Up" button displays the answer, and the reset button which starts a new game.
     Dim m_intR As Integer, m_intG As Integer, m_intB As Integer
     Dim m_Min As Integer = 70
     Dim m_Max As Integer = 220
     Private Sub GenerateColour()
         Dim rndRandom As Random = New Random()
         Dim intRand As Integer
         m_intR = rndRandom.Next(m_Min, m_Max + 1)
         m_intG = rndRandom.Next(m_Min, m_Max + 1)
         m_intB = rndRandom.Next(m_Min, m_Max + 1)
         txtObject.BackColor = Color.FromArgb(m_intR, m_intG, m_intB)
         nupR.Value = (m_Min + m_Max) / 2
         nupG.Value = (m_Min + m_Max) / 2
         nupB.Value = (m_Min + m_Max) / 2
     End Sub

     Private Sub btnTry_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTry.Click
         Dim intR As Integer = nupR.Value
         Dim intG As Integer = nupG.Value
         Dim intB As Integer = nupB.Value
         Dim intSum As Integer
         intSum = Math.Abs(m_intR - intR) + Math.Abs(m_intG - intG) + Math.Abs(m_intB - intB)
         If btnTry.Text = "Try" Then
             If intSum = 0 Then
                 lstDisplay.Items.Add("Congrations!")
                 btnTry.Text = "reset"
             Else
                 lstDisplay.Items.Add("Diff.=" & intSum & ", try again")
             End If
         Else
             btnTry.Text = "Try"
             lstDisplay.Items.Clear()
             GenerateColour()
         End If

     End Sub

     Private Sub ColourMatching_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
         nupR.Minimum = m_Min
         nupG.Minimum = m_Min
         nupB.Minimum = m_Min
         nupR.Maximum = m_Max
         nupG.Maximum = m_Max
         nupB.Maximum = m_Max
         GenerateColour()
     End Sub

     Private Sub btnGiveUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGiveUp.Click
         If btnGiveUp.Text = "I Give Up" Then
             lstDisplay.Items.Add("Colour is:" & m_intR & "," & m_intG & "," & m_intB)
             btnGiveUp.Text = "reset"
         Else
             lstDisplay.Items.Clear()
             GenerateColour()
             btnGiveUp.Text = "I Give Up"
         End If
     End Sub
     Private Sub UpdateTrialColour()
         txtTrial.BackColor = Color.FromArgb(nupR.Value, nupG.Value, nupB.Value)
     End Sub

     Private Sub nupR_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nupR.ValueChanged
         UpdateTrialColour()
     End Sub

     Private Sub nupG_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nupG.ValueChanged
         UpdateTrialColour()
     End Sub

     Private Sub nupB_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nupB.ValueChanged
         UpdateTrialColour()
    End Sub
Project Files updated 2004-01-01