-
-
[原创]猜数字小游戏
-
发表于:
2013-11-17 00:17
4811
-
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幅图是程序运行截图,压缩包是编译后的程序,请拍砖。
[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课