C#中的配色游戏

时间:2018-08-12 10:15:21

标签: c#

我需要创建一个匹配C#中颜色对的记忆游戏 我有一个List对象,其颜色已经定义。我有一个名为AssignColorsToSquares()的函数,其中将颜色分配给每个标签的.BackColor属性。但是我想知道如何在所有标签设置为默认.Backcolor的情况下运行该应用程序,因此用户不知道哪种颜色是……这是代码:

  List<Color> colores = new List<Color>() 
    {
       Color.Green, Color.Green, Color.Red, Color.Red, Color.Yellow, Color.Yellow, Color.Fuchsia, Color.Fuchsia,Color.Blue, Color.Blue, Color.Beige, Color.Beige, Color.Pink, Color.Pink, Color.Violet, Color.Violet
    };

    /// <summary> 
    /// Assign each icon from the list of icons to a random square 
    /// </summary> 
    private void AssignColorsToSquares()
    {
        // The TableLayoutPanel has 16 labels, 
        // and the icon list has 16 icons, 
        // so an icon is pulled at random from the list 
        // and added to each label.
        foreach (Control control in tableLayoutPanel1.Controls)
        {
            Label colorLabel = control as Label;
            if (colorLabel != null)
            {
                int randomNumber = random.Next(colores.Count);
                colorLabel.BackColor = colores[randomNumber];
                colorLabel.ForeColor = colorLabel.BackColor;
                colores.RemoveAt(randomNumber);
            }
        }
    } 

    public Form1()
    {
        InitializeComponent();
        AssignIconsToSquares();
    }

    /// <summary> 
    /// Every label's Click event is handled by this event handler.
    /// </summary> 
    /// <param name="sender">The label that was clicked.</param>
    /// <param name="e"></param>
    private void label_Click(object sender, EventArgs e)
    {
        // The timer is only on after two non-matching  
        // icons have been shown to the player,  
        // so ignore any clicks if the timer is running 
        if (timer1.Enabled == true)
            return; 

        Label clickedLabel = sender as Label;

        if (clickedLabel != null)
        {
            // If the clicked label is black, the player clicked 
            // an icon that's already been revealed -- 
            // ignore the click.
            if (clickedLabel.ForeColor == Color.Black)
                // All done - leave the if statements.
                return;

            // If firstClicked is null, this is the first icon  
            // in the pair that the player clicked, 
            // so set firstClicked to the label that the player  
            // clicked, change its color to black, and return. 
            if (firstClicked == null)
            {
                firstClicked = clickedLabel;
                firstClicked.ForeColor = Color.Black;

                // All done - leave the if statements.
                return;
            }

            // If the player gets this far, the timer isn't 
            // running and firstClicked isn't null, 
            // so this must be the second icon the player clicked 
            // Set its color to black.
            secondClicked = clickedLabel;
            secondClicked.ForeColor = Color.Black;

            // Check to see if the player won.
            CheckForWinner();

            // If the player clicked two matching icons, keep them  
            // black and reset firstClicked and secondClicked  
            // so the player can click another icon. 
            if (firstClicked.BackColor == secondClicked.BackColor)
            {
                firstClicked = null;
                secondClicked = null;
                return;
            }

            // If the player gets this far, the player  
            // clicked two different icons, so start the  
            // timer (which will wait three quarters of  
            // a second, and then hide the icons).
            timer1.Start();
        }
    }

    /// <summary> 
    /// This timer is started when the player clicks  
    /// two icons that don't match, 
    /// so it counts three quarters of a second  
    /// and then turns itself off and hides both icons.
    /// </summary> 
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void timer1_Tick(object sender, EventArgs e)
    {
        // Stop the timer.
        timer1.Stop();

        // Hide both icons.
        firstClicked.ForeColor = firstClicked.BackColor;
        secondClicked.ForeColor = secondClicked.BackColor;

        // Reset firstClicked and secondClicked  
        // so the next time a label is 
        // clicked, the program knows it's the first click.
        firstClicked = null;
        secondClicked = null;
    }

    /// <summary> 
    /// Check every icon to see if it is matched, by  
    /// comparing its foreground color to its background color.  
    /// If all of the icons are matched, the player wins. 
    /// </summary> 
    private void CheckForWinner()
    {
        // Go through all of the labels in the TableLayoutPanel,  
        // checking each one to see if its icon is matched.
        foreach (Control control in tableLayoutPanel1.Controls)
        {
            Label colorLabel = control as Label;

            if (colorLabel != null)
            {
                if (colorLabel.ForeColor == colorLabel.BackColor)
                    return;
            }
        }

        // If the loop didn’t return, it didn't find 
        // any unmatched icons. 
        // That means the user won. Show a message and close the form.
        MessageBox.Show("You matched all the icons!", "Congratulations!");
        Close();
    }

}

2 个答案:

答案 0 :(得分:0)

