C#如何實現(xiàn)簡單打字游戲-創(chuàng)新互聯(lián)

這篇文章主要為大家展示了C#如何實現(xiàn)簡單打字游戲,內(nèi)容簡而易懂,希望大家可以學(xué)習(xí)一下,學(xué)習(xí)完之后肯定會有收獲的,下面讓小編帶大家一起來看看吧。

創(chuàng)新互聯(lián)公司擁有一支富有激情的企業(yè)網(wǎng)站制作團隊,在互聯(lián)網(wǎng)網(wǎng)站建設(shè)行業(yè)深耕十年,專業(yè)且經(jīng)驗豐富。十年網(wǎng)站優(yōu)化營銷經(jīng)驗,我們已為上千余家中小企業(yè)提供了做網(wǎng)站、網(wǎng)站制作解決方案,定制網(wǎng)站制作,設(shè)計滿意,售后服務(wù)無憂。所有客戶皆提供一年免費網(wǎng)站維護!

運行效果圖如下:

C#如何實現(xiàn)簡單打字游戲

功能:程序運行后,點擊開始按鈕,窗體中的文本框中出現(xiàn)字母,用戶通過鍵盤輸入文本框中字母,窗體顯示用時、正確數(shù)、錯誤數(shù)和正確率。
按鈕:開始、結(jié)束、退出。

菜單:設(shè)置(開始游戲、結(jié)束游戲、退出游戲),查看(正確率、所用時間)。

頁面:

C#如何實現(xiàn)簡單打字游戲

控件屬性:

timer1:

enabled選擇false,Interval設(shè)置為5.

timer2:

enabled選擇false,Interval設(shè)置為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("游戲結(jié)束。", "提示");
  }
 
  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("游戲結(jié)束!", "提示"); 
  }
 
  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(); 
  }
 }
}

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

分享題目:C#如何實現(xiàn)簡單打字游戲-創(chuàng)新互聯(lián)
分享路徑:http://bm7419.com/article20/igcjo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、企業(yè)建站商城網(wǎng)站、Google、定制網(wǎng)站、網(wǎng)站策劃

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化