logo资料库

C#打字游戏的实现代码.docx

第1页 / 共6页
第2页 / 共6页
第3页 / 共6页
第4页 / 共6页
第5页 / 共6页
第6页 / 共6页
资料共6页,全文预览结束
界面是通过拖拽设计的,苹果的图片还是自己找吧,我当时也找了好久的 C#实现打字游戏 控制的代码如下: 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.Media; namespace 打字游戏 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } int right = 0; int wrong = 0; string code1 = "a"; string code2 = "b"; string code3 = "w";
int number = 0; int time = 0; private void Form1_KeyPress(object sender, KeyPressEventArgs e) { int x = 0, x1 = 0, x2 = 0; int y = 28, y1 = 28, y2 = 28; if (number == 0) { x = new Random().Next(0, 409); x1 = new Random().Next(528, this.ClientRectangle.Width - 230); } if (number == 1) { x = new Random().Next(0, 200); x1 = new Random().Next(327, 527); x2 = new Random().Next(654, 893); } //判断下降的速度高低 string receive1 = e.KeyChar.ToString(); if (receive1.Equals(code1)) { right++; //显示正确的个数 this.LabelScore1.Text = "当前正确数: this.panel1.Location = new Point(x, y); BOMB(); this.label1.Text = GetCode1().ToString(); //令高度从新开始 } else if (receive1.Equals(code2)) { right++; //显示正确的个数 this.LabelScore1.Text = "当前正确数: this.panel2.Location = new Point(x1, y1); BOMB(); this.label2.Text = GetCode2().ToString(); //令高度从新开始 " + right.ToString(); " + right.ToString();
} else if (number == 1) { if (receive1.Equals(code3)) { right++; //显示正确的个数 this.LabelScore1.Text = "当前正确数: this.panel3.Location = new Point(x2, y2); BOMB(); this.label3.Text = GetCode3().ToString(); //令高度从新开始 " + right.ToString(); } } else { wrong++; this.LabelLost1.Text = " " + " 当 前 错 误 数 : " + wrong.ToString(); LOSS(); } } private void BOMB() { SoundPlayer player = new SoundPlayer("1.wav"); player.Play(); } private void LOSS() { SoundPlayer player = new SoundPlayer("2.WAV"); player.Play(); } public string GetCode1() { Random rd = new Random(); code1 = Convert.ToChar(65 + rd.Next(25)).ToString().ToLower(); return code1; } //随机数处理 public string GetCode2()
{ //产生随机数 Random rd = new Random(); code2 = Convert.ToChar(65 + rd.Next(25)).ToString().ToLower(); return code2; } public string GetCode3() { //产生随机数 Random rd = new Random(); code3 = Convert.ToChar(65 + rd.Next(25)).ToString().ToLower(); return code3; } private void 开始游戏 ToolStripMenuItem_Click(object sender, EventArgs e) { //运行计时器控件 this.timer1.Enabled = true; this.LabelScore1.Text = ""; this.LabelLost1.Text = ""; this.timer2.Enabled = true; right = 0; wrong = 0; } private void timer1_Tick(object sender, EventArgs e) { while (this.panel1.Top >= this.Height) { this.panel1.Top = 3; } this.panel1.Top += 5; //循环判断字符走过的高度是否大于当前页面的高度, while (this.panel2.Top >= this.Height) { //如果大于当前高度,则令他的高度为 3 this.panel2.Top = 3; } this.panel2.Top += 5; while (this.panel3.Top >= this.Height) { this.panel3.Top = 3;
} this.panel3.Top += 5; } private void 简单 ToolStripMenuItem_Click(object sender, EventArgs e) { this.timer1.Interval = 200; number = 0; panel3.Visible = false; } private void 中等 ToolStripMenuItem_Click(object sender, EventArgs e) { this.timer1.Interval = 100; panel3.Visible = true; number = 1; } private void 复杂 ToolStripMenuItem_Click(object sender, EventArgs e) { this.timer1.Interval = 20; panel3.Visible = true; number = 1; } private void 暂停游戏 ToolStripMenuItem_Click(object sender, EventArgs e) { this.timer1.Stop(); } private void 退出 ToolStripMenuItem1_Click(object sender, EventArgs e) { this.Close(); } private void 结束 ToolStripMenuItem_Click(object sender, EventArgs e) { this.timer1.Stop(); int speed = right + wrong; string s1 = "时间:" + time / 60 + "分" + time % 60 + "秒"; double x = (right + wrong) * 60 / time; string s2 = "速度:" + x + "字/分";
double z = (double)right * 100 / (double)(right + wrong); string ss = string.Format("{0:f2}", z); string s3 = "正确率:" + ss + "%"; MessageBox.Show(s1 "\n" s2 + + + "\n" + s3," 评 测 ",MessageBoxButtons.OK,MessageBoxIcon.Information); } private void timer2_Tick(object sender, EventArgs e) { time = time + 1; } private void 帮助 ToolStripMenuItem_Click(object sender, EventArgs e) { MessageBox.Show("在“游戏设置”里,选择模式,简单,中等和复杂\n 在“游 戏”菜单下选择开始,苹果开始下落,计时开始,键盘按下苹果上的字母,苹果消失\n 在 “ 游 戏 ” 下 单 击 “ 结 束 ” , 会 对 你 的 水 平 进 行 评 测 "," 帮 助 ",MessageBoxButtons.OK,MessageBoxIcon.Question); } } }
分享到:
收藏