溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

利用C語言寫一個(gè)數(shù)字益智小游戲代碼分享

發(fā)布時(shí)間:2021-08-27 18:04:27 來源:億速云 閱讀:160 作者:chen 欄目:編程語言

這篇文章主要講解了“利用C語言寫一個(gè)數(shù)字益智小游戲代碼分享”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“利用C語言寫一個(gè)數(shù)字益智小游戲代碼分享”吧!

代碼部分頭文件區(qū)域

#include<stdio.h> #include<string.h> #include<stdlib.h> #include<time.h> #include<math.h> int score0 = 0;int u, z;

啟動(dòng)界面及主菜單

int main()     //主函數(shù)游戲說明 { int menu(); char o; printf("\n\n"); printf("\t= = = = = = = = = = = = = = = =\n"); printf("\t=  數(shù) 字 益 智 小 游 戲  =\n"); printf("\t=      =\n"); printf("\t= 歡迎游玩該數(shù)字益智小游戲,游戲共設(shè)有 =\n"); printf("\t= 四個(gè)小游戲,其中『算』小游戲各設(shè)有四 =\n"); printf("\t= 個(gè)關(guān)卡。當(dāng)你挑戰(zhàn)難度越高的小游戲,并 =\n"); printf("\t= 挑戰(zhàn)成功,你將獲得更高的得分獎(jiǎng)勵(lì),最 =\n"); printf("\t= 終將根據(jù)你的總得分劃分你的游戲段位。 =\n"); printf("\t=      =\n"); printf("\t=      =\n"); printf("\t= * * * * * * * * * =\n"); printf("\t= *  游 戲 段 位 * =\n"); printf("\t= * 『青銅』 --- 5 分 以 下 * =\n"); printf("\t= * 『白銀』 --- 5 分 - 10 分 * =\n"); printf("\t= * 『黃金』 --- 10 分 - 15 分 * =\n"); printf("\t= * 『鉑金』 --- 15 分 - 20 分 * =\n"); printf("\t= * 『鉆石』 --- 20 分 - 25 分 * =\n"); printf("\t= * 『星耀』 --- 25 分 - 30 分 * =\n"); printf("\t= * 『王者』 --- 30 分 以 上 * =\n"); printf("\t= * * * * * * * * * =\n"); printf("\t=      =\n"); printf("\t=  作 者 : Cowjiang  =\n"); printf("\t=      =\n"); printf("\t= = = = = = = = = = = = = = = =\n\n"); printf("\t  按回車鍵即可開始游戲"); scanf_s("%c", &o, 1); if (o == '\n')     //輸入回車開始游戲  { system("cls"); menu(); } else exit(0);}int menu()     //游戲主菜單函數(shù) { void one_arithmetic(); void two_arithmetic(); void comparation(); void maxmin(); int score(); int scores(); int scorep(); int o; system("color 30"); printf("\n\n"); printf("\t   **   \n"); printf("\t  *  *   \n"); printf("\t  * 主 菜 單 *  \n"); printf("\t *    * \n"); printf("\t* * * * * * * * * * * * * * * \n"); printf("\t *    * \n"); printf("\t * 1.『 算 』一位數(shù)運(yùn)算 * \n"); printf("\t * 2.『 算 』兩位數(shù)運(yùn)算 * \n"); printf("\t * 3.『 找 』夾縫找最值 * \n"); printf("\t * 4.『 排 』排序比大小 * \n"); printf("\t * 5. 我 的 目 前 得 分 * \n"); printf("\t * 6. 退 出 益 智 游 戲 * \n"); printf("\t *    * \n"); printf("\t * * * * * * * * * \n\n"); printf("\t  請輸入序號(hào)選擇:"); scanf_s("%d", &o); if (o == 1) { one_arithmetic(); } else if (o == 2) { two_arithmetic(); } else if (o == 3) { maxmin(); } else if (o == 4) { comparation(); } else if (o == 5) { scorep(); } else if (o == 6) { printf("\n\t\t= = = = = = = = = = = = = = = = =\n"); printf("\t\t=    =\n"); printf("\t\t=  感謝你的游玩! =\n"); printf("\t\t=    =\n"); printf("\t\t= = = = = = = = = = = = = = = = =\n"); exit(0); } else { printf("\n\t 你輸入的數(shù)字序號(hào)有誤噢,請重新選擇!\n"); system("pause"); system("cls"); menu(); }}

