C#实现简单打字游戏
更新时间:2020年6月25日 10:34 点击:1840
本文实例为大家分享了C#实现简单打字游戏的具体代码,供大家参考,具体内容如下
运行效果图如下:
功能:程序运行后,点击开始按钮,窗体中的文本框中出现字母,用户通过键盘输入文本框中字母,窗体显示用时、正确数、错误数和正确率。
按钮:开始、结束、退出。
菜单:设置(开始游戏、结束游戏、退出游戏),查看(正确率、所用时间)。
页面:
控件属性:
timer1:
enabled选择false,Interval设置为5.
timer2:
enabled选择false,Interval设置为1000.
代码:
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 WindowsFormsApplication3 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private int x = 200, y, num; private DateTime dt1, dt2; private int count_all = 0; private int count_correct = 0; private TimeSpan ts; Random rd = new Random(); private void btnStart_Click(object sender, EventArgs e) { tsmiRate.Enabled = true;//启用控件 dt1 = DateTime.Now; timer1.Start(); timer2.Start(); textBox1.Visible = true; num = rd.Next(65, 90); } private void btnStop_Click(object sender, EventArgs e) { tsmiTime.Enabled = true; dt2 = DateTime.Now; timer1.Stop(); timer2.Stop(); textBox1.Visible = false; MessageBox.Show("游戏结束。", "提示"); } private void btnQuit_Click(object sender, EventArgs e) { timer1.Stop(); textBox1.Visible = false; DialogResult dr = MessageBox.Show("确定要退出吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning); if (dr == DialogResult.OK) Application.Exit(); } private void tsmiStart_Click(object sender, EventArgs e) { dt1 = DateTime.Now; timer1.Start(); timer2.Start(); textBox1.Visible = true; num = rd.Next(65, 90); } private void tsmiStop_Click(object sender, EventArgs e) { dt2 = DateTime.Now; timer1.Stop(); timer2.Stop(); textBox1.Visible = false; MessageBox.Show("游戏结束!", "提示"); } private void tsmiQuit_Click(object sender, EventArgs e) { timer1.Stop(); textBox1.Visible = false; DialogResult dr = MessageBox.Show("确定要退出吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning); if (dr == DialogResult.OK) Application.Exit(); } private void tsmiRate_Click(object sender, EventArgs e) { double corr_rate = count_correct * 1.0 / count_all; string s = string.Format("{0,5:P2}",corr_rate); MessageBox.Show("正确率为:" + s, "正确率"); } private void tsmiTime_Click(object sender, EventArgs e) { ts = dt2 - dt1; MessageBox.Show("所用时间为:" + ts.Seconds + "(s)", "所用时间"); } private void timer1_Tick(object sender, EventArgs e)//??? { y++; if (y > this.ClientSize.Height - 5) y = 20; textBox1.Text = ((char)num).ToString().ToUpper(); textBox1.Location = new Point(x, y); textBox1.ForeColor = Color.FromArgb(rd.Next(0, 255), rd.Next(0, 255), rd.Next(0, 255)); } private void timer2_Tick(object sender, EventArgs e) { label2.Text = (DateTime.Now - dt1).Seconds.ToString(); } private void btnStart_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode.ToString() == textBox1.Text || e.KeyCode.ToString()!=textBox1.Text) { count_all++; while (e.KeyCode.ToString() == textBox1.Text) { count_correct++; textBox1.Visible = false; textBox1.Clear(); num = rd.Next(65, 90); textBox1.Visible = true; textBox1.Text = ((char)num).ToString(); x = rd.Next(20, 400); y = rd.Next(20, 400); textBox1.Location = new Point(x, y); } } label2.Visible = true; label8.Visible = true; label6.Text = count_correct.ToString(); label7.Text = (count_all - count_correct).ToString(); string t = string.Format("{0,5:P2}", count_correct * 1.0 / count_all); label8.Text = t.ToString(); } } }
另,页面代码(全靠拖拉)如下:
namespace WindowsFormsApplication3 { partial class Form1 { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { this.components = new System.ComponentModel.Container(); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1)); this.btnStart = new System.Windows.Forms.Button(); this.btnStop = new System.Windows.Forms.Button(); this.btnQuit = new System.Windows.Forms.Button(); this.menuStrip1 = new System.Windows.Forms.MenuStrip(); this.设置ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.tsmiStart = new System.Windows.Forms.ToolStripMenuItem(); this.tsmiStop = new System.Windows.Forms.ToolStripMenuItem(); this.tsmiQuit = new System.Windows.Forms.ToolStripMenuItem(); this.查看ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.tsmiRate = new System.Windows.Forms.ToolStripMenuItem(); this.tsmiTime = new System.Windows.Forms.ToolStripMenuItem(); this.timer1 = new System.Windows.Forms.Timer(this.components); this.timer2 = new System.Windows.Forms.Timer(this.components); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.label3 = new System.Windows.Forms.Label(); this.label4 = new System.Windows.Forms.Label(); this.label5 = new System.Windows.Forms.Label(); this.label6 = new System.Windows.Forms.Label(); this.label7 = new System.Windows.Forms.Label(); this.label8 = new System.Windows.Forms.Label(); this.textBox1 = new System.Windows.Forms.TextBox(); this.pictureBox1 = new System.Windows.Forms.PictureBox(); this.pictureBox2 = new System.Windows.Forms.PictureBox(); this.menuStrip1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit(); this.SuspendLayout(); // // btnStart // this.btnStart.Location = new System.Drawing.Point(414, 86); this.btnStart.Name = "btnStart"; this.btnStart.Size = new System.Drawing.Size(92, 41); this.btnStart.TabIndex = 0; this.btnStart.Text = "开始"; this.btnStart.UseVisualStyleBackColor = true; this.btnStart.Click += new System.EventHandler(this.btnStart_Click); this.btnStart.KeyDown += new System.Windows.Forms.KeyEventHandler(this.btnStart_KeyDown); // // btnStop // this.btnStop.Location = new System.Drawing.Point(414, 188); this.btnStop.Name = "btnStop"; this.btnStop.Size = new System.Drawing.Size(92, 41); this.btnStop.TabIndex = 1; this.btnStop.Text = "结束"; this.btnStop.UseVisualStyleBackColor = true; this.btnStop.Click += new System.EventHandler(this.btnStop_Click); // // btnQuit // this.btnQuit.Location = new System.Drawing.Point(414, 292); this.btnQuit.Name = "btnQuit"; this.btnQuit.Size = new System.Drawing.Size(92, 41); this.btnQuit.TabIndex = 2; this.btnQuit.Text = "退出"; this.btnQuit.UseVisualStyleBackColor = true; this.btnQuit.Click += new System.EventHandler(this.btnQuit_Click); // // menuStrip1 // this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.设置ToolStripMenuItem, this.查看ToolStripMenuItem}); this.menuStrip1.Location = new System.Drawing.Point(0, 0); this.menuStrip1.Name = "menuStrip1"; this.menuStrip1.Size = new System.Drawing.Size(539, 25); this.menuStrip1.TabIndex = 3; this.menuStrip1.Text = "menuStrip1"; // // 设置ToolStripMenuItem // this.设置ToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.tsmiStart, this.tsmiStop, this.tsmiQuit}); this.设置ToolStripMenuItem.Name = "设置ToolStripMenuItem"; this.设置ToolStripMenuItem.Size = new System.Drawing.Size(44, 21); this.设置ToolStripMenuItem.Text = "设置"; // // tsmiStart // this.tsmiStart.Name = "tsmiStart"; this.tsmiStart.Size = new System.Drawing.Size(124, 22); this.tsmiStart.Text = "开始游戏"; this.tsmiStart.Click += new System.EventHandler(this.tsmiStart_Click); // // tsmiStop // this.tsmiStop.Name = "tsmiStop"; this.tsmiStop.Size = new System.Drawing.Size(124, 22); this.tsmiStop.Text = "结束游戏"; this.tsmiStop.Click += new System.EventHandler(this.tsmiStop_Click); // // tsmiQuit // this.tsmiQuit.Name = "tsmiQuit"; this.tsmiQuit.Size = new System.Drawing.Size(124, 22); this.tsmiQuit.Text = "退出游戏"; this.tsmiQuit.Click += new System.EventHandler(this.tsmiQuit_Click); // // 查看ToolStripMenuItem // this.查看ToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.tsmiRate, this.tsmiTime}); this.查看ToolStripMenuItem.Name = "查看ToolStripMenuItem"; this.查看ToolStripMenuItem.Size = new System.Drawing.Size(44, 21); this.查看ToolStripMenuItem.Text = "查看"; // // tsmiRate // this.tsmiRate.Name = "tsmiRate"; this.tsmiRate.Size = new System.Drawing.Size(124, 22); this.tsmiRate.Text = "正确率"; this.tsmiRate.Click += new System.EventHandler(this.tsmiRate_Click); // // tsmiTime // this.tsmiTime.Name = "tsmiTime"; this.tsmiTime.Size = new System.Drawing.Size(124, 22); this.tsmiTime.Text = "所用时间"; this.tsmiTime.Click += new System.EventHandler(this.tsmiTime_Click); // // timer1 // this.timer1.Interval = 5; this.timer1.Tick += new System.EventHandler(this.timer1_Tick); // // timer2 // this.timer2.Interval = 1000; this.timer2.Tick += new System.EventHandler(this.timer2_Tick); // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(25, 252); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(53, 12); this.label1.TabIndex = 4; this.label1.Text = "所用时间"; // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(114, 252); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(11, 12); this.label2.TabIndex = 5; this.label2.Text = "0"; // // label3 // this.label3.AutoSize = true; this.label3.Location = new System.Drawing.Point(25, 282); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(41, 12); this.label3.TabIndex = 6; this.label3.Text = "正确数"; // // label4 // this.label4.AutoSize = true; this.label4.Location = new System.Drawing.Point(27, 321); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(41, 12); this.label4.TabIndex = 7; this.label4.Text = "错误数"; // // label5 // this.label5.AutoSize = true; this.label5.Location = new System.Drawing.Point(27, 354); this.label5.Name = "label5"; this.label5.Size = new System.Drawing.Size(41, 12); this.label5.TabIndex = 8; this.label5.Text = "正确率"; // // label6 // this.label6.AutoSize = true; this.label6.Location = new System.Drawing.Point(116, 282); this.label6.Name = "label6"; this.label6.Size = new System.Drawing.Size(11, 12); this.label6.TabIndex = 9; this.label6.Text = "0"; // // label7 // this.label7.AutoSize = true; this.label7.Location = new System.Drawing.Point(116, 320); this.label7.Name = "label7"; this.label7.Size = new System.Drawing.Size(11, 12); this.label7.TabIndex = 10; this.label7.Text = "0"; // // label8 // this.label8.AutoSize = true; this.label8.Location = new System.Drawing.Point(116, 354); this.label8.Name = "label8"; this.label8.Size = new System.Drawing.Size(41, 12); this.label8.TabIndex = 11; this.label8.Text = "label8"; this.label8.Visible = false; // // textBox1 // this.textBox1.BackColor = System.Drawing.SystemColors.Info; this.textBox1.Location = new System.Drawing.Point(60, 106); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(100, 21); this.textBox1.TabIndex = 12; this.textBox1.Visible = false; // // pictureBox1 // this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image"))); this.pictureBox1.InitialImage = null; this.pictureBox1.Location = new System.Drawing.Point(0, 28); this.pictureBox1.Name = "pictureBox1"; this.pictureBox1.Size = new System.Drawing.Size(539, 400); this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage; this.pictureBox1.TabIndex = 13; this.pictureBox1.TabStop = false; // // pictureBox2 // this.pictureBox2.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox2.Image"))); this.pictureBox2.Location = new System.Drawing.Point(0, 28); this.pictureBox2.Name = "pictureBox2"; this.pictureBox2.Size = new System.Drawing.Size(539, 400); this.pictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage; this.pictureBox2.TabIndex = 14; this.pictureBox2.TabStop = false; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(539, 429); this.Controls.Add(this.textBox1); this.Controls.Add(this.label8); this.Controls.Add(this.label7); this.Controls.Add(this.label6); this.Controls.Add(this.label5); this.Controls.Add(this.label4); this.Controls.Add(this.label3); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.Controls.Add(this.btnQuit); this.Controls.Add(this.btnStop); this.Controls.Add(this.btnStart); this.Controls.Add(this.menuStrip1); this.Controls.Add(this.pictureBox1); this.Controls.Add(this.pictureBox2); this.MainMenuStrip = this.menuStrip1; this.Name = "Form1"; this.Text = "Form1"; this.menuStrip1.ResumeLayout(false); this.menuStrip1.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.Button btnStart; private System.Windows.Forms.Button btnStop; private System.Windows.Forms.Button btnQuit; private System.Windows.Forms.MenuStrip menuStrip1; private System.Windows.Forms.ToolStripMenuItem 设置ToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem 查看ToolStripMenuItem; private System.Windows.Forms.Timer timer1; private System.Windows.Forms.Timer timer2; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label3; private System.Windows.Forms.Label label4; private System.Windows.Forms.Label label5; private System.Windows.Forms.Label label6; private System.Windows.Forms.Label label7; private System.Windows.Forms.Label label8; private System.Windows.Forms.ToolStripMenuItem tsmiRate; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.PictureBox pictureBox1; private System.Windows.Forms.ToolStripMenuItem tsmiTime; private System.Windows.Forms.ToolStripMenuItem tsmiStart; private System.Windows.Forms.ToolStripMenuItem tsmiStop; private System.Windows.Forms.ToolStripMenuItem tsmiQuit; private System.Windows.Forms.PictureBox pictureBox2; } }
更多有趣的经典小游戏实现专题,也分享给大家:
C++经典小游戏汇总
python经典小游戏汇总
python俄罗斯方块游戏集合
JavaScript经典游戏 玩不停
java经典小游戏汇总
javascript经典小游戏汇总
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持猪先飞。
上一篇: Unity Shader实现裁切效果
下一篇: C#实现微信分账功能的完整步骤
相关文章
- 我们在使用C#做项目的时候,基本上都需要制作登录界面,那么今天我们就来一步步看看,如果简单的实现登录界面呢,本文给出2个例子,由简入难,希望大家能够喜欢。...2020-06-25
- 这篇文章主要介绍了C# 字段和属性的的相关资料,文中示例代码非常详细,供大家参考和学习,感兴趣的朋友可以了解下...2020-11-03
- 这篇文章主要介绍了C#中截取字符串的的基本方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-11-03
- 本文给大家分享C#连接SQL数据库和查询数据功能的操作技巧,本文通过图文并茂的形式给大家介绍的非常详细,需要的朋友参考下吧...2021-05-17
- 这篇文章主要介绍了C#实现简单的Http请求的方法,以实例形式较为详细的分析了C#实现Http请求的具体方法,需要的朋友可以参考下...2020-06-25
- 本文主要介绍了C#中new的几种用法,具有很好的参考价值,下面跟着小编一起来看下吧...2020-06-25
使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序)
这篇文章主要介绍了使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25- 这篇文章主要介绍了C#开发Windows窗体应用程序的简单操作步骤,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-04-12
- 这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
- 最近做一个小项目不可避免的需要前端脚本与后台进行交互。由于是在asp.net中实现,故问题演化成asp.net中jiavascript与后台c#如何进行交互。...2020-06-25
- 这篇文章主要用实例讲解C#递归算法的概念以及用法,文中代码非常详细,帮助大家更好的参考和学习,感兴趣的朋友可以了解下...2020-06-25
- 本文通过例子,讲述了C++调用C#的DLL程序的方法,作出了以下总结,下面就让我们一起来学习吧。...2020-06-25
- 轻松学习C#的基础入门,了解C#最基本的知识点,C#是一种简洁的,类型安全的一种完全面向对象的开发语言,是Microsoft专门基于.NET Framework平台开发的而量身定做的高级程序设计语言,需要的朋友可以参考下...2020-06-25
- 本文主要介绍了C#变量命名规则小结,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-09
- 这篇文章主要介绍了c#中(&&,||)与(&,|)的区别详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-25
- 本文主要介绍了C# 中取绝对值的函数。具有很好的参考价值。下面跟着小编一起来看下吧...2020-06-25
- 这篇文章主要介绍了C#绘制曲线图的方法,以完整实例形式较为详细的分析了C#进行曲线绘制的具体步骤与相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
- 这篇文章主要介绍了c#自带缓存使用方法,包括获取数据缓存、设置数据缓存、移除指定数据缓存等方法,需要的朋友可以参考下...2020-06-25
- 下面小编就为大家带来一篇C#学习笔记- 随机函数Random()的用法详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
- 这篇文章主要介绍了C#中list用法,结合实例形式分析了C#中list排序、运算、转换等常见操作技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25