效果:
图二:
图三:
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 ,好多规范没能改进(捂脸),跟人家满屏飞的字母真是没法比,
忙活了一天的结果,对得起自己啦,
游戏起来还是相当开森哒,啊哈啊哈