得分統(tǒng)計(jì)

int scorep()    //查看當(dāng)前得分 { system("color F4"); z = score0; system("cls"); printf("\n\n"); printf("\t\t  *   \n"); printf("\t\t  * *   \n"); printf("\t\t  * *   \n"); printf("\t\t* * * * * * * * \n"); printf("\t\t * 當(dāng)前得分: *  \n"); printf("\t\t * %2d分 *  \n", z); printf("\t\t * * * *  \n"); printf("\t\t * * * *  \n"); if (z <= 5) { printf("\t\t ** [青 銅] **  \n"); printf("\t\t *   *  \n"); } else if (z > 5 && z <= 10) { printf("\t\t ** [白 銀] **  \n"); printf("\t\t *   *  \n"); } else if (z > 10 && z <= 15) { printf("\t\t ** [黃 金] **  \n"); printf("\t\t *   *  \n"); } else if (z > 15 && z <= 20) { printf("\t\t ** [鉑 金] **  \n"); printf("\t\t *   *  \n"); } else if (z > 20 && z <= 25) { printf("\t\t ** [鉆 石] **  \n"); printf("\t\t *   *  \n"); } else if (z > 25 && z <= 30) { printf("\t\t ** [星 耀] **  \n"); printf("\t\t *   *  \n"); } else if (z > 30) { printf("\t\t ** [王 者] **  \n"); printf("\t\t *   *  \n"); } printf("\n\n\t 繼續(xù)游戲請輸入1,退出游戲請輸入2 :"); scanf_s("%d", &u); if (u == 1)    //返回主菜單  { system("cls"); menu(); } else if (u == 2)    //退出游戲  { printf("\n\t\t= = = = = = = = = = = = = = = = =\n"); printf("\t\t=    =\n"); printf("\t\t=  感謝你的游玩! =\n"); printf("\t\t=    =\n"); printf("\t\t= = = = = = = = = = = = = = = = =\n"); exit(0); } else { printf("\n\t 你輸入的數(shù)字序號(hào)有誤噢,返回主菜單!\n"); menu(); }}int score()    //加分函數(shù) { score0++; return score0;}int scores()    //減分函數(shù) { score0--; return score0;}

一位數(shù)運(yùn)算

