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;
  }
}

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

(0)

相关推荐

  • C#实现打字游戏

    本文实例为大家分享了C#实现打字游戏的具体代码,供大家参考,具体内容如下 思路: 1.有一个游戏界面,我用panel作为游戏界面 2.开始生成字母 打字游戏的字母是不断生成的,所以用计时器timer来生成字母 所有生成的字母设置tag方便寻找 3.字母下落 字母下落是一个持续的动作,所以也在计时器里做 在计时器里通过foreach遍历panel中的所有控件,同时通过tag找到字母,让字母下降 4.生成子弹 通过获取键盘事件生成子弹 5.子弹与字母相碰 代码: private void Form1

  • C#实现简单打字游戏

    本文实例为大家分享了C#实现简单打字游戏的具体代码,供大家参考,具体内容如下 运行效果图如下: 功能:程序运行后,点击开始按钮,窗体中的文本框中出现字母,用户通过键盘输入文本框中字母,窗体显示用时.正确数.错误数和正确率. 按钮:开始.结束.退出. 菜单:设置(开始游戏.结束游戏.退出游戏),查看(正确率.所用时间). 页面: 控件属性: timer1: enabled选择false,Interval设置为5. timer2: enabled选择false,Interval设置为1000. 代码

  • jQuery 写的简单打字游戏可以提示正确和错误的次数

    var off_x; //横坐标 var count=0; //总分 var speed=5000; //速度,默认是5秒. var keyErro=0; //输入错误次数 var keyRight=0; //输入正确的次数 //组织字母 var charArray=new Array("A", "B", "C", "D", "E", "F", "G", "

  • javascript实现简单打字游戏

    本文实例为大家分享了javascript打字游戏的具体代码,供大家参考,具体内容如下 <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>传智打字游戏</title> <style type="text/css"> .label{ position:a

  • C#实现简单打字小游戏

    本文实例为大家分享了C#实现简单打字小游戏的具体代码,供大家参考,具体内容如下 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.For

  • javascript 打字游戏实现代码

    效果如图所示:下面是核心代码 复制代码 代码如下: GAME = { //随机产生字母 randLetter: function() { var arrLetter = new Array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K&qu

  • JS实现简单打字测试

    本文实例为大家分享了JS实现简单打字测试的具体代码,供大家参考,具体内容如下 需求:实现以下的功能 1.有三个小方块,分别用来当前输入的错误数量.打字的时间和当前的正确率. 2.下方是用来显示测试句子的容器. 3.最后是输入框 具体思路: 点击输入文本区域时,开始测试,会根据用户输入来统计当前的错误数和正确率,时间会减少.当输完整段句子时,会自动更新下一段句子.当时间为0时,游戏结束,文本框不能再输入,然后会统计打字速度. 具体代码如下: Html部分 <!DOCTYPE html> <

  • java实现打字游戏小程序

    本文实例为大家分享了java实现打字游戏小程序的具体代码,供大家参考,具体内容如下 一.设计思路 1.创建一个窗体 2.在窗体上放置一个面板,用paint方法画出英文字母,随机放置字母位置,并随时间自动下落 3.设置得分扣分机制 4.在面板上添加键盘监听器,输入正确得分,输入错误则扣分 ps:面板和监听器用一个类实现(这样简单方便) 二.具体实现 MyPanel.java import java.awt.Color; import java.awt.Font; import java.awt.G

  • python实现的简单RPG游戏流程实例

    本文实例讲述了python实现的简单RPG游戏流程.分享给大家供大家参考.具体如下: #RPG rpg = True whp = 100 mahp = 100 hhp = 100 MHP = 10 def dgrnd () : wa = raw_input ("What does Warrior do?") ma = raw_input ("What does Mage do?") ha = raw_input ("What does Healer do?

  • Python加pyGame实现的简单拼图游戏实例

    本文实例讲述了Python加pyGame实现的简单拼图游戏.分享给大家供大家参考.具体实现方法如下: import pygame, sys, random from pygame.locals import * # 一些常量 WINDOWWIDTH = 500 WINDOWHEIGHT = 500 BACKGROUNDCOLOR = (255, 255, 255) BLUE = (0, 0, 255) BLACK = (0, 0, 0) FPS = 40 VHNUMS = 3 CELLNUMS

  • Python实现的简单算术游戏实例

    本文实例讲述了Python实现的简单算术游戏.分享给大家供大家参考.具体实现方法如下: #!/usr/bin/env python from operator import add, sub from random import randint, choice ops = {'+': add, '-':sub} #定义一个字典 MAXTRIES = 2 def doprob(): op = choice('+-') #用choice从'+-'中随意选择操作符 nums = [randint(1,

随机推荐