小編給大家分享一下C語言中如何編一個數(shù)字益智小游戲,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
我們提供的服務有:網(wǎng)站建設、做網(wǎng)站、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認證、青羊ssl等。為成百上千企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務,是有科學管理、有技術的青羊網(wǎng)站制作公司程序功能及運行情況
設計的程序是一個數(shù)字益智游戲,旨在培養(yǎng)小朋友玩家的數(shù)學思維,提高玩家的數(shù)學能力。游戲共設有四個不同的小游戲,分別是一位數(shù)四則運算、兩位數(shù)四則運算、找最值游戲、排序游戲。程序能實現(xiàn)產(chǎn)生隨機題目,并能檢驗玩家的作答是否正確。為了小朋友能感受到游戲的樂趣,特意設置了得分系統(tǒng),答對不同題目有不同的加分,并會根據(jù)總得分劃分不同「段位」,呈現(xiàn)給玩家。另外,程序還運用了更加充滿活力的設計,讓玩家愛上該游戲。
程序運行截圖主菜單
運算游戲
找最值游戲
排序游戲
得分統(tǒng)計
代碼部分頭文件區(qū)域
#include#include #include #include #include int score0 = 0; int u, z;
啟動界面及主菜單
int main() //主函數(shù)游戲說明 { int menu(); char o; printf("\n\n"); printf("\t= = = = = = = = = = = = = = = =\n"); printf("\t= 數(shù) 字 益 智 小 游 戲 =\n"); printf("\t= =\n"); printf("\t= 歡迎游玩該數(shù)字益智小游戲,游戲共設有 =\n"); printf("\t= 四個小游戲,其中『算』小游戲各設有四 =\n"); printf("\t= 個關卡。當你挑戰(zhàn)難度越高的小游戲,并 =\n"); printf("\t= 挑戰(zhàn)成功,你將獲得更高的得分獎勵,最 =\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ù)運算 * \n"); printf("\t * 2.『 算 』兩位數(shù)運算 * \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 你輸入的數(shù)字序號有誤噢,請重新選擇!\n"); system("pause"); system("cls"); menu(); } }
得分統(tǒng)計
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 繼續(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ù)字序號有誤噢,返回主菜單!\n"); menu(); } } int score() //加分函數(shù) { score0++; return score0; } int scores() //減分函數(shù) { score0--; return score0; }
一位數(shù)運算
void one_arithmetic() //一位數(shù)運算 { 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ù)運算主菜單 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請輸入數(shù)字序號選擇挑戰(zhà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ù)混合運算 { 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ù)字序號有誤噢,請重新輸入!\n\n"); system("pause"); one_arithmetic(); } }
兩位數(shù)運算
void two_arithmetic() //兩位數(shù)運算 { 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ù)運算主菜單 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請輸入數(shù)字序號選擇挑戰(zhà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ù)混合運算 { 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ù)字序號有誤噢,請重新輸入!\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* 開始游戲后將會顯示隨機五個整數(shù), *\n"); printf("\t* 請根據(jù)提示從小到大依次輸入排序后 *\n"); printf("\t* 的五個整數(shù),若排序正確即可獲得 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 輸入數(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("接下來,請從小到大輸入排序后的五個整數(shù)(用空格隔開):"); 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太可惜了,就差一點就成功了!再挑戰(zhàn)一次吧!\n\n"); system("pause"); comparation(); } } system("color AF"); printf("\n哇你太棒啦,挑戰(zhàn)成功!獲得 3 分獎勵!\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. 查看當前得分\n\n"); printf("回復數(shù)字序號選擇一項吧:"); 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ù)當前游戲\n\n"); printf("回復數(shù)字序號選擇一項吧:"); 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* 開始游戲后將會顯示隨機五個整數(shù), *\n"); printf("\t* 請分別找出五個整數(shù)的大值和最小 *\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 輸入數(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) //驗證回答正確與否 { system("color AF"); printf("\n哇你太棒啦!獲得獎勵 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. 查看當前得分\n\n"); printf("回復數(shù)字序號選擇一項吧:"); 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有點小可惜,再挑戰(zhàn)一次吧!\n\n"); system("pause"); maxmin(); } } else //退出找最值游戲? { system("color F0"); printf("\n\n想玩別的游戲嗎?\n\n"); printf("1. 挑戰(zhàn)其它游戲 "); printf(" 2. 回到當前游戲\n\n"); printf("回復數(shù)字序號選擇一項吧:"); scanf_s("%d", &q); if (q == 2) maxmin(); else { system("cls"); menu(); } } }
以上是“C語言中如何編一個數(shù)字益智小游戲”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關注創(chuàng)新互聯(lián)網(wǎng)站建設公司行業(yè)資訊頻道!
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)建站www.cdcxhl.com,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。