C#如何实现简单打字游戏
这篇文章主要为大家展示了C#如何实现简单打字游戏,内容简而易懂,希望大家可以学习一下,学习完之后肯定会有收获的,下面让小编带大家一起来看看吧。
创新互联专业为企业提供乐山网站建设、乐山做网站、乐山网站设计、乐山网站制作等企业网站建设、网页设计与制作、乐山企业网站模板建站服务,10年乐山做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
运行效果图如下:
功能:程序运行后,点击开始按钮,窗体中的文本框中出现字母,用户通过键盘输入文本框中字母,窗体显示用时、正确数、错误数和正确率。
按钮:开始、结束、退出。
菜单:设置(开始游戏、结束游戏、退出游戏),查看(正确率、所用时间)。
页面:
控件属性:
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 { ////// 必需的设计器变量。 /// private System.ComponentModel.IContainer components = null; ////// 清理所有正在使用的资源。 /// /// 如果应释放托管资源,为 true;否则为 false。 protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 ////// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// 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#如何实现简单打字游戏的内容,如果你们有学习到知识或者技能,可以把它分享出去让更多的人看到。
当前文章:C#如何实现简单打字游戏
网页链接:http://ybzwz.com/article/jpcigc.html