20161028 数字游戏

来源:互联网 发布:加油站软件app 编辑:程序博客网 时间:2024/05/17 22:09

        Thread thread;        Random random = new Random();        int num;        private void btnStart_Click(object sender, EventArgs e)        {            RemoveControls();            int x = 25;            int y = 118;            for (int i = 0; i < 100; i++)            {                Button bt = new Button();                bt.Text = (i + 1).ToString();                bt.Name = (i + 1).ToString();                bt.Width = 35;                bt.Height = 35;                bt.Location = new Point(x, y);                bt.Click += new EventHandler(bt_Click);                x += 36;                if ((i + 1) % 10 == 0)                {                    x = 25;                    y += 36;                }                Controls.Add(bt);            }            thread = new Thread(                delegate()                {                    int count = 0;                    while (true)                    {                        count = ++count > 100000000 ? 0 : count;                        this.Invoke(                            (MethodInvoker)delegate                            {                                lbTime.Text = count.ToString();                            });                        Thread.Sleep(1000);                    }                });            thread.IsBackground = true;            thread.Start();            num = random.Next(1, 100);            btnStart.Enabled = false;        }        void bt_Click(object sender, EventArgs e)        {            if (int.Parse(GetCount())>= 100)            {                if (MessageBox.Show("Game Over!") == DialogResult.OK)                {                    btnStart.Enabled = true;                }                 else                {                    return;                }                            }            Control control = sender as Control;            if (int.Parse(control.Name) > num)            {                control.BackColor = Color.Red;                control.Enabled = false;                control.Text = "大";            }            else if (int.Parse(control.Name) < num)            {                control.BackColor = Color.Blue;                control.Enabled = false;                control.Text = "小";            }            else if (int.Parse(control.Name) == num)            {                control.BackColor = Color.Green;                control.Text = "Equal";                thread.Abort();                MessageBox.Show(String.Format("恭喜您猜对了!共猜对了{0}次 用时{1}秒",                    GetCount(), lbTime.Text), "恭喜!");                btnStart.Enabled = true;            }        }        string GetCount()        {            int temp = 0;            foreach (Control c in Controls)            {                if (!c.Enabled)                {                    temp++;                }            }            return temp.ToString();        }        void RemoveControls()        {            for (int i = 0; i < 100; i++)//开始遍历100个按钮            {                if (Controls.ContainsKey((i + 1).ToString()))                {                    for (int j = 0; j < Controls.Count; j++)//遍历窗体控件集合                    {                        if (Controls[j].Name == (i + 1).ToString())                        {                            Controls.RemoveAt(j);//删除指定按钮                            break;                        }                    }                }            }        }        private void Form1_FormClosing(object sender, FormClosingEventArgs e)        {            Environment.Exit(0);        }

1 0