using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Threading; namespace DaZiGame { public partial class FormGame : Form { int count = 0; public FormGame() { InitializeComponent(); } private void FormGame_Load(object sender, EventArgs e) { Control.CheckForIllegalCrossThreadCalls = false; this.timer1.Start(); } private void timer1_Tick(object sender, EventArgs e) { Label lable = new Label(); count ++; lable.Name = "Lable" + count.ToString(); lable.Width = 50; lable.Height = 50; lable.ForeColor = Color.White; lable.Font = new System.Drawing.Font("宋体", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.Controls.Add(lable); Random random = new Random(DateTime.Now.Millisecond * DateTime.Now.Millisecond); lable.Left =10 + random.Next(200); Letter letter = new Letter(lable, this); Thread thread = new Thread(new ThreadStart(letter.Run)); thread.Start(); } private void FormGame_KeyPress(object sender, KeyPressEventArgs e) { foreach (Label a in this.Controls) { if (a == null) break; try { if (a.Text[0] == e.KeyChar) { a.Dispose(); this.Controls.Remove(a); } } catch (Exception ex) {
} } } } class Letter { Label lable; Form form; int yanse = 2; public Letter(Label lab, Form form) { this.lable = lab; this.form = form; Random random = new Random(DateTime.Now.Millisecond); yanse = random.Next(5) + 1;
lable.Text = Convert.ToChar(65 + random.Next(57)).ToString(); switch (yanse) { case 1: lable.ForeColor = Color.Violet; break; case 2: lable.ForeColor = Color.Black; break; case 3: lable.ForeColor = Color.Pink; break; case 4: lable.ForeColor = Color.Red; break; case 5: lable.ForeColor = Color.Red; break; case 6: lable.ForeColor = Color.Green; break; } } public void Run() { try { while (lable.Top <= form.Height + 100) { if (lable == null) Thread.CurrentThread.Abort(); lable.Top += 10; Thread.Sleep(100); } if (Thread.CurrentThread.IsAlive) Thread.CurrentThread.Abort(); } finally { if (!lable.Disposing) lable.Dispose(); form.Controls.Remove(lable); } } } }
对呀 刚实现的源代码 所有的 不过还得又一个Timer控件
初学,得要最简单的,一个字母往下掉的,呵呵: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms;namespace RandomChar { public partial class Form1 : Form { public Form1() { InitializeComponent(); } int success = 0; //正确数 int error = 0; //错误数 int rate = 0; //正确率 private void Form1_Load(object sender, EventArgs e) { this.timer1.Interval = 100; } //点击“开始”触发计时器 private void btnBegin_Click(object sender, EventArgs e) { this.timer1.Start(); } //点结束键停止游戏并计算正确数、错误数和正确率 private void btnOver_Click(object sender, EventArgs e) { this.timer1.Stop(); this.lblTrueNum.Text = this.success.ToString(); //输出正确数 this.lblFalseNum.Text = this.error.ToString(); //输出错误数 //判断有没有输入 if ((this.success + this.error) == 0) { DialogResult result = MessageBox.Show("您还没有输入呢!"); } //如果输入,计算正确率 else { rate = (this.success / (this.success + this.error)) * 100; this.lblRateNum.Text = rate.ToString() + "%"; //输出正确率 } } //计时器事件 private void timer1_Tick(object sender, EventArgs e) { this.lblChar1.Top += 10; //字符于上边框的距离自加10 //如果字符于上边框的距离大于窗体的高度,即重新生成字符 if (this.lblChar1.Top >= this.Height) { this.lblChar1.Top = 5; } } //键盘接收用户输入字母 private void Form1_KeyPress(object sender, KeyPressEventArgs e) { string code = e.KeyChar.ToString(); if (code.Equals(this.lblChar1.Text)) { GetNewCode(); //产生随机字符 this.success++; } else { GetNewCode(); //产生随机字符 this.error++; } } //产生随机字符的方法 private void GetNewCode() { Random rd = new Random(DateTime.Now.Millisecond); string code = Convert.ToChar(65 + rd.Next(57)).ToString().ToLower(); this.lblChar1.Text = code; this.lblChar1.Top = 5; } }}
{
this.timer1.Interval = 500;
this.timer1.Start();
}
Random r = new Random();
private void timer1_Tick(object sender, EventArgs e)
{
Label l = new Label();
char cc = (char)((int)'A' + r.Next(26));
l.Text = cc.ToString();
l.Left = r.Next(this.Width);
this.Controls.Add(l); foreach (Control c in this.Controls)
{
c.Top += 15;
if (c.Top == this.Height) c.Dispose();
}
} private void Form1_KeyDown(object sender, KeyEventArgs e)
{
foreach (Control c in this.Controls)
{
if (c.GetType() == typeof(Label))
{
Label t = (Label)c;
if (t.Text == e.KeyCode.ToString())
this.Controls.Remove(c);
} }
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace App_S2T012_Krrish_TypedGame
{
public partial class frmTypedGame : Form
{
//随机数对象,在每次实例化一个Label时创建一个一个随机数
Random _rand; //计分正确率
int _sum = 0;
//计算产生label的个数
int _count = 0; //用来存储开始按钮的点击次数
int _pointTimes = 0; //存储label并没被键盘输入给打掉的个数,即自动销毁的label的个数
int _labelCounts; //线程数组
List<Thread> _treadList = new List<Thread>(); /// <summary>
/// 只读索引,用来获得pnlShowLetters中的label
/// </summary>
/// <param name="text">label中的文本内容</param>
/// <returns>返回一个label</returns>
public Label this[string text]
{
get
{
//循环去寻找文本内容为text的label
foreach (Label label in this.pnlShowLetters.Controls)
{
//当文本内容相同,就返回该label,否则返回null;
if (label.Text.Equals(text))
{
return label;
}
}
return null;
}
}
/// <summary>
/// 产生Lable标签的事件,每隔一段时间就产生一个Lable标签
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timerProduce_Tick(object sender, EventArgs e)
{
#region 产生Lable标签 float rightRate = 0f; _rand = new Random();
//在每次实例化一个Label时创建一个一个随机数
int randNumber = _rand.Next(48, 127); char ch = (char)randNumber; //声明一个Label对象label
Label label = new Label(); //设置label的大小
label.Size = new Size(20, 20); //设置label中的文本内容
label.Text = ch.ToString(); //设置label中的文本字体大小
label.Font = new Font("宋体", 15); //测试,用来设置label的背景色
label.BackColor = Color.FromArgb(_rand.Next(0, 255), _rand.Next(0, 255), _rand.Next(0, 255)); //设置label的位置,这个位置是随机的(在x坐标上的位置是随机出现的)
label.Location = new Point(_rand.Next(0, 615), 0); //将label添加到panel中即是在pnlShowLetters中显示
this.pnlShowLetters.Controls.Add(label); //产生一个label就计数一次
this._count++; //实例化移动标签类(MoveLabel)对象
MoveLabel moveLabel = new MoveLabel(label); //实例化为每一个label添加一个移动的线程
Thread thread = new Thread(new ThreadStart(moveLabel.Move));
//开启移动线程
thread.Start();
this._treadList.Add(thread);
//测试代码,用来关闭线程
foreach (Label lab_el in this.pnlShowLetters.Controls)
{
if (lab_el == null)
{
thread.Abort();
}
} //计算正确率
rightRate = ((float)_sum / this._count) * 100; //实现动态地给显示正确率的label标签设置文本内容
this.lblResult.Text = rightRate.ToString() + "%";
#endregion
} /// <summary>
/// 输入的字符和label中的相同时,清除label
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void frmTypedGame_KeyPress(object sender, KeyPressEventArgs e)
{
//用来存储删除label以前的Panel中的控件的个数
int begion; //用来存储删除label以后的Panel中的控件的个数
int end; //计算正确率
float rightRate = 0; //记录删除以前的控件的个数
begion = this.pnlShowLetters.Controls.Count; //从pnlShowLetters的所有Label控件中去寻找文本内容为text的
#region 测试代码,这里是用for循环来做的,但是用foreach来更快
//for (int i = 0; i < this.pnlShowLetters.Controls.Count; i++)
//{
// this.pnlShowLetters.Controls.Remove(this[e.KeyChar.ToString()]); //}
#endregion
foreach (Label label in this.pnlShowLetters.Controls)
{
this.pnlShowLetters.Controls.Remove(this[e.KeyChar.ToString()]);
}
//记录删除以后的个数
end = this.pnlShowLetters.Controls.Count; //累加被删除的label的个数
_sum += (begion - end); //计算正确率
rightRate = ((float)_sum / this._count) * 100; //实现动态地给显示正确率的label标签设置文本内容
this.lblResult.Text = rightRate.ToString()+"%";
} /// <summary>
/// 用来调整难度
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void vsrlbDifficulty_Scroll(object sender, ScrollEventArgs e)
{
//加快调用Timmer空间的调用时间
this.timerProduce.Interval = 200 * (this.vsrlbDifficulty.Value - 1);
} /// <summary>
/// 用来控制血量的减少
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timerLife_Tick(object sender, EventArgs e)
{
//用来控制血量
foreach (Label outLabel in this.pnlShowLetters.Controls)
{
//判断是否已经超过屏幕底线,s是否不为空,生命值是否大于0
if ((outLabel.Top > 445) && outLabel != null && this.procbLife.Value > 0)
{
if (this.procbLife.Value == 1)
{
//将生命值设置为0
this.procbLife.Value = 0; //产生字符停止
this.timerProduce.Stop();
//测试血量的事件停止
this.timerLife.Stop(); foreach (Label tempLabel in this.pnlShowLetters.Controls)
{
if (tempLabel != null)
{
this.pnlShowLetters.Controls.Remove(tempLabel);
}
} foreach (Thread thread in this._treadList)
{
if (thread.ThreadState != ThreadState.Stopped)
{
thread.Abort();
}
}
break;
}
else
{
//生命值减1
this.procbLife.Value -= 1;
}
}
}
} private void lblStartAndEnd_Click(object sender, EventArgs e)
{
//当点击一次按钮,就计数一次
this._pointTimes++; //判断点击次数是否能被2整除,不能被2整除的就用来做为结束标志,能的作为开始标志
if (_pointTimes % 2 != 0)
{
this.lblStartAndEnd.Text = "结束↓";
//开始产生字符
this.timerProduce.Start();
//开始计算生命值
this.timerLife.Start();
}
else
{
this.lblStartAndEnd.Text = "开始↑"; //停止产生字符
this.timerProduce.Stop();
//停止计算生命值
this.timerLife.Stop();
}
} private void frmTypedGame_FormClosed(object sender, FormClosedEventArgs e)
{
foreach (Thread thread in this._treadList)
{
if (thread.ThreadState != ThreadState.Stopped)
{
thread.Abort();
}
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace App_S2T012_Krrish_TypedGame
{
/// <summary>
/// 移动标签类,用来控制标签在窗体中的移动,
/// 其中包括了,标签的移动方法move,标签属性,和成员
/// </summary>
public class MoveLabel
{
//私有成员,Label标签
Label _label; //随机数对象,用来控制label的位置,即在X轴上的位置
Random _rand = new Random(); /// <summary>
/// 读写属性,对标签的设置与获取
/// </summary>
public Label Label
{
get
{
return this._label;
}
set
{
this._label = value;
}
} /// <summary>
/// 参数化构造方法
/// </summary>
/// <param name="label">标签</param>
public MoveLabel(Label label)
{
this._label = label;
} #region 可删除的测试代码 ///// <summary>
///// 参数化构造方法重载1
///// </summary>
///// <param name="panel">整个Panel作为参数来传递</param>
//public MoveLabel(Panel panel)
//{
// this._panel = panel;
//}
#endregion #region 测试代码 /// <summary>
/// 移动标签的方法
/// </summary>
public void Move()
{
//声明一个随机对象,用来作为lable的位置随机
// _rand = new Random(); if (this._label != null)
{
while (true)
{
//让label向下落
this._label.Top += 3;
//测试,让_lable的横坐标随意变化
this._label.Left = _rand.Next(0, 590); //让线程休眠100毫秒
Thread.Sleep(100); if (this._label.Top == 450)
{
this._label = null;
//关闭当前线程,因为lable已经被删除,这里是为了释放资源
Thread.CurrentThread.Abort();
}
}
}
else
{
//关闭当前线程,因为lable已经被删除,这里是为了释放资源
Thread.CurrentThread.Abort();
}
} #endregion }
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace DaZiGame
{
public partial class FormGame : Form
{
int count = 0;
public FormGame()
{
InitializeComponent();
} private void FormGame_Load(object sender, EventArgs e)
{
Control.CheckForIllegalCrossThreadCalls = false;
this.timer1.Start();
} private void timer1_Tick(object sender, EventArgs e)
{
Label lable = new Label();
count ++;
lable.Name = "Lable" + count.ToString(); lable.Width = 50;
lable.Height = 50;
lable.ForeColor = Color.White;
lable.Font = new System.Drawing.Font("宋体", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.Controls.Add(lable);
Random random = new Random(DateTime.Now.Millisecond * DateTime.Now.Millisecond);
lable.Left =10 + random.Next(200); Letter letter = new Letter(lable, this);
Thread thread = new Thread(new ThreadStart(letter.Run));
thread.Start();
} private void FormGame_KeyPress(object sender, KeyPressEventArgs e)
{
foreach (Label a in this.Controls)
{
if (a == null)
break; try
{
if (a.Text[0] == e.KeyChar)
{
a.Dispose();
this.Controls.Remove(a);
}
}
catch (Exception ex)
{
}
}
}
} class Letter
{
Label lable;
Form form;
int yanse = 2;
public Letter(Label lab, Form form)
{
this.lable = lab;
this.form = form;
Random random = new Random(DateTime.Now.Millisecond);
yanse = random.Next(5) + 1;
lable.Text = Convert.ToChar(65 + random.Next(57)).ToString(); switch (yanse)
{
case 1:
lable.ForeColor = Color.Violet;
break;
case 2:
lable.ForeColor = Color.Black;
break;
case 3:
lable.ForeColor = Color.Pink;
break;
case 4:
lable.ForeColor = Color.Red;
break;
case 5:
lable.ForeColor = Color.Red;
break;
case 6:
lable.ForeColor = Color.Green;
break;
}
} public void Run()
{
try
{
while (lable.Top <= form.Height + 100)
{
if (lable == null)
Thread.CurrentThread.Abort();
lable.Top += 10;
Thread.Sleep(100);
}
if (Thread.CurrentThread.IsAlive)
Thread.CurrentThread.Abort();
}
finally
{
if (!lable.Disposing)
lable.Dispose();
form.Controls.Remove(lable);
}
}
}
}
刚实现的源代码
所有的
不过还得又一个Timer控件
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace RandomChar
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} int success = 0; //正确数
int error = 0; //错误数
int rate = 0; //正确率 private void Form1_Load(object sender, EventArgs e)
{
this.timer1.Interval = 100;
} //点击“开始”触发计时器
private void btnBegin_Click(object sender, EventArgs e)
{
this.timer1.Start();
} //点结束键停止游戏并计算正确数、错误数和正确率
private void btnOver_Click(object sender, EventArgs e)
{
this.timer1.Stop();
this.lblTrueNum.Text = this.success.ToString(); //输出正确数
this.lblFalseNum.Text = this.error.ToString(); //输出错误数
//判断有没有输入
if ((this.success + this.error) == 0)
{
DialogResult result = MessageBox.Show("您还没有输入呢!");
}
//如果输入,计算正确率
else
{
rate = (this.success / (this.success + this.error)) * 100;
this.lblRateNum.Text = rate.ToString() + "%"; //输出正确率
}
} //计时器事件
private void timer1_Tick(object sender, EventArgs e)
{
this.lblChar1.Top += 10; //字符于上边框的距离自加10
//如果字符于上边框的距离大于窗体的高度,即重新生成字符
if (this.lblChar1.Top >= this.Height)
{
this.lblChar1.Top = 5;
}
} //键盘接收用户输入字母
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
string code = e.KeyChar.ToString();
if (code.Equals(this.lblChar1.Text))
{
GetNewCode(); //产生随机字符
this.success++;
}
else
{
GetNewCode(); //产生随机字符
this.error++;
}
} //产生随机字符的方法
private void GetNewCode()
{
Random rd = new Random(DateTime.Now.Millisecond);
string code = Convert.ToChar(65 + rd.Next(57)).ToString().ToLower();
this.lblChar1.Text = code;
this.lblChar1.Top = 5;
} }}