c#键盘单词输入判断游戏
2023-09-27 14:27:29 时间
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;
using System.IO;
namespace WindowsFormsApplication68
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int x = 200, y=20, num;
private DateTime dt1, dt2;
private int count = 0; //打字总数
private int yes = 0; //打字正确数
private TimeSpan ts;//存放时间的差值
/*
Seconds:返回用秒计算的TimeSpan值。
*/
Random rd = new Random();
private StreamReader sr = new StreamReader(@"text\myfile.txt", Encoding.GetEncoding("gb2312"));//把文件打包放在编写的项目里
string line;
string[] word;
private void Form1_Load(object sender, EventArgs e)
{
this.KeyPreview = true;
button1.Text = "开始";
button2.Text = "暂停";
button3.Text = "退出";
label1.Text = "正确数:";
label2.Text = "错误数:";
label3.Text = "正确率:";
label4.Text = "0";
label5.Text = "0";
label7.Text = "所用时间:";
label8.Text = "0";
label6.Visible = false;//正确率隐藏
label9.Visible = false;//开始显示单词的也隐藏
timer1.Enabled = false;//两个计时器都不运行
//timer1.Interval = 5;//间隔时间为5毫秒
timer1.Interval = 5;//间隔时间为5毫秒
timer2.Enabled = false;
timer2.Interval = 1000;
line = sr.ReadToEnd();//取出单词
word = line.Split(',');
开始游戏ToolStripMenuItem.Click += new EventHandler(button1_Click);
暂停游戏ToolStripMenuItem.Click += new EventHandler(button2_Click);
结束游戏ToolStripMenuItem.Click += new EventHandler(button3_Click);
}
#region 按钮事件
private void button1_Click(object sender, EventArgs e)//点击开始(1.记录下当前的时间,两个计时器开始运行 2.正确率开始统计)
{
textBox1.Focus();
dt1 = DateTime.Now;
timer1.Start();
timer2.Start();
num = rd.Next(0, word.Length);
label9.Text = word[num];//设置下一个标签的值
label9.Visible = true;
}
private void button2_Click(object sender, EventArgs e)//游戏暂停(两个计时器停止计时,正确率隐藏,记录下当前的时间)
{
dt2 = DateTime.Now;
timer1.Stop();
timer2.Stop();
label9.Visible = false;
MessageBox.Show("游戏暂停!", "提示");
}
private void button3_Click(object sender, EventArgs e)//退出游戏(计时器停止,单词标签不显示)
{
timer1.Stop();
///
timer2.Stop();
label9.Visible = false;
DialogResult dr = MessageBox.Show("确定要退出吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
if (dr == DialogResult.OK)
Application.Exit();
}
#endregion
#region 时间控件
private void timer1_Tick(object sender, EventArgs e)//第一个计时器触发事件
{
y++;
if (y > this.ClientSize.Height - 5)//定义随机产生的标签位置不能移出控件,若移出,显示在20的位置上
y = 20;//显示在开始定义的位置
label9.Text = word[num];
label9.Location = new Point(x, y);//设置新标签新的位置和颜色,只有y在不断的增加
label9.ForeColor = Color.FromArgb(rd.Next(0, 255), rd.Next(0, 255), rd.Next(0, 255));
// label9.ForeColor = Color.FromArgb(0,0,0);//黑色
}
private void timer2_Tick(object sender, EventArgs e)//改变时间
{
label8.Text = (DateTime.Now - dt1).Seconds.ToString();//显示所用的时间,现在时间-开始时间转换成字符形式
}
#endregion
#region 标签显示
private void 正确率ToolStripMenuItem_Click(object sender, EventArgs e)
{
double truerate = yes * 1.0 / count;
string s = string.Format("{0,5:P2}", truerate);
MessageBox.Show("正确率为:" + s, "正确率");
}
private void 所用时间ToolStripMenuItem_Click(object sender, EventArgs e)
{
ts = dt2 - dt1;
MessageBox.Show("所用时间为:" + ts.Seconds + "(s)", "所用时间");
}
#endregion
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
count++;//记录的总数
if (textBox1.Text == label9.Text)//输入正确时
{
yes++;
label9.Visible = false;//关闭单词标签
num = rd.Next(0, word.Length);//产生随机数
label9.Visible = true;//打开单词标签
label9.Text = word[num];//
x = rd.Next(20, 490);//判断是否出界的时候就看所判断的标签大小
y = 20;
label9.Location = new Point(x, y);//设置新的位置,也是随机的
}
label6.Visible = true;//开始时间和正确率都显示
label8.Visible = true;
label4.Text = yes.ToString();//显示正确数和错误数
label5.Text = (count - yes).ToString();
string t = string.Format("{0,5:P2}", yes * 1.0 / count);
label6.Text = t.ToString();//计算正确率转换成百分号
textBox1.Text = "";
}
}
}
}
工具栏上设置了快捷键
F1-开始
F2-暂停
F3-结束
图片:
相关文章
- c#课程设计---猜猜看游戏
- Expression Trees (C# and Visual Basic)
- C#中的异常处理
- C#中dll附加配置文件
- 【UWA学堂专栏推荐】C#与Lua混用,如何消除对游戏性能的影响
- C# 动态创建SQL数据库(二) 在.net core web项目中生成二维码 后台Post/Get 请求接口 方式 WebForm 页面ajax 请求后台页面 方法 实现输入框小数多 自动进位展示,编辑时实际值不变 快速掌握Gif动态图实现代码 C#处理和对接HTTP接口请求
- 【微信小程序项目实践总结】30分钟从陌生到熟悉 web app 、native app、hybrid app比较 30分钟ES6从陌生到熟悉 【原创】浅谈内存泄露 HTML5 五子棋 - JS/Canvas 游戏 meta 详解,html5 meta 标签日常设置 C#中回滚TransactionScope的使用方法和原理
- 【C#】var、对应具体类型、object性能比较
- C# 坦克游戏大战中学习相关类(Closing与Closed区别)
- 免费分享《精通C#游戏编程》
- 使用C#和MonoGame开发俄罗斯方块游戏
- 基于C#开发(图形界面)五子棋游戏【100010615】
- C#把json对象序列(List<T>)转化为字符串时去掉值为null的字段
- C# GroupBy的使用
- 《C#零基础入门之百识百例》(四十)方法应用 -- 推箱子游戏 -- 代码分析
- 《C#零基础入门之百识百例》(五十八)接口 -- 模拟银行存储
- C#程序之Main()方法
- C# SocketHelper 源码
- 在C#中开启事务
- C# winform 读取数据库并显示成datagrid
- 接口 C# 请求数据 方式1
- C# WCF简单使用
- C# 线程