当前位置:文档之家› C#打字游戏课程设计

C#打字游戏课程设计

C# 打字游戏程序核心代码主要部分:设计程序流程图:主窗体代码:using System;using System.Collections.Generic;using ponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;//课程名称 C# 程序设计实践//设计题目打字游戏//专业计算机科学与技术//班级//学号//姓名//指导老师namespace Typing_Game{public partial class FrmTyping : Form {public FrmTyping(){InitializeComponent();public int right = 0; //正确个数计数public int wrong = 0; //错误个数计数public int sum = 0; //总个数技术public int count = 0; //按键总次数public float accuracy; //正确率public int Minute = 1; //设置默认没局游戏时间public int minute; //设置游戏中的倒计时分public int second; //设置游戏中的倒计时秒private void FrmTyping_Load(object sender, EventArgs e){}//在状态栏中显示当前系统时间private void timerSys_Tick(object sender, EventArgs e){DateTime now = DateTime.Now;this.tsslSysTime.Text = "系统时间: " + now.Hour.ToString() + ":" + now.Minute.ToString() + ":" + now.Second.ToString()+'\t';}//开始&结束按钮private void tsmiStratOrOver_Click(object sender, EventArgs e){if (tsmiStratOrOver.Text == "开始"){tsmiStratOrOver.Text = "结束";timerWord.Start();timerGame.Start();timerGT.Start();//在游戏开始时暂停&继续按钮可用tsmiPauseOrContinue.Enabled = true;//在游戏未开始时选择游戏等级与设置游戏按钮无效tsmiLevel.Enabled = false;tsmiOption.Enabled = false;//游戏开始初始化游戏数据right = 0;wrong = 0;sum = 0;count = 0;minute = Minute;second = 0;}else{tsmiStratOrOver.Text = "开始";//在游戏未开始时暂停&继续按钮无效tsmiPauseOrContinue.Enabled = false;//在游戏未开始时可以选择游戏等级与设置游戏tsmiLevel.Enabled = true;tsmiOption.Enabled = true;frmResult frm = new frmResult(this);frm.Show();}}//游戏暂停&继续功能实现private void tsmiPauseOrContinue_Click(object sender, EventArgs e) {if (tsmiPauseOrContinue.Text == "暂停"){tsmiPauseOrContinue.Text = "继续";timerWord.Enabled = false;timerGame.Enabled = false;timerGT.Enabled = false;}else{tsmiPauseOrContinue.Text = "暂停";timerWord.Enabled = true;timerGame.Enabled = true;timerGT.Enabled = true;}}//退出程序private void tsmiExit_Click(object sender, EventArgs e){Application.Exit();}//游戏等级为初级时private void tsmiLevelPrimary_Click(object sender, EventArgs e) {tsmiLevelPrimary.Checked = true;tsmiLevelMidder.Checked = false;tsmiLevelHigh.Checked = false;timerGame.Interval = 180;timerWord.Interval = 1800;}//游戏等级为中级时private void tsmiLevelMidder_Click(object sender, EventArgs e) {tsmiLevelPrimary.Checked = false;tsmiLevelMidder.Checked = true;tsmiLevelHigh.Checked = false;timerGame.Interval = 90;timerWord.Interval = 900;}//游戏等级为高级时private void tsmiLevelHigh_Click(object sender, EventArgs e){tsmiLevelPrimary.Checked = false;tsmiLevelMidder.Checked = false;tsmiLevelHigh.Checked = true;timerGame.Interval = 50;timerWord.Interval = 500;}//跳出设置每局时间窗体private void tsmiSetTime_Click(object sender, EventArgs e){frmSetTime frm = new frmSetTime(this);frm.Show();}//跳出关于窗体private void tsmiAbout_Click(object sender, EventArgs e){About frm = new About();frm.Show();}//对打字游戏中字母的掉落private void timerGame_Tick(object sender, EventArgs e){foreach (Control con in this.Controls){if (con is Label){if (con.Top >= this.Height-51){con.Dispose(); //当label掉落到窗体边框 label消失}elsecon.Top += 5;}}}//对打字游戏开始时不断随机生成的字母添加到新生成的label中并设置label的基本属性private void timerWord_Tick(object sender, EventArgs e){Random r = new Random();int a = r.Next(65, 90); //随机生成A~Z的ASCII码char c = Convert.ToChar(a); //ASCII转换成字母Label lalChar = new Label(); //生成新LabellalChar.Text = c.ToString(); //字母放入//设置Label基本属性固定大小字体居中对齐等等...lalChar.AutoSize = false;lalChar.Height = 51;lalChar.Width = 51;lalChar.Left = r.Next(r.Next( 0, this.Width/5-25),r.Next(this.Width-100,this.Width - 51));lalChar.Font = new Font("楷体", 15, FontStyle.Regular);lalChar.TextAlign = ContentAlignment.MiddleCenter;//将Label加入到Controls中this.Controls.Add(lalChar);//每生成一个Label 计入字母总数中sum++;//每生成一个Label都重新计算一次正确率accuracy = (float)right / (2 * sum) + (float)right / (2 * count);//状态栏中实时更新数据统计tsslSum.Text = "总个数为 :" + sum.ToString();tsslAccuracy.Text = "正确率为: " + (accuracy * 100).ToString() + "%";}//状态栏中的倒计时算法private void timerGT_Tick(object sender, EventArgs e){if (minute == 0 && second == 0){frmResult frm = new frmResult(this);frm.Show();}else if (second == 0){minute -= 1;second = 59;}elsesecond -= 1;//实时更新倒计时数据tsslGameTime.Text = "游戏剩余时间: "+ minute.ToString() + ":"+ second.ToString(); }//键盘输入对应的正确或错误的处理private void FrmTyping_KeyPress(object sender, KeyPressEventArgs e){//每按下一个按键加入输入总数的计数count++;//bool t的值来判定输入的对错bool t = false;string s = e.KeyChar.ToString().ToUpper();foreach (Control con in this.Controls){if (con is Label){if (con.Text==s){t = true;right++;con.Dispose();break;}}}if (t == false)wrong++;//实时更新状态栏的数据tsslRightNo.Text = "正确个数: " + right.ToString();tsslWrongNo.Text = "错误个数: " + wrong.ToString();accuracy = (float)right / (2 * sum) + (float)right / (2 * count);tsslAccuracy.Text = "正确率为: " + (accuracy * 100).ToString() + "%";}}}时间设置窗体代码:using System;using System.Collections.Generic;using ponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace Typing_Game{public partial class frmSetTime : Form{FrmTyping frm;public frmSetTime(FrmTyping frm){InitializeComponent();this.frm = frm;}//当窗体跳出时 textbox默认显示当前游戏的每局时间private void frmSetTime_Load(object sender, EventArgs e){textBox1.Text = frm.Minute.ToString();}//当textbox中内容发生变化时,对textbox中的内容做出判断如果不符合要求 label显示提示错误的输入。

相关主题