实例介绍
【实例简介】通过对数值的计算、比较、转换制作出的猜数小游戏
【实例截图】
【核心代码】
/* **************************************************************************************** * 关于这个软件 * * 制作/更改时间: 2020年7月23日18:48:22 * * 作者:gfdgd xi * * 程序编辑/调试平台(最近一次):Visual Studio 2019 Professional 以及 Windows 8.1 * * 程序目的:通过对数值的计算、比较、转换制作出的猜数小游戏 * **************************************************************************************** */ // 导入头文件 // using System.Collections.Generic; // using System.ComponentModel; // using System.Data; // using System.Drawing; // using System.Linq; // using System.Text; // 因为这些注释提示不需要,于是注释掉 using System.IO; using System; using System.Windows.Forms; namespace Guess_number { public partial class Form1 : Form { public Form1() { InitializeComponent(); skinEngine1.SkinFile = "Theme\\Tacitly approve theme\\theme.ssk"; // 打开默认的主题文件 Text = File.ReadAllText("About\\Software title.txt"); // 读取文本文档内的内容更改标题 } int trytime = 0, scopyleast = 0, scopymore = 0, guessnumber = 0; // 创建变量以保存游戏设置 private void exitThisSoftwareToolStripMenuItem_Click(object sender, EventArgs e) { Close(); } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if(MessageBox.Show("你确定要退出程序吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) // 弹出对话框,提示是否退出 { e.Cancel = true; // 不要关闭软件 } } private void minimumThisSoftwareToolStripMenuItem_Click(object sender, EventArgs e) { WindowState = FormWindowState.Minimized; } private void aboutThisSoftwareToolStripMenuItem_Click(object sender, EventArgs e) { MessageBox.Show(File.ReadAllText("About\\About this software.txt"), File.ReadAllText("About\\About this software title.txt"), MessageBoxButtons.OK, MessageBoxIcon.Information ); } private void changeOtherThemeToolStripMenuItem_Click(object sender, EventArgs e) { if(openFileDialog1.ShowDialog() == DialogResult.OK) // 如果用户选择了文件并点击了“确定” { skinEngine1.SkinFile = openFileDialog1.FileName; // 将主题更改为用户选定的主题 } } private void button2_Click(object sender, EventArgs e) { try { /* 将文本框内的文本转为数字 */ int playpersonguessnumber = Convert.ToInt32(secondpersonguessnumber.Text); // 如果数字有误,就抛出错误 if(playpersonguessnumber == guessnumber) // 如果用户输入的数字与玩家预先设置的数字相同 { // 提示你赢了 gametips.Text = "游戏提示: 你赢了!你猜测的数字是对的!"; MessageBox.Show("游戏提示: 你赢了!你猜测的数字是对的!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); // 更改控件 secondperson.Enabled = false; firstperson.Visible = true; } else // 如果没赢 { if (playpersonguessnumber > guessnumber) { // 提示数字大了 gametips.Text = "游戏提示: 你猜的数大了!"; MessageBox.Show("游戏提示: 你猜的数大了!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } if (playpersonguessnumber < guessnumber) { // 提示数字小了 gametips.Text = "游戏提示: 你猜的数小了!"; MessageBox.Show("游戏提示: 你猜的数小了!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } trytime--; secondpersontrytime.Text = "你还有" trytime "次机会可以尝试"; secondpersongametips.Text = "游戏开始!\n数字范围:" scopyleast "到" scopymore; // change label text if (trytime == 0) // 如果一次机会都没有 { // 提示游戏结束 gametips.Text = "游戏提示: 游戏失败!"; MessageBox.Show("游戏提示: 游戏失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); // 更改控件 secondperson.Enabled = false; firstperson.Visible = true; } } } catch // 如果数字有误 { // 提示输入的错误有误 gametips.Text = "游戏提示:你在文本框输入的数字有误! \n请检查文本框内数字是否正确"; MessageBox.Show("游戏提示:你在文本框输入的数字有误!请检查文本框内数字是否正确", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error ); } } private void button1_Click(object sender, EventArgs e) { try { /* 将文本框的文本改为数字 */ trytime = Convert.ToInt32(firstpersontrytimes.Text); scopyleast = Convert.ToInt32(firstpersonscopeleast.Text); scopymore = Convert.ToInt32(firstpersonmore.Text); guessnumber = Convert.ToInt32(firstpersonguessnumber.Text); if(trytime <= 0) // 如果次数设置不合理 { Convert.ToInt32("Wrong!"); // 故意抛出错误,毕竟“wrong!”怎么改为数字呢? } /* 如果没有错误,游戏开始! */ firstperson.Visible = false; // 隐藏用户设置控件 secondperson.Enabled = true; // 显示用户玩耍控件 secondpersongametips.Text = "游戏开始!\n数字范围:" scopyleast "到" scopymore; // 更改标签文本 secondpersontrytime.Text = "你有" trytime "次机会可以尝试"; } catch // 如果文本输入有误 { // 提示文本 settingtips.Text = "设置错误: 你文本框输入的数字有误! \n请检查文本框内输入的数字是否正确!"; MessageBox.Show("设置错误: 你文本框输入的数字有误!请检查文本框内输入的数字是否正确!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error ); } } } }
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论