c#简单打字游戏

运行效果图如下:



功能:程序运行后,点击开始按钮,窗体中的文本框中出现字母,用户通过键盘输入文本框中字母,窗体显示用时、正确数、错误数和正确率。

按钮:开始、结束、退出。

菜单:设置(开始游戏、结束游戏、退出游戏),查看(正确率、所用时间)。

页面:


控件属性:

timer1:

enabled选择false,Interval设置为5.

timer2:

enabled选择false,Interval设置为1000.

代码:

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;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private int x = 200, y, num;
        private DateTime dt1, dt2;
        private int count_all = 0;
        private int count_correct = 0;
        private TimeSpan ts;
        Random rd = new Random();
  

        private void btnStart_Click(object sender, EventArgs e)
        {
            tsmiRate.Enabled = true;//启用控件
            dt1 = DateTime.Now;
            timer1.Start();
            timer2.Start();
            textBox1.Visible = true;
            num = rd.Next(65, 90);
        }


        private void btnStop_Click(object sender, EventArgs e)
        {
            tsmiTime.Enabled = true;
            dt2 = DateTime.Now;
            timer1.Stop();
            timer2.Stop();
            textBox1.Visible = false;
            MessageBox.Show("游戏结束。", "提示");
        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
            timer1.Stop();
            textBox1.Visible = false;
            DialogResult dr = MessageBox.Show("确定要退出吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
                Application.Exit(); 
        }

        private void tsmiStart_Click(object sender, EventArgs e)
        {
            dt1 = DateTime.Now;
            timer1.Start();
            timer2.Start();
            textBox1.Visible = true;
            num = rd.Next(65, 90); 
        }

        private void tsmiStop_Click(object sender, EventArgs e)
        {
            dt2 = DateTime.Now;
            timer1.Stop();
            timer2.Stop();
            textBox1.Visible = false;
            MessageBox.Show("游戏结束!", "提示"); 
        }

        private void tsmiQuit_Click(object sender, EventArgs e)
        {
            timer1.Stop();
            textBox1.Visible = false;
            DialogResult dr = MessageBox.Show("确定要退出吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
                Application.Exit();
        }

        private void tsmiRate_Click(object sender, EventArgs e)
        {
            double corr_rate = count_correct * 1.0 / count_all;
            string s = string.Format("{0,5:P2}",corr_rate);
            MessageBox.Show("正确率为:" + s, "正确率"); 
        }

        private void tsmiTime_Click(object sender, EventArgs e)
        {
            ts = dt2 - dt1;
            MessageBox.Show("所用时间为:" + ts.Seconds + "(s)", "所用时间");  
        }

        private void timer1_Tick(object sender, EventArgs e)//???
        {
            y++;
            if (y > this.ClientSize.Height - 5)
                y = 20;
            textBox1.Text = ((char)num).ToString().ToUpper();
            textBox1.Location = new Point(x, y);
            textBox1.ForeColor = Color.FromArgb(rd.Next(0, 255), rd.Next(0, 255), rd.Next(0, 255));
        }

        private void timer2_Tick(object sender, Eve
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值