zoukankan      html  css  js  c++  java
  • 【基础知识】创建匹配游戏

    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;
            }
        }
    }

    附上链接

  • 相关阅读:
    .NET中使用Redis总结 —— 1.Redis搭建
    Java 通过JDBC连接Mysql数据库
    5.java设计模式之建造者模式
    4.java设计模式之原型模式
    3.java设计模式之工厂模式
    2.java设计模式之单例模式
    1.java设计模式之七大设计原则和UML类图
    1.使用javax.mail, spring的JavaMailSender,springboot发送邮件
    1.7 栈(使用数组模拟)
    1.6 单向环形链表和约瑟夫问题
  • 原文地址:https://www.cnblogs.com/songxxu/p/3552231.html
Copyright © 2011-2022 走看看