欢迎来到 嗅灵易学

零基础也能上手的脚本技术课,一对一答疑带你入门

[原创]猜数字小游戏

[原创]猜数字小游戏


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)

        { 

        }

    }

}


附件的4幅图是程序运行截图,压缩包是编译后的程序,请拍砖。

上传的附件 Ashampoo_Snap_2013.11.16_23h53m58s_004_Give it a Shot-.png
Ashampoo_Snap_2013.11.16_23h54m03s_005_Give it a Shot-.png
Ashampoo_Snap_2013.11.16_23h54m09s_006_Give it a Shot-.png
Ashampoo_Snap_2013.11.16_23h54m15s_007_Congratulations-.png
NGame.rar

注意:上传附件及图片大小不得大于30M。

⚠️ 版权声明:
本博客所有内容(含教程、源码、工具)仅供个人技术学习与研究交流使用,严禁商用、倒卖、二次分发及非法用途。
未经作者书面授权,任何组织或个人不得转载、复制或用于其他平台,违者将追究相关责任。

0 0 0 举报
复制成功