• 【基础知识】创建匹配游戏


    namespace WindowsFormsApplication3
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                MatchGraphic();
            }
            Random random = new Random();
            Label firstLabel = null;
            Label lastLabel = null;
            List<string> icons = new List<string>() 
            {
            "!", "!", "N", "N", ",", ",", "k", "k",
            "b", "b", "v", "v", "w", "w", "z", "z"
            };
            private void MatchGraphic()
            {
                foreach (Control control in tableLayoutPanel1.Controls)
                {
                    Label iconLable=control as Label;
                    if (iconLable!=null)
                    {
                        int randomNumber = random.Next(icons.Count);
                        iconLable.Text = icons[randomNumber];
                        iconLable.ForeColor = iconLable.BackColor;
                        icons.RemoveAt(randomNumber);
                    }
                }
            }
    
            private void label_Click(object sender, EventArgs e)
            {
                if (timer1.Enabled == true)
                    return;
                Label sendLabel = sender as Label;
                if (sendLabel != null)
                {
                    if(sendLabel.ForeColor == Color.Black) 
                    return;
                    if (firstLabel == null)
                    {
                        firstLabel = sendLabel;
                        sendLabel.ForeColor = Color.Black;
                        return;
                    }
                    lastLabel = sendLabel;
                    lastLabel.ForeColor = Color.Black;
                    CheckForWinner();
                    if (firstLabel.Text == lastLabel.Text)
                    {
                        firstLabel = null;
                        lastLabel = null;
                        return;
                    }
                    timer1.Start();
                }
            }
            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 iconLabel = control as Label;
    
                    if (iconLabel != null)
                    {
                        if (iconLabel.ForeColor == iconLabel.BackColor)
                            return;
                    }
                }
                 
                MessageBox.Show("游戏胜利!", "恭喜!");
                Close();
            }
            private void timer1_Tick(object sender, EventArgs e)
            {
                timer1.Stop();
                firstLabel.ForeColor = firstLabel.BackColor;
                lastLabel.ForeColor = lastLabel.BackColor;
                firstLabel = null;
                lastLabel = null;
            }
        }
    }

    附上链接

  • 相关阅读:
    C++ Boost 函数与回调应用
    C++ Boost库 操作字符串与正则
    C++ Boost库 实现命令行解析
    PHP 开发与代码审计(总结)
    c strncpy函数代码实现
    c strcat函数代码实现
    c strcpy函数代码实现
    c strlen函数代码实现
    Java-IO流-打印流
    Java-IO流-文件复制2
  • 原文地址:https://www.cnblogs.com/songxxu/p/3552231.html
Copyright © 2020-2023  润新知