猜数字小游戏

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NGame
{
    public partial class Frm_Main : Form
    {
        public Frm_Main()
        {
            InitializeComponent();
        }
        
        System.Threading.Thread G_th;

        Random G_random = new Random();

        int G_int_num;

        private void btn_begin_Click(object sender, EventArgs e)
        {
            RemoveControl();
            int p_int_x = 10;
            int p_int_y = 60;
            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(p_int_x, p_int_y);
                bt.Click += new EventHandler(bt_Click);
                p_int_x += 36;
                if ((i + 1) % 10 == 0)
                {
                    p_int_x = 10;
                    p_int_y += 36;
                }
                Controls.Add(bt);
            }

            G_th = new System.Threading.Thread(
                delegate()
                {
                    int P_int_count = 0;
                    while (true)
                    {
                        P_int_count =
                            ++P_int_count > 100000000 ? 0 : P_int_count;
                        this.Invoke(
                            (MethodInvoker)delegate
                            {
                                lb_time.Text =
                                    P_int_count.ToString();
                            });
                        System.Threading.Thread.Sleep(1000);
                    }
                });
            G_th.IsBackground = true;
            G_th.Start();
            G_int_num = G_random.Next(1, 100);
            btn_begin.Enabled = false;
        }

        void bt_Click(object sender, EventArgs e)
        {
            Control P_control = sender as Control;
            if (int.Parse(P_control.Name) > G_int_num)
            {
                P_control.BackColor = Color.Red;
                P_control.Enabled = false;
                P_control.Text = "大";    
            }
            if (int.Parse(P_control.Name) < G_int_num)
            {
                P_control.BackColor = Color.Red;
                P_control.Enabled = false;
                P_control.Text = "小";    

            }
            if (int.Parse(P_control.Name) == G_int_num)
            {
                G_th.Abort();
                MessageBox.Show(string.Format(
                    "Congratulations! You made the right bid! You use {0} times and {1} seconds to get it!",
                    GetCount(), lb_time.Text), "Congratulations!");
                btn_begin.Enabled = true;
            }
        }

        string GetCount()
        {
            // throw new NotImplementedException();
            int P_int_temp = 0;
            foreach (Control c in Controls)
            {
                if (!c.Enabled) P_int_temp++;
            }
            return P_int_temp.ToString();
        }

        void RemoveControl()
        {
            //throw new NotImplementedException();
            for (int i = 0; i < 100; i++)
            {
                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 Frm_Main_FormClosing(object sender, FormClosingEventArgs e)
        {
            Environment.Exit(0);
        }

        private void Frm_Main_Load(object sender, EventArgs e)
        { 
        }

    }
}













  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值