输入代码:
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+"秒");
}
}
}
运行截图:
总结:
一个小型类似金山打字游戏的项目,感觉自己还是蛮拼的。不过还有很多功能尚未学会去做。