您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了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#如何實現簡單打字游戲的內容,如果你們有學習到知識或者技能,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。