void one_arithmetic()   //一位數(shù)運(yùn)算 { int p, a, b, c, d, f, g, h, l, m; float e; srand(time(NULL)); a = rand() % 10; b = rand() % 10; g = rand() % 10; h = rand() % 10; system("color E0"); system("cls"); printf("\n\n\t\t * * * * * * * * \n"); //一位數(shù)運(yùn)算主菜單  printf("\t\t *   * \n"); printf("\t\t * 1. [第一關(guān)] 加 法 * \n"); printf("\t\t * 2. [第二關(guān)] 減 法 * \n"); printf("\t\t * 3. [第三關(guān)] 乘 法 * \n"); printf("\t\t * 4. [第四關(guān)] 除 法 * \n"); printf("\t\t * 5. [進(jìn)階]混合運(yùn)算 * \n"); printf("\t\t * 6. [返 回] 主菜單 * \n"); printf("\t\t *   * \n"); printf("\t\t * * * * * * * * \n"); printf("\t\t  * * *  \n"); printf("\t\t  * * *  \n"); printf("\t\t * * * * * \n"); printf("\t\t  * * *  \n"); printf("\t\t  *  \n"); printf("\t\t請輸入數(shù)字序號(hào)選擇挑戰(zhàn)關(guān)卡:"); scanf_s("%d", &p); while (p == 1)    //一位數(shù)加法  { system("cls"); printf("\n\t%d + %d = ", a, b); scanf_s("%d", &c); if (c == a + b) {  system("color AE");  printf("\n太棒了!挑戰(zhàn)成功,加 1 分!\n\n");  score();  system("pause"); } else if (c != a + b) {  system("color 4E");  printf("\n太可惜了!挑戰(zhàn)失敗,不要灰心噢!\n\n");  system("pause"); } one_arithmetic(); } while (p == 2)     //一位數(shù)減法  { system("cls"); if (a > b)   //控制大數(shù)減小數(shù)  {  printf("\n\t%d - %d = ", a, b);  scanf_s("%d", &c);  if (c == a - b)  {  system("color AE");  printf("\n太棒了!挑戰(zhàn)成功,加 1 分!\n\n");  score();  system("pause");  }  else if (c != a - b)  {  system("color 4E");  printf("\n太可惜了!挑戰(zhàn)失敗,不要灰心噢!\n\n");  system("pause");  }  one_arithmetic(); } else if (a < b) {  system("cls");  printf("\n\t%d - %d = ", b, a);  scanf_s("%d", &c);  if (c == b - a)  {  system("color AE");  printf("\n太棒了!挑戰(zhàn)成功,加 1 分!\n\n");  score();  system("pause");  }  else if (c != b - a)  {  system("color 4E");  printf("\n\t太可惜了!挑戰(zhàn)失敗,不要灰心噢!\n\n");  system("pause");  }  one_arithmetic(); } } while (p == 3) //一位數(shù)乘法  { system("cls"); printf("\n\t%d * %d = ", a, b); scanf_s("%d", &c); if (c == a * b) {  system("color AE");  printf("\n太棒了!挑戰(zhàn)成功,加 1 分!\n\n");  score();  system("pause"); } else if (c != a * b) {  system("color 4E");  printf("\n\t\t太可惜了!挑戰(zhàn)失敗,不要灰心噢!\n\n");  system("pause"); } one_arithmetic(); } while (p == 4)  //一位數(shù)除法  { system("cls"); b = rand() % (9) + 1;     //防止被除數(shù)為0  printf("\n\t(四舍五入保留整數(shù))%d除以%d等于", a, b); scanf_s("%f", &e); f = b / 2;      //四舍五入  if (a % b <= f) {  d = (int)(e);  if (d == a / b)  {  system("color AE");  printf("\n太棒了!挑戰(zhàn)成功,加 1 分!\n\n");  score();  system("pause");  }  else if (d != a / b)  {  system("color 4E");  printf("\n太可惜了!挑戰(zhàn)失敗,不要灰心噢!\n\n");  system("pause");  } } if (a % b > f) {  d = (int)(e - 0.5);  if (d == a / b)  {  system("color AE");  printf("\n太棒了!挑戰(zhàn)成功,加 1 分!\n\n");  score();  system("pause");  }  else if (d != a / b)  {  system("color 4E");  printf("\n太可惜了!挑戰(zhàn)失敗,不要灰心噢!\n\n");  system("pause");  } } one_arithmetic(); } while (p == 5)    //一位數(shù)混合運(yùn)算  { system("cls"); printf("\n\t%d + %d * ( %d - %d ) = ", a, b, g, h); scanf_s("%d", &l); m = a + b * (g - h); if (l == m) {  system("color AE");  printf("\n太棒了!挑戰(zhàn)成功,加 2 分!\n\n");  score();  score();  system("pause"); } else if (l != m) {  system("color 4E");  printf("\n太可惜了!挑戰(zhàn)失敗,不要灰心噢!\n\n");  system("pause"); } one_arithmetic(); } while (p == 6) { system("cls"); menu(); } while (p < 1 || p > 6) { system("color 4E"); printf("\n\t\t輸入數(shù)字序號(hào)有誤噢,請重新輸入!\n\n"); system("pause"); one_arithmetic(); }}

兩位數(shù)運(yùn)算

