/*
* 利用Random类,结合Timer控件,设计窗体。在窗体上的文本框中显示一个大写字符,如果用户输入正确,则产生另一个大写字符。
* 实现打字游戏。(提示:金山打字通的简易版)窗体中的按钮可以用菜单实现。
* 菜单命令组成:
* (1)“设置”菜单:开始游戏、结束游戏和退出游戏。
* (2)“查看”菜单:查看正确率和打字所用的时间。
* 提示与思考:
* (1)判断输入字符正确与否,采用哪个控件的哪个事件;
* (2)首先考虑如何实现一个静止的字符(不移动)的输入判断;然后考虑如何让字符动起来;
* (3)如果字符移出窗体边界,仍然没有输入正确,如何让该字符重新出现在窗体的顶端某处。
*/
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 WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int x=200,y,num;
private DateTime dt1,dt2;
private int count=0; //打字总数
private int count1=0; //打字正确数
private TimeSpan ts;
Random rd = new Random();
private void Form1_Load(object sender, EventArgs e)
{
正确率ToolStripMenuItem.Enabled = false;
所用时间ToolStripMenuItem.Enabled = false;
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;
textBox1.Visible = false;
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.Image = Image.FromFile(Application.StartupPath + @"\g.png");
timer1.Enabled = false;
timer1.Interval = 5;
timer2.Enabled = false;
timer2.Interval = 1000;
}
private void button1_Click(object sender, EventArgs e) //开始按钮
{
正确率ToolStripMenuItem.Enabled = true;
dt1 = DateTime.Now;
timer1.Start();
timer2.Start();
textBox1.Visible = true;
num = rd.Next(65, 90);
}
private void button2_Click(object sender, EventArgs e) //结束按钮
{
所用时间ToolStripMenuItem.Enabled = true;
dt2 = DateTime.Now;
timer1.Stop();
timer2.Stop();
textBox1.Visible = false;
MessageBox.Show("游戏结束!", "提示");
}
private void button3_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 开始游戏ToolStripMenuItem_Click(object sender, EventArgs e)
{
dt1 = DateTime.Now;
timer1.Start();
timer2.Start();
textBox1.Visible = true;
num = rd.Next(65, 90);
}
private void 结束游戏ToolStripMenuItem_Click(object sender, EventArgs e)
{
dt2 = DateTime.Now;
timer1.Stop();
timer2.Stop();
textBox1.Visible = false;
MessageBox.Show("游戏结束!", "提示");
}
private void 退出游戏ToolStripMenuItem_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 正确率ToolStripMenuItem_Click(object sender, EventArgs e)
{
double truerate = count1 * 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)", "所用时间");
}
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)
{
label8.Text = (DateTime.Now - dt1).Seconds.ToString();
}
private void button1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode.ToString() == textBox1.Text || e.KeyCode.ToString() != textBox1.Text)
{
count++;
while (e.KeyCode.ToString() == textBox1.Text)
{
count1++;
textBox1.Visible = false;
textBox1.Clear();
num = rd.Next(65, 90);
textBox1.Visible = true;
textBox1.Text = ((char)num).ToString();
x = rd.Next(20, 600);
y = rd.Next(20, 300);
textBox1.Location = new Point(x, y);
}
}
label6.Visible = true;
label8.Visible = true;
label4.Text = count1.ToString();
label5.Text = (count - count1).ToString();
string t = string.Format("{0,5:P2}", count1 * 1.0 / count);
label6.Text = t.ToString();
}
}
}
C#—打字游戏
最新推荐文章于 2024-07-04 12:10:18 发布