当前位置:网站首页 > 游戏 >

C语言实现小游戏

来源:www.999sf.com | 编辑:999搜服 | 发布时间:2016-06-08 23:03

小编导读:

这个C游戏是我的一个同学让我帮他朋友写的,因为一直催我,昨天下午叫我帮的忙,让我赶紧帮他朋友写完。所以昨晚花了几个小时帮他写的。大晚上的...

/************************************************************************/ /* @author mero */ /* @data 2016.6.8 */ /* @version 1.0 */ /************************************************************************/ #include <stdio.h> #include <stdlib.h> #include <time.h> #include <windows.h> //结构体config用来配置机会次数,题量大小 struct config{ int chance;//用来存储每道题目机会次数 int topicNum;//用来存储题目数量 }cfg[4],*p; /*结构体award用于保存奖励语*/ struct award{ char good[30]; char well[20]; char cheers[20]; char nice[20]; char fine[20]; }award1; int main(void){ /*------------------------函数声明部分-------------------------*/ int getRandom(int,int);//得到随机数函数 int add(int,int); //加法函数 int mul(int,int); //乘法函数 void goto_xy(int,int); //定位光标到指定坐标 /*------------------------业务逻辑区---------------------------*/ printf("\n\t\t\t\t 小学生算术游戏v1.0\n"); printf("\n\t\t\t\t*********************\n\n"); printf("\t\t\t\t\t[1]加法\n"); printf("\t\t\t\t\t[2]减法\n"); printf("\t\t\t\t\t[3]乘法\n"); printf("\t\t\t\t\t[4]除法\n"); printf("\t\t\t\t\t[5]设置题量大小\n"); printf("\t\t\t\t\t[6]设置答题机会\n"); printf("\t\t\t\t\t[0]退出\n"); printf("\n\t\t\t\t*********************\n\n"); //初始配置数据 struct config cfg[4]={{3,5},{3,5},{3,5},{3,5}}; p=cfg; struct award award1={"恭喜你,奖励一根棒棒糖","非常棒,太聪明啦","不错哦!加油","干得漂亮","运气真好"}; int selector; for (;;) { printf("\t\t\t\t请输入选项[ ]\n"); HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); COORD coordScreen = {0, 0}; //光标位置 CONSOLE_SCREEN_BUFFER_INFO csbi; GetConsoleScreenBufferInfo(hConsole, &csbi); //printf("光标坐标:(%d,%d)\n", csbi.dwCursorPosition.X, csbi.dwCursorPosition.Y); goto_xy(43,csbi.dwCursorPosition.Y-1); scanf("%d",&selector); if (selector>=0&&selector<7) { switch(selector){ case 0: printf("\t\t-----------正在退出游戏中-----------\n"); printf("\t\t************************************\n"); printf("\t\t\t\t------正在恢复原始状态--------\n"); printf("\t\t\t\t------------请稍后------------\n"); int j; for(j=0;j<4;j++){ p->chance=3; p->topicNum=5; } printf("\t\t\t\t------------已恢复------------\n"); printf("\t\t--------游戏已退出,重新开始--------\n"); printf("\t\t************************************\n"); break; case 1: printf("\t\t\t\t欢迎进入加法游戏中\n"); int i; int result; for(i=0;i<p->topicNum;i++){ srand(time(NULL)); int a = getRandom(1,100); int b = getRandom(1,100); //产生结果 int addResult=add(a,b); printf("\t\t\t\t当前正在做第%d题,请输入 \n",i+1); printf("\t\t\t\t%d+%d=",a,b); scanf("%d",&result); printf("\t\t\t------当前输入的数为%d-------\n",result); if(result==addResult){ printf("\t\t\t恭喜你做对了,正在加载下一题\n"); int hah=getRandom(1,5); switch(hah){ case 1: printf("\t\t\t\t%s\n",award1.good); break; case 2: printf("\t\t\t\t%s\n",award1.well); break; case 3: printf("\t\t\t\t%s\n",award1.nice); break; case 4: printf("\t\t\t\t%s\n",award1.cheers); break; case 5: printf("\t\t\t\t%s\n",award1.fine); break; } }else{ printf("\t\t\t“再试一次!”\n"); int j; for (j=0;j<p->chance-1;j++) { printf("\t\t\t\t请输入 "); scanf("%d",&result); printf("\t\t\t------当前输入的数为%d-------\n",result); if (result==addResult) { printf("\t\t\t恭喜你做对了,正在加载下一题\n"); int hah=getRandom(1,5); switch(hah){ case 1: printf("\t\t\t\t%s\n",award1.good); break; case 2: printf("\t\t\t\t%s\n",award1.well); break; case 3: printf("\t\t\t\t%s\n",award1.nice); break; case 4: printf("\t\t\t\t%s\n",award1.cheers); break; case 5: printf("\t\t\t\t%s\n",award1.fine); break; } break; } else{ printf("\t\t\t“再试一次!”\n"); } } printf("\t\t\t\t第%d题正确答案是%d\n",i+1,addResult); } } break; case 2: printf("\t\t\t\t欢迎进入减法游戏中\n"); int g; int result1; for(g=0;g<(p+1)->topicNum;g++){ int a,b; for(;;){ srand(time(NULL)); a=getRandom(1,100); b=getRandom(1,100); if(a>b){ break; } } //产生结果 int subResult=a-b; printf("\t\t\t\t当前正在做第%d题,请输入 \n",g+1); printf("\t\t\t\t%d-%d=",a,b); scanf("%d",&result1); printf("\t\t\t------当前输入的数为%d-------\n",result1); if(result1==subResult){ printf("\t\t\t恭喜你做对了,正在加载下一题\n"); int hah=getRandom(1,5); switch(hah){ case 1: printf("\t\t\t\t%s\n",award1.good); break; case 2: printf("\t\t\t\t%s\n",award1.well); break; case 3: printf("\t\t\t\t%s\n",award1.nice); break; case 4: printf("\t\t\t\t%s\n",award1.cheers); break; case 5: printf("\t\t\t\t%s\n",award1.fine); break; } }else{ printf("\t\t\t“再试一次!”\n"); int v; for (v=0;v<(p+1)->chance-1;v++) { printf("\t\t\t\t请输入 "); scanf("%d",&result1); printf("\t\t\t------当前输入的数为%d-------\n",result1); if (result1==subResult) { printf("\t\t\t恭喜你做对了,新开传奇网站,正在加载下一题\n"); int hah=getRandom(1,5); switch(hah){ case 1: printf("\t\t\t\t%s\n",award1.good); break; case 2: printf("\t\t\t\t%s\n",award1.well); break; case 3: printf("\t\t\t\t%s\n",award1.nice); break; case 4: printf("\t\t\t\t%s\n",award1.cheers); break; case 5: printf("\t\t\t\t%s\n",award1.fine); break; } break; } else{ printf("\t\t\t“再试一次!”\n"); } } printf("\t\t\t\t第%d题正确答案是%d\n",g+1,subResult); } } break; case 3: printf("\t\t\t\t欢迎进入乘法游戏中\n"); int m; int result2; for(m=0;m<(p+2)->topicNum;m++){ srand(time(NULL)); int a = getRandom(1,100); int b = getRandom(1,100); //产生结果 int mulResult=mul(a,b); printf("\t\t\t\t当前正在做第%d题,请输入 \n",m+1); printf("\t\t\t\t%d*%d=",a,b); scanf("%d",&result2); printf("\t\t\t------当前输入的数为%d-------\n",result2); if(result2==mulResult){ printf("\t\t\t恭喜你做对了,正在加载下一题\n"); int hah=getRandom(1,5); switch(hah){ case 1: printf("\t\t\t\t%s\n",award1.good); break; case 2: printf("\t\t\t\t%s\n",award1.well); break; case 3: printf("\t\t\t\t%s\n",award1.nice); break; case 4: printf("\t\t\t\t%s\n",award1.cheers); break; case 5: printf("\t\t\t\t%s\n",award1.fine); break; } }else{ printf("\t\t\t“再试一次!”\n"); int n; for (n=0;n<(p+2)->chance-1;n++) { printf("\t\t\t\t请输入 "); scanf("%d",&result2); printf("\t\t\t------当前输入的数为%d-------\n",result2); if (result2==mulResult) { printf("\t\t\t恭喜你做对了,正在加载下一题\n"); int hah=getRandom(1,5); switch(hah){ case 1: printf("\t\t\t\t%s\n",award1.good); break; case 2: printf("\t\t\t\t%s\n",award1.well); break; case 3: printf("\t\t\t\t%s\n",award1.nice); break; case 4: printf("\t\t\t\t%s\n",award1.cheers); break; case 5: printf("\t\t\t\t%s\n",award1.fine); break; } break; } else{ printf("\t\t\t“再试一次!”\n"); } } printf("\t\t\t\t第%d题正确答案是%d\n",m+1,mulResult); } } break; case 4: printf("\t\t\t\t欢迎进入除法游戏中\n"); int mm; int result3; for(mm=0;mm<(p+3)->topicNum;mm++){ int a,b; for(;;){ srand(time(NULL)); a=getRandom(1,100); b=getRandom(1,100); if(a%b==0){ break; } } //产生结果 int divideResult=a/b; printf("\t\t\t\t当前正在做第%d题,请输入 \n",mm+1); printf("\t\t\t\t%d/%d=",a,b); scanf("%d",&result3); printf("\t\t\t------当前输入的数为%d-------\n",result3); if(result3==divideResult){ printf("\t\t\t恭喜你做对了,正在加载下一题\n"); int hah=getRandom(1,5); switch(hah){ case 1: printf("\t\t\t\t%s\n",award1.good); break; case 2: printf("\t\t\t\t%s\n",award1.well); break; case 3: printf("\t\t\t\t%s\n",award1.nice); break; case 4: printf("\t\t\t\t%s\n",award1.cheers); break; case 5: printf("\t\t\t\t%s\n",award1.fine); break; } }else{ printf("\t\t\t“再试一次!”\n"); int nn; for (nn=0;nn<(p+3)->chance-1;nn++) { printf("\t\t\t\t请输入 "); scanf("%d",&result3); printf("\t\t\t------当前输入的数为%d-------\n",result3); if (result3==divideResult) { printf("\t\t\t恭喜你做对了,正在加载下一题\n"); int hah=getRandom(1,5); switch(hah){ case 1: printf("\t\t\t\t%s\n",award1.good); break; case 2: printf("\t\t\t\t%s\n",award1.well); break; case 3: printf("\t\t\t\t%s\n",award1.nice); break; case 4: printf("\t\t\t\t%s\n",award1.cheers); break; case 5: printf("\t\t\t\t%s\n",award1.fine); break; } break; } else{ printf("\t\t\t“再试一次!”\n"); } } printf("\t\t\t\t第%d题正确答案是%d\n",mm+1,divideResult); } } break; case 5: printf("\t\t\t\t------请输入题量大小----\n"); scanf("%d%d%d%d",&cfg[0].topicNum,&cfg[1].topicNum,&cfg[2].topicNum,&cfg[3].topicNum); printf("\t\t\t\t你已修改加减乘除题量大小为%d %d %d %d\n",cfg[0].topicNum,cfg[1].topicNum,cfg[2].topicNum,cfg[3].topicNum); /*-----------------修改题量大小---------------*/ break; case 6: printf("\t\t\t\t请输入要修改的加减乘除游戏机会次数\n"); scanf("%d%d%d%d",&cfg[0].chance,&cfg[1].chance,&cfg[2].chance,&cfg[3].chance); printf("\t\t\t\t你已修改加减乘除题量大小为%d %d %d %d\n",cfg[0].chance,cfg[1].chance,cfg[2].chance,cfg[3].chance); /*-----------------修改机会次数---------------*/ break; } }else{ printf("对不起,请输入正确选项\n"); } } return 0; } /*-----------------------函数实现部分-------------------------*/ //获取随机数函数 int getRandom(int a,int b){ int ran; ran = rand()%(b-a+1)+a; return ran; } //加法函数 int add(int a,int b){ int sum=0; sum = a+b; return sum; } //乘法函数 int mul(int a,int b){ int mulResult; mulResult=a*b; return mulResult; } void goto_xy(int x,int y){ //定位光标位置到指定坐标 HANDLE hOut; hOut = GetStdHandle(STD_OUTPUT_HANDLE); COORD pos={x,y}; SetConsoleCursorPosition(hOut,pos); }


程序员的思维,呵呵!代码很简明。也没写成头文件形式,全部放在一个文件中,方便。

文章-C语言实现小游戏,是由http://999sf.com提供,转载请注明版权出处!

上一篇:女主则调侃回应:“有考虑下给点广告费么安慰下我们这些失足老公的妇女么?” 知名游戏人火狼(微博)也对此评论

下一篇:大部分厂商观望和怀疑的态度