C#打字游戏课程设计.doc_第1页
C#打字游戏课程设计.doc_第2页
C#打字游戏课程设计.doc_第3页
C#打字游戏课程设计.doc_第4页
C#打字游戏课程设计.doc_第5页
已阅读5页,还剩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;/课程名称 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消失 else con.Top += 5; /对打字游戏开始时 不断随机生成的字母添加到新生成的label中 并设置label的基本属性 private void timerWord_Tick(object sender, EventArgs e) Random r = new Random(); int a = r.Next(65, 90); /随机生成AZ的ASCII码 char c = Convert.ToChar(a); /ASCII转换成字母 Label lalChar = new Label(); /生成新Label lalChar.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; else second -= 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 System.ComponentModel;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显示提示错误的输入。 private void textBox1_TextChanged(object sender, EventArgs e) lalPS.Text = ; btnOK.Enabled = true; try int m = Convert.ToInt32(textBox1.Text); if (m 60) lalPS.Text = 请输入 160 的整数; btnOK.Enabled = false; catch(Exception ex) lalPS.Text = 请输入 160 的整数; btnOK.Enabled = false; /“确定”后 数据返回到主窗体 从而改变每局时间 private void btnOK_Click(object sender, EventArgs e) frm.Minute = int.Parse(textBox1.Text); this.Close(); /“取消” private void BtnCancel_Click(object sender, EventArgs e) this.Close(); 成绩窗体代码: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 Typing_Game public partial class frmResult : Form FrmTyping frm; public frmResult(FrmTyping frm) InitializeComponent(); this.frm = frm; /按下确定键后 原有面板上的显示数据全部清空 private void button2_Click(object sender, EventArgs e) foreach (Control con in frm.Controls) if (con is Label) con.Visible = false; this.Close(); frm.right = 0; frm.sum = 0; frm.wrong = 0; frm.accuracy = 0; frm.tsslAccuracy.Text = ; frm.tsslRightNo.Text = ; frm.tsslWrongNo.Text = ; frm.tsslGameTime.Text = ; frm.tsslSum.

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论