void two_arithmetic()   //兩位數(shù)運(yùn)算 { int p, a, b, c, d, f, g, h, l, m; float e; srand(time(NULL)); a = rand() % (90) + 10; b = rand() % (90) + 10; g = rand() % (90) + 10; h = rand() % (90) + 10; system("color B0"); system("cls"); printf("\n\n\t\t * * * * * * * * \n"); //兩位數(shù)運(yùn)算主菜單  printf("\t\t *   * \n"); printf("\t\t * 1. [第一關(guān)] 加 法 * \n"); printf("\t\t * 2. [第二關(guān)] 減 法 * \n"); printf("\t\t * 3. [第三關(guān)] 乘 法 * \n"); printf("\t\t * 4. [第四關(guān)] 除 法 * \n"); printf("\t\t * 5. [進(jìn)階]混合運(yùn)算 * \n"); printf("\t\t * 6. [返 回] 主菜單 * \n"); printf("\t\t *   * \n"); printf("\t\t * * * * * * * * \n"); printf("\t\t  * * *  \n"); printf("\t\t  * * *  \n"); printf("\t\t * * * * * \n"); printf("\t\t  * * *  \n"); printf("\t\t  *  \n"); printf("\t\t請輸入數(shù)字序號(hào)選擇挑戰(zhàn)關(guān)卡:"); scanf_s("%d", &p); while (p == 1)   //兩位數(shù)加法  { system("cls"); printf("\n\t%d + %d = ", a, b); scanf_s("%d", &c); if (c == a + b) {  system("color AE");  printf("\n太棒了!挑戰(zhàn)成功,加 1 分!\n\n");  score();  system("pause"); } else if (c != a + b) {  system("color 4B");  printf("\n太可惜了!挑戰(zhàn)失敗,不要灰心噢!\n\n");  system("pause"); } two_arithmetic(); } while (p == 2)  //兩位數(shù)減法  { if (a > b)  //控制大數(shù)減小數(shù)  {  system("cls");  printf("\n\t%d - %d = ", a, b);  scanf_s("%d", &c);  if (c == a - b)  {  system("color AE");  printf("\n太棒了!挑戰(zhàn)成功,加 1 分!\n\n");  score();  system("pause");  }  else if (c != a - b)  {  system("color 4B");  printf("\n太可惜了!挑戰(zhàn)失敗,不要灰心噢!\n\n");  system("pause");  }  two_arithmetic(); } else if (a < b) {  system("cls");  printf("\n\t%d - %d = ", b, a);  scanf_s("%d", &c);  if (c == b - a)  {  system("color AE");  printf("\n太棒了!挑戰(zhàn)成功,加 1 分!\n\n");  score();  system("pause");  }  else if (c != b - a)  {  system("color 4B");  printf("\n太可惜了!挑戰(zhàn)失敗,不要灰心噢!\n\n");  system("pause");  }  two_arithmetic(); } } while (p == 3)     //兩位數(shù)乘法  { system("cls"); printf("\n\t%d * %d = ", a, b); scanf_s("%d", &c); if (c == a * b) {  system("color AE");  printf("\n太棒了!挑戰(zhàn)成功,加 1 分!\n\n");  score();  system("pause"); } else if (c != a * b) {  system("color 4B");  printf("\n太可惜了!挑戰(zhàn)失敗,不要灰心噢!\n\n");  system("pause"); } two_arithmetic(); } while (p == 4)      //兩位數(shù)除法  { b = rand() % (9) + 1;    //防止被除數(shù)為0  system("cls"); printf("\n\t(四舍五入保留整數(shù))%d除以%d等于", a, b); scanf_s("%f", &e); f = b / 2;     //四舍五入  if (a % b <= f) {  d = (int)(e);  if (d == a / b)  {  system("color AE");  printf("\n太棒了!挑戰(zhàn)成功,加 1 分!\n\n");  score();  system("pause");  }  else if (d != a / b)  {  system("color 4B");  printf("\n太可惜了!挑戰(zhàn)失敗,不要灰心噢!\n\n");  system("pause");  } } if (a % b > f) {  d = (int)(e - 0.5);  if (d == a / b)  {  system("color AE");  printf("\n太棒了!挑戰(zhàn)成功,加 1 分!\n\n");  score();  system("pause");  }  else if (d != a / b)  {  system("color 4B");  printf("\n太可惜了!挑戰(zhàn)失敗,不要灰心噢!\n\n");  system("pause");  } } two_arithmetic(); } while (p == 5)  //兩位數(shù)混合運(yùn)算  { system("cls"); printf("\n\t%d + %d * ( %d - %d ) = ", a, b, g, h); scanf_s("%d", &l); m = a + b * (g - h); if (l == m) {  system("color AE");  printf("\n太棒了!挑戰(zhàn)成功,加 2 分!\n\n");  score();  score();  system("pause"); } else if (l != m) {  system("color 4B");  printf("\n太可惜了!挑戰(zhàn)失敗,不要灰心噢!\n\n");  system("pause"); } one_arithmetic(); } while (p == 6) { system("cls"); menu(); } while (p < 1 || p > 6) { system("color 4B"); printf("\n\t\t輸入數(shù)字序號(hào)有誤噢,請重新輸入!\n\n"); system("pause"); two_arithmetic(); }}

