神奇的c/c++小游戲((提高你的編程興趣)
神奇的c/c++
以下代碼在Dev,codeblocks,VC上都能運(yùn)行
#include<stdio.h> #include<time.h> #include<stdlib.h> #include<conio.h> #include<windows.h> //下面Sleep()函數(shù)的頭文件 #include<mmsystem.h> void menu() { printf(" *****************************\n"); printf(" *****************************\n"); printf(" ************1.play***********\n"); printf(" ************0.exit***********\n"); printf(" **********2.chakan***********\n"); printf(" *********3.qingkong**********\n"); printf(" *****************************\n"); printf("請(qǐng)選擇: \n"); } void game() { system("cls"); system ( "color 33" ); //設(shè)置顏色 FILE *fp; if((fp=fopen("D:\\caishuziyou.txt","a+"))==NULL) { printf("open file error\n"); return ; } int rand_num=rand()%100; int tmp=0,t=0; while(1) { int flag=0; printf("請(qǐng)輸入你要猜的數(shù)字:"); scanf("%d",&tmp); if(tmp>rand_num) { printf("猜大了!\n"); flag=0; } if(tmp<rand_num) { printf("猜小了!\n"); flag=0; } if(tmp==rand_num) { printf("正確!恭喜!\n"); printf("共用了%d次\n",t+1); FILE *fp; fp=fopen("D:\\caishuziyou.txt","w"); if(fp==NULL) { printf("create file failed\n"); return ; } fprintf(fp,"%d",t+1); fclose(fp); break; } if(flag==0) t++; printf(" 你再猜:\n"); } printf("\n按任意鍵返回目錄"); getch(); } void menu2() { system("cls"); system ( "color B6" ); //設(shè)置顏色 printf("即將登陸系統(tǒng)?。?!\n"); getch(); for(int t=3;t>=1;t--) { system("cls"); printf("%d",t); Sleep(700); system("cls"); } } void chakan() { system("cls"); system ( "color E0" ); //設(shè)置顏色 int t=0; FILE*fp; if((fp=fopen("D:\\caishuziyou.txt","r"))==NULL) { printf("can not to open the file!\n"); exit(0); } while(fscanf(fp,"%d",&t)!=EOF)//若不到文件結(jié)尾則繼續(xù) { printf("此玩家共進(jìn)行了%d次\n",t); } printf("\n按任意鍵返回目錄"); getch(); } void qk()//清空 { FILE *fp; system("cls"); //清屏函數(shù),不管下面是否保存數(shù)據(jù),每次輸入新數(shù)據(jù)是屏幕上只能有此次的數(shù)據(jù) system ( "color C0" ); //設(shè)置顏色 if((fp=fopen("D:\\caishuziyou.txt","w"))==NULL) { printf("open file error\n"); return; } fclose(fp); printf("\n按任意鍵返回!"); getch(); } int main() { int input=0; srand((unsigned)time(NULL)); do{ //while(1) //{ system("cls"); menu(); // printf("請(qǐng)選擇:"); scanf("%d",&input); switch(input) { case 1: menu2(); game(); break; case 2: chakan(); break; case 3: qk(); break; case 0: exit(0); break; default:printf("輸入錯(cuò)誤!請(qǐng)重新輸入!"); } //} }while(input); return 0; }
以上就是神奇的c/c++小游戲的詳細(xì)代碼,更多關(guān)于c/c++小游戲的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C語言實(shí)現(xiàn)航班售票系統(tǒng) C語言實(shí)現(xiàn)航班管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)航班售票系統(tǒng),C語言實(shí)現(xiàn)航班管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12Linux網(wǎng)絡(luò)編程之基于UDP實(shí)現(xiàn)可靠的文件傳輸示例
這篇文章主要介紹了Linux網(wǎng)絡(luò)編程之基于UDP實(shí)現(xiàn)可靠的文件傳輸示例,是很實(shí)用的技巧,需要的朋友可以參考下2014-08-08C++函數(shù)三種傳參形式(指針傳遞、引用傳遞、值傳遞)
不論是哪種參數(shù)傳遞方式,都有形參和實(shí)參之分,本文主要介紹了C++函數(shù)三種傳參形式(指針傳遞、引用傳遞、值傳遞),具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03C++ 自增、自減運(yùn)算符的重載和性能分析小結(jié)
這篇文章主要介紹了C++ 自增、自減運(yùn)算符的重載和性能分析小結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12重啟后nvidia-smi命令不可執(zhí)行出現(xiàn)“Make?sure?that?the?latest?NVIDIA?
這篇文章主要介紹了重啟后nvidia-smi命令不可執(zhí)行,出現(xiàn)“Make?sure?that?the?latest?NVIDIA?driver?is?installed?and?running.”問題,本文給大家分享最新完美解決方法,需要的朋友可以參考下2022-12-12C++中靜態(tài)庫與動(dòng)態(tài)庫的使用示例
在C/C++中使用庫的技術(shù),庫主要分為兩種類型:靜態(tài)庫和動(dòng)態(tài)庫,本文主要介紹了C++中靜態(tài)庫與動(dòng)態(tài)庫的使用示例,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09C++設(shè)計(jì)模式編程中簡(jiǎn)單工廠與工廠方法模式的實(shí)例對(duì)比
這篇文章主要介紹了C++設(shè)計(jì)模式編程中簡(jiǎn)單工廠與工廠方法模式的實(shí)例對(duì)比,文中最后對(duì)兩種模式的優(yōu)缺點(diǎn)總結(jié)也比較詳細(xì),需要的朋友可以參考下2016-03-03