C#范式打字游戏

效果:


图二:


图三:


C#

Code:

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 打字
{
    public partial class Form1 : Form
    {
        
        double cnt = 0, sum = 0;
        private DateTime dt1, dt2;
        private TimeSpan ts;
        public Form1()
        {
            InitializeComponent();
        }
        ///产生一个随机小写字母
        string[] s = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
                          "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", 
                          "v", "w", "x", "y", "z"};
        private void button1_Click(object sender, EventArgs e)///开始游戏
        {
            label2.Visible = true;
            Random rd = new Random();
            timer1.Start();
            dt1 = DateTime.Now;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.KeyPreview = true;///窗体优先接收键盘事件
            label1.Visible = false;
            label2.Visible = false;
            label3.Visible = false;
            label4.Visible = false;
            label5.Visible = false;
        }

        private void button2_Click(object sender, EventArgs e)///结束游戏
        {
            timer1.Stop();
            MessageBox.Show("确定要结束游戏"); 
        }

        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == char.Parse(label2.Text))///keychar产生的是一个ASCII码
            {
                cnt++;
                sum++;
            }
            else
            sum++;
        }

        private void button3_Click(object sender, EventArgs e)///查看游戏
        {
            timer1.Stop();
            label1.Visible = true ;
            label3.Visible = true;
            label1.Text = (cnt / sum * 1.0).ToString();
            dt2 = DateTime.Now;
            ts = dt2-dt1;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Random rd = new Random();
            label2.Text = s[rd.Next(0, s.Length)];
            label2.Top+= 100;
            if (label2.Top>this.ClientSize.Height)
            {
                label2.Top = 0;
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult dr = MessageBox.Show("确定要退出吗?","结束提示框",MessageBoxButtons.OKCancel,MessageBoxIcon.Warning);
            if (DialogResult.Cancel==dr)
            {
                e.Cancel = true;
            }
            if (DialogResult.Yes==dr)
            {
                Application.Exit();
            }

        }

        private void 开始游戏ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            label2.Visible = true;
            Random rd = new Random();
            timer1.Start();
            dt1 = DateTime.Now;
        }

        private void 结束游戏ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("结束游戏");
        }

        private void 退出游戏ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确定要退出吗?", "结束提示框", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (DialogResult.Yes == dr)
            {
                Application.Exit();
            }
        }

        private void 正确率ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            timer1.Stop();
            label3.Visible = true;
            label1.Visible = true;
            label1.Text = (cnt / sum * 1.0).ToString();
        }

        private void 打字时间ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            label4.Visible = true;
            label5.Visible = true;
            label5.Text = ts.Seconds.ToString();
        }   

    }
}

自己的游戏自己定的规则,实在是因为写的好low 快哭了,好多规范没能改进(捂脸),跟人家满屏飞的字母真是没法比,

忙活了一天的结果,对得起自己啦,

游戏起来还是相当开森哒,啊哈啊哈


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值