您应该尽最大努力将匹配游戏的视图和逻辑分开。话虽如此,以下使用字典作为逻辑。该游戏可以正常运行,因此当您单击两种不匹配的颜色时,它们将在计时器结束后变为矢车菊蓝色。选择两个匹配项,它们将不会还原为矢车菊蓝色。选择全部后,您的消息即表示祝贺!出现。

    /// <summary> 
    /// Assign each icon from the list of icons to a random square 
    /// </summary> 
    Dictionary<Label, Color> labelToColor = new Dictionary<Label, Color>();
    private void AssignIconsToSquares()
    {
        // The TableLayoutPanel has 16 labels, 
        // and the icon list has 16 icons, 
        // so an icon is pulled at random from the list 
        // and added to each label.
        foreach (Control control in tableLayoutPanel1.Controls)
        {
            Label colorLabel = control as Label;
            if (colorLabel != null)
            {
                int randomNumber = random.Next(colores.Count);
                labelToColor.Add(colorLabel, colores[randomNumber]);

                //colorLabel.BackColor = colores[randomNumber];
                colorLabel.ForeColor = colorLabel.BackColor;
                colores.RemoveAt(randomNumber);
            }
        }
    }


    public Form1()
    {
        InitializeComponent();
        AssignIconsToSquares();
    }

    /// <summary> 
    /// Every label's Click event is handled by this event handler.
    /// </summary> 
    /// <param name="sender">The label that was clicked.</param>
    /// <param name="e"></param>
    private void label_Click(object sender, EventArgs e)
    {
        // The timer is only on after two non-matching  
        // icons have been shown to the player,  
        // so ignore any clicks if the timer is running 
        if (timer1.Enabled == true)
            return;

        Label clickedLabel = sender as Label;

        if (clickedLabel != null)
        {
            // If the clicked label is black, the player clicked 
            // an icon that's already been revealed -- 
            // ignore the click.
            if (clickedLabel.ForeColor == Color.Black)
                // All done - leave the if statements.
                return;

            // If firstClicked is null, this is the first icon  
            // in the pair that the player clicked, 
            // so set firstClicked to the label that the player  
            // clicked, change its color to black, and return. 
            if (firstClicked == null)
            {
                firstClicked = clickedLabel;
                firstClicked.ForeColor = Color.Black;
                firstClicked.BackColor = labelToColor[firstClicked];

                // All done - leave the if statements.
                return;
            }

            // If the player gets this far, the timer isn't 
            // running and firstClicked isn't null, 
            // so this must be the second icon the player clicked 
            // Set its color to black.
            secondClicked = clickedLabel;
            secondClicked.ForeColor = Color.Black;
            secondClicked.BackColor = labelToColor[secondClicked];

            // Check to see if the player won.
            CheckForWinner();

            // If the player clicked two matching icons, keep them  
            // black and reset firstClicked and secondClicked  
            // so the player can click another icon. 
            Color firstColor = labelToColor[secondClicked];
            if (labelToColor[firstClicked] == labelToColor[secondClicked])
            {
                firstClicked = null;
                secondClicked = null;
                return;
            }


            // If the player gets this far, the player  
            // clicked two different icons, so start the  
            // timer (which will wait three quarters of  
            // a second, and then hide the icons).
            timer1.Start();
        }
    }

    /// <summary> 
    /// This timer is started when the player clicks  
    /// two icons that don't match, 
    /// so it counts three quarters of a second  
    /// and then turns itself off and hides both icons.
    /// </summary> 
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void timer1_Tick(object sender, EventArgs e)
    {
        // Stop the timer.
        timer1.Stop();

        // Hide both icons.
        firstClicked.ForeColor = Color.CornflowerBlue;
        secondClicked.ForeColor = Color.CornflowerBlue;

        firstClicked.BackColor = Color.CornflowerBlue;
        secondClicked.BackColor = Color.CornflowerBlue;
        // Reset firstClicked and secondClicked  
        // so the next time a label is 
        // clicked, the program knows it's the first click.
        firstClicked = null;
        secondClicked = null;
    }

    /// <summary> 
    /// Check every icon to see if it is matched, by  
    /// comparing its foreground color to its background color.  
    /// If all of the icons are matched, the player wins. 
    /// </summary> 
    private void CheckForWinner()
    {
        // Go through all of the labels in the TableLayoutPanel,  
        // checking each one to see if its icon is matched.
        foreach (Control control in tableLayoutPanel1.Controls)
        {
            Label colorLabel = control as Label;

            if (colorLabel != null)
            {
                if (colorLabel.ForeColor == colorLabel.BackColor)
                    return;
            }
        }

        // If the loop didn’t return, it didn't find 
        // any unmatched icons. 
        // That means the user won. Show a message and close the form.
        MessageBox.Show("You matched all the icons!", "Congratulations!");
        Close();
    }

答案 1 :(得分:0)

我找到了想要的东西。 这是代码:

 private void AssignarColoresAEtiquetas()
    {
        foreach (Control control in tableLayoutPanel1.Controls)
        {
            Label colorLabel = control as Label;

            if (colorLabel != null)
            {                    
                    int randomNumber = random.Next(colores.Count);
                    colorLabel.BackColor = colores[randomNumber];
                    colorLabel.ForeColor = colorLabel.BackColor;
                    colorLabel.Image = Properties.Resources.Captura1;
                    colores.RemoveAt(randomNumber);                                   
            }
        }
    }

我还在标签上设置了图像,以便可以覆盖标签的背景色。这样,标签将显示为覆盖有默认图像。