本程序写于刚刚学习C#线程时,游戏者可以选择四个不同级别的速度进行打字练习,在规定的时间内,用键盘敲击屏幕中出现的相应字母后,该字母便会消失,分数也随即加2,由于时间原因并没有实现太多其他功能,如级别的自动增加,点击相应字母后在该字母上出现相应效果,只是简单的对C#中的线程练习了一把,希望对初学线程的朋友能够有所帮助。
以下为运行效果图:
以下为程序源代码:
Game.cs文件:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
namespace P195
{
public class frmGame : System.Windows.Forms.Form
{
成员变量#region 成员变量
private int _level = 0;
private int _score = 0;
private Random _random = null;
private string[] letters = {"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 Thread _randomThread = null;
private System.Windows.Forms.Label _randomLable;
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.MenuItem menuItem9;
private System.Windows.Forms.MenuItem menuItem10;
private System.Windows.Forms.MenuItem mnuGame;
private System.Windows.Forms.MenuItem mnuEasy;
private System.Windows.Forms.MenuItem mnuNormal;
private System.Windows.Forms.MenuItem mnuHard;
private System.Windows.Forms.MenuItem mnuSuper;
private System.Windows.Forms.MenuItem mnuDrop;
private System.Windows.Forms.MenuItem mnuExit;
private System.Windows.Forms.MenuItem mnuHelp;
private System.Windows.Forms.MenuItem mnuAbout;
private System.Windows.Forms.Label lblLevel;
private System.Windows.Forms.Label lblScore;
private System.Windows.Forms.MainMenu mnuMain;
private System.Windows.Forms.Label lblTimeDown;
private System.Windows.Forms.Label lblTimeUp;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Timer timer2;
private System.ComponentModel.IContainer components;
#endregion
构造#region 构造
public frmGame()
{
InitializeComponent();
}
#endregion
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmGame));
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.lblTimeDown = new System.Windows.Forms.Label();
this.mnuMain = new System.Windows.Forms.MainMenu();
this.mnuGame = new System.Windows.Forms.MenuItem();
this.mnuEasy = new System.Windows.Forms.MenuItem();
this.mnuNormal = new System.Windows.Forms.MenuItem();
this.mnuHard = new System.Windows.Forms.MenuItem();
this.mnuSuper = new System.Windows.Forms.MenuItem();
this.menuItem9 = new System.Windows.Forms.MenuItem();
this.mnuDrop = new System.Windows.Forms.MenuItem();
this.menuItem10 = new System.Windows.Forms.MenuItem();
this.mnuExit = new System.Windows.Forms.MenuItem();
this.mnuHelp = new System.Windows.Forms.MenuItem();
this.mnuAbout = new System.Windows.Forms.MenuItem();
this.lblLevel = new System.Windows.Forms.Label();
this.lblScore = new System.Windows.Forms.Label();
this.lblTimeUp = new System.Windows.Forms.Label();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.timer2 = new System.Windows.Forms.Timer(this.components);
this.SuspendLayout();
//
// lblTimeDown
//
this.lblTimeDown.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.lblTimeDown.Location = new System.Drawing.Point(136, 16);
this.lblTimeDown.Name = "lblTimeDown";
this.lblTimeDown.Size = new System.Drawing.Size(385, 16);
this.lblTimeDown.TabIndex = 3;
this.lblTimeDown.Visible = false;
//
// mnuMain
//
this.mnuMain.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.mnuGame,
this.mnuHelp});
//
// mnuGame
//
this.mnuGame.Index = 0;
this.mnuGame.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.mnuEasy,
this.mnuNormal,
this.mnuHard,
this.mnuSuper,
this.menuItem9,
this.mnuDrop,
this.menuItem10,
this.mnuExit});
this.mnuGame.Text = "Game";
//
// mnuEasy
//
this.mnuEasy.Index = 0;
this.mnuEasy.Text = "Easy";
//
// mnuNormal
//
this.mnuNormal.Index = 1;
this.mnuNormal.Text = "Normal";
//
// mnuHard
//
this.mnuHard.Index = 2;
this.mnuHard.Text = "Hard";
//
// mnuSuper
//
this.mnuSuper.Index = 3;
this.mnuSuper.Text = "Super";
//
// menuItem9
//
this.menuItem9.Index = 4;
this.menuItem9.Text = "-";
//
// mnuDrop
//
this.mnuDrop.Enabled = false;
this.mnuDrop.Index = 5;
this.mnuDrop.Text = "Drop";
//
// menuItem10
//
this.menuItem10.Index = 6;
this.menuItem10.Text = "-";
//
// mnuExit
//
this.mnuExit.Index = 7;
this.mnuExit.Text = "Exit";
//
// mnuHelp
//
this.mnuHelp.Index = 1;
this.mnuHelp.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.mnuAbout});
this.mnuHelp.Text = "Help";
//
// mnuAbout
//
this.mnuAbout.Index = 0;
this.mnuAbout.Text = "About";
//
// lblLevel
//
this.lblLevel.Location = new System.Drawing.Point(8, 16);
this.lblLevel.Name = "lblLevel";
this.lblLevel.Size = new System.Drawing.Size(104, 16);
this.lblLevel.TabIndex = 4;
this.lblLevel.Text = "level";
this.lblLevel.Visible = false;
//
// lblScore
//
this.lblScore.Location = new System.Drawing.Point(544, 16);
this.lblScore.Name = "lblScore";
this.lblScore.Size = new System.Drawing.Size(48, 16);
this.lblScore.TabIndex = 5;
this.lblScore.Text = "0";
this.lblScore.Visible = false;
//
// lblTimeUp
//
this.lblTimeUp.BackColor = System.Drawing.Color.MediumVioletRed;
this.lblTimeUp.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.lblTimeUp.Location = new System.Drawing.Point(136, 16);
this.lblTimeUp.Name = "lblTimeUp";
this.lblTimeUp.Size = new System.Drawing.Size(385, 16);
this.lblTimeUp.TabIndex = 6;
this.lblTimeUp.Visible = false;
//
// pictureBox1
//
this.pictureBox1.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("pictureBox1.BackgroundImage")));
this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox1.Location = new System.Drawing.Point(0, 0);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(642, 403);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox1.TabIndex = 7;
this.pictureBox1.TabStop = false;
//
// frmGame
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackColor = System.Drawing.Color.LightBlue;
this.ClientSize = new System.Drawing.Size(642, 403);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.lblTimeUp);
this.Controls.Add(this.lblScore);
this.Controls.Add(this.lblLevel);
this.Controls.Add(this.lblTimeDown);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.Menu = this.mnuMain;
this.Name = "frmGame";
this.Text = "打字练习";
this.Load += new System.EventHandler(this.frmGame_Load);
this.ResumeLayout(false);
}
#endregion
程序入口#region 程序入口
static void Main()
{
Application.Run(new frmGame());
}
#endregion
级别选择菜单事件处理#region 级别选择菜单事件处理
private void MenuClick(object sender, System.EventArgs e)
{
MenuItem btn = (MenuItem)sender;
this.lblLevel.Text = "Level:" + btn.Text;
this.lblLevel.Visible = true;
this.lblTimeDown.Visible = true;
this.lblTimeUp.Visible = true;
this.lblScore.Visible = true;
this.pictureBox1.Visible = false;
this.lblScore.Text = "0";
this._score = 0;
this.mnuDrop.Enabled = true;
switch(btn.Text)
{
case "Easy":
this._level = 4000;
this.IsPlaying(true);
break;
case "Normal":
this._level = 3000;
this.IsPlaying(true);
break;
case "Hard":
this._level = 2000;
this.IsPlaying(true);
break;
case "Super":
this._level = 1000;
this.IsPlaying(true);
break;
}
this.timer1.Interval = this._level;
this.timer1.Start();
this.timer2.Interval = 1000;
this.timer2.Start();
}
#endregion
判断是否正在游戏,改变菜单按钮的可用性#region 判断是否正在游戏,改变菜单按钮的可用性
private void IsPlaying(bool flag)
{
if(flag)
{
this.mnuEasy.Enabled = false;
this.mnuNormal.Enabled = false;
this.mnuHard.Enabled = false;
this.mnuSuper.Enabled = false;
}
else
{
this.mnuEasy.Enabled = true;
this.mnuNormal.Enabled = true;
this.mnuHard.Enabled = true;
this.mnuSuper.Enabled = true;
}
}
#endregion
窗体加载事件#region 窗体加载事件
private void frmGame_Load(object sender, System.EventArgs e)
{
this.mnuExit.Click += new System.EventHandler(this.mnuExit_Click);
this.mnuEasy.Click += new System.EventHandler(this.MenuClick);
this.mnuNormal.Click += new System.EventHandler(this.MenuClick);
this.mnuHard.Click += new System.EventHandler(this.MenuClick);
this.mnuSuper.Click += new System.EventHandler(this.MenuClick);
this.timer1.Tick += new EventHandler(timer1_Tick);
this.timer2.Tick +=new EventHandler(timer2_Tick);
this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.frmGame_KeyPress);
this.mnuDrop.Click += new System.EventHandler(this.mnuDrop_Click);
this.mnuAbout.Click += new System.EventHandler(this.mnuAbout_Click);
}
#endregion
计时器(处理随机产生字母及速度)#region 计时器(处理随机产生字母及速度)
private void timer1_Tick(object sender, EventArgs e)
{
this._randomLable = new Label();
this._randomLable.Size = new System.Drawing.Size(30,30);
this._randomLable.Location = new System.Drawing.Point(this.RandomPoint(),50);
this._randomLable.Font = new System.Drawing.Font("宋体", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this._randomLable.Text = this.RandomLetter();
this.Controls.Add(this._randomLable);
MyThread my = new MyThread();
my.Speed = 10;
my.MyControl = this._randomLable;
this._randomThread = new Thread(new ThreadStart(my.LableMove));
this._randomThread.Start();
}
#endregion
计时器(处理随游戏时间)#region 计时器(处理随游戏时间)
private void timer2_Tick(object sender, EventArgs e)
{
this.lblTimeUp.Width -= 1;
if(this.lblTimeUp.Width == 0)
{
string score = this.lblScore.Text;
this.timer2.Stop();
this.timer1.Stop();
foreach(Control ctrl in this.Controls)
{
if(ctrl.Name!="lblLevel" && ctrl.Name!="lblScore" && ctrl.Name!="lblTimeDown" && ctrl.Name!="lblTimeUp")
ctrl.Visible = false;
}
MessageBox.Show("Time Over\nYour Score is "+score+"","Prompt");
this.IsPlaying(false);
this.lblTimeUp.Width = 385;
this.lblTimeUp.Visible = false;
this.lblTimeDown.Visible = false;
this.lblScore.Visible = false;
this.lblLevel.Visible = false;
this.pictureBox1.Visible = true;
this.mnuDrop.Enabled = false;
}
}
#endregion
随机产生字母#region 随机产生字母
private string RandomLetter()
{
DateTime date = DateTime.Now;
//this._random = new Random();
this._random = new Random((int)date.Ticks * 100);
int x = this._random.Next(26);
return this.letters[x];
}
#endregion
随机产生字母标签的横坐标#region 随机产生字母标签的横坐标
private int RandomPoint()
{
DateTime date = DateTime.Now;
//this._random = new Random();
this._random = new Random((int)date.Ticks * 52);
int x = this._random.Next(30,619);
return x;
}
#endregion
玩家键盘响应事件#region 玩家键盘响应事件
private void frmGame_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
foreach(Control ctrl in this.Controls)
{
if(e.KeyChar.ToString().ToUpper().Equals(ctrl.Text) && ctrl.Location.Y<300)
{
this.Controls.Remove(ctrl);
this._score += 2;
this.lblScore.Text = this._score.ToString();
break;
}
}
}
#endregion
退出事件处理#region 退出事件处理
private void mnuExit_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
#endregion
放弃本局游戏事件处理#region 放弃本局游戏事件处理
private void mnuDrop_Click(object sender, System.EventArgs e)
{
foreach(Control ctrl in this.Controls)
{
if(ctrl.Name!="lblLevel" && ctrl.Name!="lblScore" && ctrl.Name!="lblTimeDown" && ctrl.Name!="lblTimeUp")
ctrl.Visible = false;
}
this.IsPlaying(false);
this.lblTimeUp.Width = 385;
this.lblTimeUp.Visible = false;
this.lblTimeDown.Visible = false;
this.lblScore.Visible = false;
this.lblLevel.Visible = false;
this.pictureBox1.Visible = true;
this.timer2.Stop();
this.timer1.Stop();
this.mnuDrop.Enabled = false;
}
#endregion
关于#region 关于
private void mnuAbout_Click(object sender, System.EventArgs e)
{
MessageBox.Show("Name:Typewriter Practise\nAuthor:非凡DZ","About");
}
#endregion
}
}
MyThread.cs文件:
using System;
using System.Windows.Forms;
using System.Threading;
namespace P195
{
public class MyThread
{
private int _speed = 0;
private Control _myControl;
public int Speed
{
set
{
this._speed = value;
}
get
{
return this._speed;
}
}
public Control MyControl
{
set
{
this._myControl = value;
}
get
{
return this._myControl;
}
}
public void LableMove()
{
while(true)
{
if(!this._myControl.Visible)
{
Thread.CurrentThread.Abort();
return;
}
if(this._myControl.Location.Y >= 300)
{
this._myControl.Visible = false;
Thread.CurrentThread.Abort();
return;
}
System.Drawing.Point lablePos = this._myControl.Location;
lablePos.Offset(0,10);
this._myControl.Location = lablePos;
Thread.Sleep(1000);
}
}
}
}
点击下载源代码:
/Files/daizhenghenry/TypewriterPractise.rar