zoukankan      html  css  js  c++  java
  • _033_猜字游戏源码

    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;
    using System.Threading;
    
    namespace 制作猜字游戏
    {
        public partial class Frm_main : Form
        {
            public int G_int_num; //定义字段,模拟全局变量
            public int G_int_temp;//定义字段,模拟全局变量
            public int G_time_num;//定义字段,模拟全局变量
            Thread G_th;//开辟一个进程
            public Frm_main()
            {
                InitializeComponent();//初始化form上的控件
                this.Text = "制作一个数字猜猜看小游戏";//更改窗口的标题
            }
            private void button1_Click(object sender, EventArgs e)//按键触发事件
            {
                //RemoveControl();
                int P_int_x = 10;//x轴初始坐标为10
                int P_int_y = 60;//y轴初始坐标为60
                for (int i = 0; i < 100; i++)//添加100个按钮
                {
                    Button bt = new Button();   //创建Button控件bt  
                    bt.Text = (i + 1).ToString();//设置button控件的文本值
                    bt.Name = (i + 1).ToString();//设置Button的Name属性
                    bt.Width = 35;//按钮的宽度
                    bt.Height = 35;//按钮的高度
                    bt.Location = new Point(P_int_x, P_int_y);//定义button控件的位置
                    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 > 100000) ? 0 : P_int_count;
                            /* 三元表达式判断P_int_count>10000是否成立,如果成立则P_int_count=0,
                             如果不成立则P_int_count=P_int_count;*/
                            this.Invoke((MethodInvoker)delegate
                                {
                                    lb_time.Text = P_int_count.ToString();
                                });
                            System.Threading.Thread.Sleep(1000);
                            G_time_num = P_int_count;
                        }
                    });
                G_th.IsBackground = true;
                G_th.Start();
    
                Random G_random = new Random();//建立一个随机数函数
                G_int_num = G_random.Next(1, 100);//产生一个随机数,在1~100之间
                button1.Enabled = false;//按钮失效
            }
    
            private 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.Gold;
                    P_control.Text = "命中";
                    MessageBox.Show("恭喜您!!猜中了!
    "+
                        string.Format("用了{0}秒钟,按{1}次", G_time_num, GetCount()));
                    G_th.Abort();  // 线程终止
                    RemoveControl();//清除所有按键
                    //goto ;
                    //Environment.Exit(0);    //强行关闭窗体
                }
                else
                {
                    if (int.Parse(P_control.Name) < G_int_num)
                    {
                        P_control.BackColor = Color.Green;    //背景 设置 绿色
                        P_control.Enabled = false;          //按钮停用
                        P_control.Text = "小";
                        MessageBox.Show("小于随机数");
                        
                    }
                    else
                    {
                        P_control.BackColor = Color.Red;    //背景 设置 红色
                        P_control.Enabled = false;          //按钮停用
                        P_control.Text = "大";
                        MessageBox.Show("大于随机数");
                    }
                    // }
                }
            }
            string GetCount()
    
            {
    
                int P_int_temp = 0; //初始化计数器
    
                foreach (Control c in Controls)//遍历控件在controls中
                {
                    if (!c.Enabled) P_int_temp++; // 计数器累加 
                }
                return P_int_temp.ToString();// 把整数变成字符串返回
    
            }
            void RemoveControl()//删除控件方法
            {
                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;
                            }
                        }
                    }
                }
            }
        }
    }
    

      

  • 相关阅读:
    IPv6 tutorial – Part 8: Special addresses
    IPv6 tutorial – Part 7: Zone ID and unique local IPv6 unicast addresses
    ipconfig命令
    netsh命令
    Android-用你自己的自定义图像资源(2)
    Android 按下电源按钮关闭小学习过程的整个长度
    将android界面背景设置为黑色
    capturing self strongly in this block is likely to lead to a retain cycle
    Hadoop-2.2.0中国文献—— Web应用代理
    使用live delegate on解决js后装html故障问题
  • 原文地址:https://www.cnblogs.com/yuanshou/p/10263494.html
Copyright © 2011-2022 走看看