你的代码不全啊,我没给你测出来,下面是我以前做的,你可以参考下:
using System;
using System.Drawing;
using System.Collections;
using
using System.Windows.Forms;
using System.Data;
using System.Threading;
namespace sgl
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class frmTypedGame : System.Windows.Forms.Form
{
private System.Windows.Forms.Panel panLetterArea;
private System.Windows.Forms.Button btnStart;
private System.Windows.Forms.Timer timer;
private components;
private System.Windows.Forms.Label labGrade;
private System.Windows.Forms.Label labGradeVaule;
int count = 0;
public frmTypedGame()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
= new ();
this.panLetterArea = new System.Windows.Forms.Panel();
this.btnStart = new System.Windows.Forms.Button();
this.timer = new System.Windows.Forms.Timer();
this.labGrade = new System.Windows.Forms.Label();
this.labGradeVaule = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// panLetterArea
//
this.panLetterArea.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(224)), ((System.Byte)(224)), ((System.Byte)(224)));
this.panLetterArea.Location = new System.Drawing.Point(16, 8);
this.panLetterArea.Name = "panLetterArea";
this.panLetterArea.Size = new System.Drawing.Size(552, 288);
this.panLetterArea.TabIndex = 0;
//
// btnStart
//
this.btnStart.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.btnStart.Location = new System.Drawing.Point(456, 312);
this.btnStart.Name = "btnStart";
this.btnStart.Size = new System.Drawing.Size(104, 32);
this.btnStart.TabIndex = 1;
this.btnStart.Text = "开始";
this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
//
// timer
//
this.timer.Interval = 1500;
this.timer.Tick += new System.EventHandler(this.timer_Tick);
//
// labGrade
//
this.labGrade.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.labGrade.Location = new System.Drawing.Point(16, 312);
this.labGrade.Name = "labGrade";
this.labGrade.Size = new System.Drawing.Size(56, 24);
this.labGrade.TabIndex = 2;
this.labGrade.Text = "分数:";
this.labGrade.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// labGradeVaule
//
this.labGradeVaule.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.labGradeVaule.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(0)), ((System.Byte)(192)));
this.labGradeVaule.Location = new System.Drawing.Point(80, 312);
this.labGradeVaule.Name = "labGradeVaule";
this.labGradeVaule.Size = new System.Drawing.Size(72, 24);
this.labGradeVaule.TabIndex = 3;
this.labGradeVaule.Text = "0";
this.labGradeVaule.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// frmTypedGame
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(592, 350);
this.Controls.Add(this.labGradeVaule);
this.Controls.Add(this.labGrade);
this.Controls.Add(this.btnStart);
this.Controls.Add(this.panLetterArea);
this.KeyPreview = true;
this.MaximizeBox = false;
this.Name = "frmTypedGame";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "打字游戏";
this.Closing += new (this.frmTypedGame_Closing);
this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.frmTypedGame_KeyPress);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new frmTypedGame());
}
//开始按钮。
private void btnStart_Click(object sender, System.EventArgs e) {
this.timer.Enabled = !this.timer.Enabled;
this.btnStart.Text = !this.timer.Enabled ? "开始" : "停止";
}
//定时器。
private void timer_Tick(object sender, System.EventArgs e) {
Label lab = new Label();
Random ran = new Random((int)DateTime.Now.Ticks);
int x = ran.Next(this.panLetterArea.Width+10);
int ch = ran.Next(97,123);
lab.Location = new System.Drawing.Point(x,0);
lab.Name = "lab";
lab.Size = new System.Drawing.Size(40, 40);
lab.Font = new System.Drawing.Font("宋体", 22F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
lab.ForeColor = Color.Purple;
lab.TabIndex = 0;
lab.Text = (char)ch + "";
this.panLetterArea.Controls.Add(lab);
LabLetter ll = new LabLetter(lab,this.panLetterArea.Height);
}
//键盘监听。
private void frmTypedGame_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) {
char ch = e.KeyChar;
foreach(Label la in this.panLetterArea.Controls){
if( la.Text == ch.ToString() ){
this.panLetterArea.Controls.Remove(la);
count += 10;
this.labGradeVaule.Text = count.ToString();
break;
}
}
}
//关闭。
private void frmTypedGame_Closing(object sender, e) {
Application.Exit();
}
}
//label类。
class LabLetter{
private Label _lab;
private int _hgt;
Thread thr;
//构造。
public LabLetter( Label lab,int hgt){
this._lab = lab;
this._hgt = hgt;
thr = new Thread(new ThreadStart(Move));
thr.IsBackground = true;
thr.Start();
}
//移动方法。
public void Move(){
lock(this){
while(true){
this._lab.Top += 10;
Thread.Sleep(400);
if( this._lab.Top == this._hgt ){
this._lab = null;
this._lab.Dispose();
thr.Abort();
}
}
}
}
}
}