您好,登錄后才能下訂單哦!
這篇文章主要講解了“利用C語言寫一個數字益智小游戲代碼分享”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“利用C語言寫一個數字益智小游戲代碼分享”吧!
代碼部分頭文件區域
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<time.h> #include<math.h> int score0 = 0;int u, z;
啟動界面及主菜單
int main() //主函數游戲說明 { int menu(); char o; printf("\n\n"); printf("\t= = = = = = = = = = = = = = = =\n"); printf("\t= 數 字 益 智 小 游 戲 =\n"); printf("\t= =\n"); printf("\t= 歡迎游玩該數字益智小游戲,游戲共設有 =\n"); printf("\t= 四個小游戲,其中『算』小游戲各設有四 =\n"); printf("\t= 個關卡。當你挑戰難度越高的小游戲,并 =\n"); printf("\t= 挑戰成功,你將獲得更高的得分獎勵,最 =\n"); printf("\t= 終將根據你的總得分劃分你的游戲段位。 =\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() //游戲主菜單函數 { 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.『 算 』一位數運算 * \n"); printf("\t * 2.『 算 』兩位數運算 * \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 請輸入序號選擇:"); 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 你輸入的數字序號有誤噢,請重新選擇!\n"); system("pause"); system("cls"); menu(); }}
得分統計
int scorep() //查看當前得分 { 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 * 當前得分: * \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 繼續游戲請輸入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 你輸入的數字序號有誤噢,返回主菜單!\n"); menu(); }}int score() //加分函數 { score0++; return score0;}int scores() //減分函數 { score0--; return score0;}
一位數運算
void one_arithmetic() //一位數運算 { 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"); //一位數運算主菜單 printf("\t\t * * \n"); printf("\t\t * 1. [第一關] 加 法 * \n"); printf("\t\t * 2. [第二關] 減 法 * \n"); printf("\t\t * 3. [第三關] 乘 法 * \n"); printf("\t\t * 4. [第四關] 除 法 * \n"); printf("\t\t * 5. [進階]混合運算 * \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請輸入數字序號選擇挑戰關卡:"); scanf_s("%d", &p); while (p == 1) //一位數加法 { system("cls"); printf("\n\t%d + %d = ", a, b); scanf_s("%d", &c); if (c == a + b) { system("color AE"); printf("\n太棒了!挑戰成功,加 1 分!\n\n"); score(); system("pause"); } else if (c != a + b) { system("color 4E"); printf("\n太可惜了!挑戰失敗,不要灰心噢!\n\n"); system("pause"); } one_arithmetic(); } while (p == 2) //一位數減法 { system("cls"); if (a > b) //控制大數減小數 { printf("\n\t%d - %d = ", a, b); scanf_s("%d", &c); if (c == a - b) { system("color AE"); printf("\n太棒了!挑戰成功,加 1 分!\n\n"); score(); system("pause"); } else if (c != a - b) { system("color 4E"); printf("\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太棒了!挑戰成功,加 1 分!\n\n"); score(); system("pause"); } else if (c != b - a) { system("color 4E"); printf("\n\t太可惜了!挑戰失敗,不要灰心噢!\n\n"); system("pause"); } one_arithmetic(); } } while (p == 3) //一位數乘法 { system("cls"); printf("\n\t%d * %d = ", a, b); scanf_s("%d", &c); if (c == a * b) { system("color AE"); printf("\n太棒了!挑戰成功,加 1 分!\n\n"); score(); system("pause"); } else if (c != a * b) { system("color 4E"); printf("\n\t\t太可惜了!挑戰失敗,不要灰心噢!\n\n"); system("pause"); } one_arithmetic(); } while (p == 4) //一位數除法 { system("cls"); b = rand() % (9) + 1; //防止被除數為0 printf("\n\t(四舍五入保留整數)%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太棒了!挑戰成功,加 1 分!\n\n"); score(); system("pause"); } else if (d != a / b) { system("color 4E"); printf("\n太可惜了!挑戰失敗,不要灰心噢!\n\n"); system("pause"); } } if (a % b > f) { d = (int)(e - 0.5); if (d == a / b) { system("color AE"); printf("\n太棒了!挑戰成功,加 1 分!\n\n"); score(); system("pause"); } else if (d != a / b) { system("color 4E"); printf("\n太可惜了!挑戰失敗,不要灰心噢!\n\n"); system("pause"); } } one_arithmetic(); } while (p == 5) //一位數混合運算 { 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太棒了!挑戰成功,加 2 分!\n\n"); score(); score(); system("pause"); } else if (l != m) { system("color 4E"); printf("\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輸入數字序號有誤噢,請重新輸入!\n\n"); system("pause"); one_arithmetic(); }}
兩位數運算
void two_arithmetic() //兩位數運算 { 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"); //兩位數運算主菜單 printf("\t\t * * \n"); printf("\t\t * 1. [第一關] 加 法 * \n"); printf("\t\t * 2. [第二關] 減 法 * \n"); printf("\t\t * 3. [第三關] 乘 法 * \n"); printf("\t\t * 4. [第四關] 除 法 * \n"); printf("\t\t * 5. [進階]混合運算 * \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請輸入數字序號選擇挑戰關卡:"); scanf_s("%d", &p); while (p == 1) //兩位數加法 { system("cls"); printf("\n\t%d + %d = ", a, b); scanf_s("%d", &c); if (c == a + b) { system("color AE"); printf("\n太棒了!挑戰成功,加 1 分!\n\n"); score(); system("pause"); } else if (c != a + b) { system("color 4B"); printf("\n太可惜了!挑戰失敗,不要灰心噢!\n\n"); system("pause"); } two_arithmetic(); } while (p == 2) //兩位數減法 { if (a > b) //控制大數減小數 { system("cls"); printf("\n\t%d - %d = ", a, b); scanf_s("%d", &c); if (c == a - b) { system("color AE"); printf("\n太棒了!挑戰成功,加 1 分!\n\n"); score(); system("pause"); } else if (c != a - b) { system("color 4B"); printf("\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太棒了!挑戰成功,加 1 分!\n\n"); score(); system("pause"); } else if (c != b - a) { system("color 4B"); printf("\n太可惜了!挑戰失敗,不要灰心噢!\n\n"); system("pause"); } two_arithmetic(); } } while (p == 3) //兩位數乘法 { system("cls"); printf("\n\t%d * %d = ", a, b); scanf_s("%d", &c); if (c == a * b) { system("color AE"); printf("\n太棒了!挑戰成功,加 1 分!\n\n"); score(); system("pause"); } else if (c != a * b) { system("color 4B"); printf("\n太可惜了!挑戰失敗,不要灰心噢!\n\n"); system("pause"); } two_arithmetic(); } while (p == 4) //兩位數除法 { b = rand() % (9) + 1; //防止被除數為0 system("cls"); printf("\n\t(四舍五入保留整數)%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太棒了!挑戰成功,加 1 分!\n\n"); score(); system("pause"); } else if (d != a / b) { system("color 4B"); printf("\n太可惜了!挑戰失敗,不要灰心噢!\n\n"); system("pause"); } } if (a % b > f) { d = (int)(e - 0.5); if (d == a / b) { system("color AE"); printf("\n太棒了!挑戰成功,加 1 分!\n\n"); score(); system("pause"); } else if (d != a / b) { system("color 4B"); printf("\n太可惜了!挑戰失敗,不要灰心噢!\n\n"); system("pause"); } } two_arithmetic(); } while (p == 5) //兩位數混合運算 { 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太棒了!挑戰成功,加 2 分!\n\n"); score(); score(); system("pause"); } else if (l != m) { system("color 4B"); printf("\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輸入數字序號有誤噢,請重新輸入!\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"); //排序游戲規則 printf("\t* 游 戲 規 則 *\n"); printf("\t* 開始游戲后將會顯示隨機五個整數, *\n"); printf("\t* 請根據提示從小到大依次輸入排序后 *\n"); printf("\t* 的五個整數,若排序正確即可獲得 3 *\n"); printf("\t* 分獎勵。加油噢,你能行! *\n"); printf("\t* * * * * * * * * * * * * *\n"); printf("\t * * * \n"); printf("\t * * * \n"); printf("\t * * * * * \n"); printf("\t * * * \n"); printf("\t * \n"); printf("\t 輸入數字 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++) //將數組從小到大排列 { 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("接下來,請從小到大輸入排序后的五個整數(用空格隔開):"); for (i = 0; i < 5; i++) scanf_s("%d", &b[i]); for (i = 0; i < 5; i++) //驗證回答正確與否 { if (a[i] == b[i]) continue; else { system("color 49"); printf("\n\t太可惜了,就差一點就成功了!再挑戰一次吧!\n\n"); system("pause"); comparation(); } } system("color AF"); printf("\n哇你太棒啦,挑戰成功!獲得 3 分獎勵!\n\n"); score(); score(); score(); system("pause"); system("color 0F"); printf("\n\n想挑戰別的游戲嗎?\n\n"); printf("1. 挑戰其它游戲 "); printf(" 2. 繼續挑戰排序游戲 "); printf(" 3. 查看當前得分\n\n"); printf("回復數字序號選擇一項吧:"); 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. 挑戰其它游戲 "); printf(" 2. 繼續當前游戲\n\n"); printf("回復數字序號選擇一項吧:"); 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"); //找最值游戲規則 printf("\t* 游 戲 規 則 *\n"); printf("\t* 開始游戲后將會顯示隨機五個整數, *\n"); printf("\t* 請分別找出五個整數的最大值和最小 *\n"); printf("\t* 值,若回答正確即可獲得 2 分獎勵。 *\n"); printf("\t* 加油噢,你能行! *\n"); printf("\t* * * * * * * * * * * * * *\n"); printf("\t * * * \n"); printf("\t * * * \n"); printf("\t * * * * * \n"); printf("\t * * * \n"); printf("\t * \n"); printf("\t 輸入數字 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) //驗證回答正確與否 { system("color AF"); printf("\n哇你太棒啦!獲得獎勵 2 分!\n\n"); score(); score(); system("pause"); system("color 0F"); printf("\n\n想挑戰別的游戲嗎?\n\n"); printf("1. 挑戰其它游戲 "); printf(" 2. 繼續挑戰找最值游戲 "); printf(" 3. 查看當前得分\n\n"); printf("回復數字序號選擇一項吧:"); 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有點小可惜,再挑戰一次吧!\n\n"); system("pause"); maxmin(); } } else //退出找最值游戲? { system("color F0"); printf("\n\n想玩別的游戲嗎?\n\n"); printf("1. 挑戰其它游戲 "); printf(" 2. 回到當前游戲\n\n"); printf("回復數字序號選擇一項吧:"); scanf_s("%d", &q); if (q == 2) maxmin(); else { system("cls"); menu(); } }}
感謝各位的閱讀,以上就是“利用C語言寫一個數字益智小游戲代碼分享”的內容了,經過本文的學習后,相信大家對利用C語言寫一個數字益智小游戲代碼分享這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。