C#做应用啦---打字游戏

   输入代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace using_System_
{
    public partial class Form1 : Form
    {
        int right=0,all=0;
        DateTime dt1, dt2;
        TimeSpan ts;
        public Form1()
        {
            InitializeComponent();
        }

        private void 开始游戏ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.timer1.Start();
            dt1 = System.DateTime.Now;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Label I = new Label();
            Random rd = new Random();
            I.Font = new Font("宋体", 30);
            I.Size = new Size(40,40);
            I.BackColor = Color.Yellow;
            char ss = (char)((int)'a' + rd.Next(26));//随机水平字母
            I.Text = ss.ToString();
            I.Left = rd.Next(this.Width);//随机水平位置
            this.Controls.Add(I);//将标签添加到指定的面板控件中
            foreach (Control c in this.Controls)
            {
                if (c.GetType() == typeof(Label))
                {
                    c.Top +=30;//向下移动 
                    if (c.Top == this.Height)
                    {
                        c.Dispose();//到底消除
                    }
                }
            }
        }

        private void 简单ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            timer1.Interval = 1000;
        }

        private void 中等ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            timer1.Interval = 500;
        }

        private void 困难ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            timer1.Interval = 100;
        }

        private void 暂停游戏ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (DialogResult.Yes == MessageBox.Show("确认暂停吗[Y/N]", "暂停确认窗口", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2))
            {
                timer1.Stop();
                dt2 = System.DateTime.Now;
            }
        }

        private void 结束游戏ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (DialogResult.Yes == MessageBox.Show("确认结束吗[Y/N]", "结束提示窗口", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2))
            {
                Application.Exit();
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.timer1.Interval = 1000;          
        }

        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            all++;
            foreach (Control c in this.Controls)
            {
                if (c.GetType() == typeof(Label))//按键和标签字母一致的消除
                {
                    Label t = (Label)c;
                    if (t.Text == e.KeyChar.ToString())                     
                    {
                        this.Controls.Remove(c);
                        right++;
                        break;
                    }
                }
            }
        }

        private void 总共打字数ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show(all.ToString());
        }

        private void 打字正确率ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string str = "";
            double b =right; 
            double d =all; 
            double Rv = (b / d);
            Rv*= 100;
            Rv=Math.Round(Rv); 
            str = Rv.ToString() + "%";
            MessageBox.Show(str); 
        }

        private void 打字时间ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ts = dt1 - dt2;
            string fen = ts.Minutes.ToString();
            string miao = ts.Seconds.ToString();
            MessageBox.Show(fen + "分" + miao+"秒");
        }
    }
}


运行截图:

总结:

一个小型类似金山打字游戏的项目,感觉自己还是蛮拼的。不过还有很多功能尚未学会去做。


评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

潇潇雨歇_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值