基本信息
源码名称:C# 数值的计算、比较、换算实例(入门级)
源码大小:3.80M
文件格式:.zip
开发语言:C#
更新时间:2020-07-23
   源码介绍
通过对数值的计算、比较、转换制作出的猜数小游戏


/* 
 ****************************************************************************************
 * 关于这个软件                                                                         *
 *     制作/更改时间: 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
                    );
            }
        }
    }
}