排序游戲

void comparation()    //排序游戲 { int a[5], b[5]; int i, j, n, p, q; srand(time(NULL)); for (i = 0; i < 5; i++) a[i] = rand() % 10; system("cls"); system("color 9F"); printf("\n\n\t* * * * * * * * * * * * * *\n"); //排序游戲規(guī)則  printf("\t*  游 戲 規(guī) 則  *\n"); printf("\t* 開始游戲后將會(huì)顯示隨機(jī)五個(gè)整數(shù), *\n"); printf("\t* 請根據(jù)提示從小到大依次輸入排序后 *\n"); printf("\t* 的五個(gè)整數(shù),若排序正確即可獲得 3 *\n"); printf("\t* 分獎(jiǎng)勵(lì)。加油噢,你能行!  *\n"); printf("\t* * * * * * * * * * * * * *\n"); printf("\t   * * *  \n"); printf("\t   * * *  \n"); printf("\t  * * * * * \n"); printf("\t   * * *  \n"); printf("\t   *  \n"); printf("\t  輸入數(shù)字 0 即可開始游戲:"); scanf_s("%d", &p); if (p == 0)         printf("\n\n\t* * * * * * * * * * * * * *\n"); printf("\t*     *\n"); printf("\t* "); for (i = 0; i < 5; i++)  printf(" %d ", a[i]); for (j = 0; j < 4; j++)   //將數(shù)組從小到大排列  {  for (i = 0; i < 4 - j; i++)  {  if (a[i] > a[i + 1])  {   n = a[i];   a[i] = a[i + 1];   a[i + 1] = n;  }  } } printf(" *\n"); printf("\t*     *\n"); printf("\t* * * * * * * * * * * * * *\n\n"); printf("接下來,請從小到大輸入排序后的五個(gè)整數(shù)(用空格隔開):"); for (i = 0; i < 5; i++)  scanf_s("%d", &b[i]); for (i = 0; i < 5; i++)    //驗(yàn)證回答正確與否  {  if (a[i] == b[i])  continue;  else  {  system("color 49");  printf("\n\t太可惜了,就差一點(diǎn)就成功了!再挑戰(zhàn)一次吧!\n\n");  system("pause");  comparation();  } } system("color AF"); printf("\n哇你太棒啦,挑戰(zhàn)成功!獲得 3 分獎(jiǎng)勵(lì)!\n\n"); score(); score(); score(); system("pause"); system("color 0F"); printf("\n\n想挑戰(zhàn)別的游戲嗎?\n\n"); printf("1. 挑戰(zhàn)其它游戲 "); printf(" 2. 繼續(xù)挑戰(zhàn)排序游戲 "); printf(" 3. 查看當(dāng)前得分\n\n"); printf("回復(fù)數(shù)字序號(hào)選擇一項(xiàng)吧:"); scanf_s("%d", &q); if (q == 2) {  system("cls");  comparation(); } else if (q == 3)  scorep(); else {  system("cls");  menu(); } } else  //退出排序游戲?  { system("color F0"); printf("\n\n想玩別的游戲嗎?\n\n"); printf("1. 挑戰(zhàn)其它游戲 "); printf(" 2. 繼續(xù)當(dāng)前游戲\n\n"); printf("回復(fù)數(shù)字序號(hào)選擇一項(xiàng)吧:"); scanf_s("%d", &q); if (q == 2)  comparation(); else {  system("cls");  menu(); } }}

