欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#实现简单打字游戏

程序员文章站 2022-11-19 18:18:18
本文实例为大家分享了c#实现简单打字游戏的具体代码,供大家参考,具体内容如下运行效果图如下:功能:程序运行后,点击开始按钮,窗体中的文本框中出现字母,用户通过键盘输入文本框中字母,窗体显示用时、正确数...

本文实例为大家分享了c#实现简单打字游戏的具体代码,供大家参考,具体内容如下

运行效果图如下:

C#实现简单打字游戏

功能:程序运行后,点击开始按钮,窗体中的文本框中出现字母,用户通过键盘输入文本框中字母,窗体显示用时、正确数、错误数和正确率。
按钮:开始、结束、退出。

菜单:设置(开始游戏、结束游戏、退出游戏),查看(正确率、所用时间)。

页面:

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经典小游戏汇总

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

相关标签: C# 打字