按钮点击小游戏:(vs2010,C# Windows form)

本文介绍如何使用C#和Visual Studio 2010创建一个简单的按钮点击游戏。游戏中按钮会在每次点击后随机移动,并调整大小,同时显示剩余点击次数。页面左上角将显示已用时间。

按钮点击小游戏:(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.运行程序~



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值