找最值游戲

void maxmin(){ int i, j, n, p, q, max, min, maxx, minn; int a[5]; srand(time(NULL)); system("color DF"); system("cls"); printf("\n\n\t* * * * * * * * * * * * * *\n"); //找最值游戲規(guī)則  printf("\t*  游 戲 規(guī) 則  *\n"); printf("\t* 開始游戲后將會(huì)顯示隨機(jī)五個(gè)整數(shù), *\n"); printf("\t* 請分別找出五個(gè)整數(shù)的最大值和最小 *\n"); printf("\t* 值,若回答正確即可獲得 2 分獎(jiǎng)勵(lì)。 *\n"); printf("\t* 加油噢,你能行!   *\n"); printf("\t* * * * * * * * * * * * * *\n"); printf("\t   * * *  \n"); printf("\t   * * *  \n"); printf("\t  * * * * * \n"); printf("\t   * * *  \n"); printf("\t   *  \n"); printf("\t  輸入數(shù)字 0 即可開始游戲:"); scanf_s("%d", &p); if (p == 0) { for (i = 0; i < 5; i++)  a[i] = rand() % 10; printf("\n\n\t* * * * * * * * * * * * * *\n"); printf("\t*     *\n"); printf("\t* "); for (i = 0; i < 5; i++)  printf(" %d ", a[i]); printf(" *\n"); printf("\t*     *\n"); printf("\t* * * * * * * * * * * * * *\n\n"); printf("接下來,請找出最大值:"); scanf_s("%d", &maxx); printf("\n然后請找出最小值:"); scanf_s("%d", &minn); max = a[4]; min = a[1]; for (i = 0; i < 5; i++) {  if (a[i] > max)  max = a[i];  if (a[i] < min)  min = a[i]; } if (maxx == max && minn == min)  //驗(yàn)證回答正確與否  {  system("color AF");  printf("\n哇你太棒啦!獲得獎(jiǎng)勵(lì) 2 分!\n\n");  score();  score();  system("pause");  system("color 0F");  printf("\n\n想挑戰(zhàn)別的游戲嗎?\n\n");  printf("1. 挑戰(zhàn)其它游戲 ");  printf(" 2. 繼續(xù)挑戰(zhàn)找最值游戲 ");  printf(" 3. 查看當(dāng)前得分\n\n");  printf("回復(fù)數(shù)字序號(hào)選擇一項(xiàng)吧:");  scanf_s("%d", &q);  if (q == 2)  {  system("cls");  maxmin();  }  else if (q == 3)  scorep();  else  {  system("cls");  menu();  } } else {  system("color 4F");  printf("\n有點(diǎn)小可惜,再挑戰(zhàn)一次吧!\n\n");  system("pause");  maxmin(); } } else  //退出找最值游戲?  { system("color F0"); printf("\n\n想玩別的游戲嗎?\n\n"); printf("1. 挑戰(zhàn)其它游戲 "); printf(" 2. 回到當(dāng)前游戲\n\n"); printf("回復(fù)數(shù)字序號(hào)選擇一項(xiàng)吧:"); scanf_s("%d", &q); if (q == 2)  maxmin(); else {  system("cls");  menu(); } }}

感謝各位的閱讀,以上就是“利用C語言寫一個(gè)數(shù)字益智小游戲代碼分享”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對利用C語言寫一個(gè)數(shù)字益智小游戲代碼分享這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI