ToB企服应用市场:ToB评测及商务社交产业平台

标题: 菜单 [打印本页]

作者: 怀念夏天    时间: 2025-1-5 11:32
标题: 菜单
准备写一个基于C的扫雷游戏,这是第一篇,内容:扫雷的主菜单
思绪

表现菜单->用户选择->判断用户选项
实现

表现菜单

首先在main函数内表现菜单,菜单表现部分实现在 MainMenu 函数内
  1. int main(void)
  2. {
  3.         while (true)
  4.         {
  5.                 int iChoose = -1;
  6.                 MainMenu();//加载菜单
  7.         }
  8. }
复制代码
菜单实现

菜单提供给用户三个选项:
1、开始游戏
2、游戏帮助
3、退出
此处,设定用户选择 1 是开始游戏,用户选择 2 是游戏帮助,用户选择 0 是退出游戏,具体实现如下:

用户选择

表现完菜单之后喊用户输入选项,并将用户的选择保存在 iChoose 中,方便后续进行判断
  1. int main(void)
  2. {
  3.         while (true)
  4.         {
  5.         ...
  6.                 printf("please input your choose:");//请输入你的选择
  7.                 scanf("%d", &iChoose);//获取iChoose
  8.         }
  9. }
复制代码
判断用户的选择

在实现菜单时,设置用户选择 0 时退出游戏,用户选择 1 开始游戏,用户选择 2 游戏帮助,其他选项则无效,故而按照此思绪进行判断
用户选择 0

此时退出游戏
  1. int main(void)
  2. {
  3.         while (true)
  4.         {
  5.         ...
  6.                 //退出
  7.                 if (0 == iChoose)
  8.                 {
  9.                         printf("\n\nExiting the game, please wait!\n");//正在退出游戏,请稍等
  10.                         Sleep(100);
  11.                         break;
  12.                 }
  13.         }
  14. }
复制代码
用户选择 2

此时表现游戏帮助,由于在 iChoose 为 0 分支内,采用了 break 语句跳出循环,故在此分支内依旧用 if 判断,而不采用 else if 判断
  1. int main(void)
  2. {
  3.         while (true)
  4.         {
  5.         ...
  6.                 //帮助
  7.                 if (2 == iChoose)
  8.                 {
  9.                         printf("\n\nThe game has primary, intermediate, advanced and custom modes\n");
  10.                         printf("A certain number of mines are randomly arranged in the minefield\n");
  11.                         printf("The player needs to find all the blocks that are not mines as soon as possible, but do not step on the mines.\n\n\n");
  12.                         Sleep(2000);
  13.                         continue;
  14.                 }
  15.         }
  16. }
复制代码
用户选择其他选项

此时为无效输入,由于在 iChoose 为 2 分支内,采用了 continue 语句来竣事此次循环,进入下一次循环,故在此分支内依旧用 if 判断,而不采用 else if 判断
  1. int main(void)
  2. {
  3.         while (true)
  4.         {
  5.         ...
  6.                 //无效输入
  7.                 if (iChoose > 2)
  8.                 {
  9.                         printf("\n\nInvalid input\n\n");//输入无效
  10.                         Sleep(500);
  11.                         continue;
  12.                 }
  13.         }
  14. }
复制代码
用户选择 1

此时开始游戏,以上分支均未跑到的话,那用户就是选择 1 了,此处无需判断,直接实现
此处将游戏部分封装在了 Play 函数内,直接调用,调用竣事后代表游戏竣事,随即询问用户是否再来一局:
如若不来,则退出循环,再在外层循环判断一下,进而退出外层循环
如若需要再来一局,则继承循环,调用 Play 函数,进行游戏
  1. int main(void)
  2. {
  3.     ...
  4.                 int iiChoose;
  5.                 //游戏
  6.                 while (true)
  7.                 {
  8.                         //PlayMenu();//无等级选择,故无此函数
  9.                         Play();
  10.                         printf("Do you want another game? 1 Yes, other no");//是否再来一局,1是,其他否
  11.                         scanf("%d", &iiChoose);
  12.                         if (1 != iiChoose)
  13.                                 break;
  14.                 }
  15.                 if (1 != iiChoose)
  16.                         break;
  17.         }
  18. }
复制代码
完整代码

见网盘
扫雷 https://www.alipan.com/s/yVJb2bBk1yj 提取码: 33jm 点击链接保存,大概复制本段内容,打开「阿里云盘」APP ,无需下载极速在线查看,视频原画倍速播放。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4