按钮点击小游戏:(vs2010,C# Windows form)
每次点击按钮,按钮会出现在一个新的地方,同时按钮上显示还需要点击几次完成游戏。同时左上角显示经过的时间。下面给出做这个小游戏的步骤:
1.创建一个新的 Windows FormsApplication,可以看到一个空的windows窗口如下图所示。
2.为页面添加三个控件:在Toolbox中直接拖到窗口里即可。button(button1),label(label1),timer(timer1),括号外为控件名称,括号内为控件ID。
3.打开Form1.cs的代码,可以右键点击SolutionExplorer中的Form1.cs然后点击View Code,打开。添加下面的代码即可:
static int count = 10;
static int t = 0;
private void button1_Click(object sender, EventArgs e)
{
if (count == 1)
{
timer1.Enabled = false;
((Button)sender).Text = string.Format("{0}", t);
((Button)sender).Size = new System.Drawing.Size(75, 23);
((Button)sender).Location = new System.Drawing.Point(150, 150);
}
else
{
if (count == 10)
{timer1.Enabled = true;
}
Random r1 = new Random();
//r1[0].Next(400)
((Button)sender).Location = new System.Drawing.Point(r1.Next(100) + 100, r1.Next(100) + 100);
count--;
((Button)sender).Text = string.Format("{0}", count);
((Button)sender).Size = new System.Drawing.Size(r1.Next(70) + 10, r1.Next(20) + 5);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
t++;
label1.Text = string.Format("时间:{0}", t);
}
4.运行程序~