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(); } } }
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
网页标题:C#如何实现简单打字游戏-创新互联
文章地址:http://azwzsj.com/article/igcjo.html