300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > c语言版贪吃蛇《课程设计》

c语言版贪吃蛇《课程设计》

时间:2021-02-14 00:14:08

相关推荐

c语言版贪吃蛇《课程设计》

主要运用了光标的移动-.-

教程会在一个月内发布-.-最近考试好多......

看代码应该能看懂....

3.1 规则模块与动画模块

1)函数原型:void gui( );void dong( );

2)功 能:利用for循环语句和printf,COORD,Sleep()函数完成光标的移动和游戏规则的输出。

3)变量及类型:

COORD coord; 定义坐标结构体,通过它来移动光标的坐标。

int x,y,R,i; 定义x,y两个变量,用来传递坐标的位置。R为游戏高度。i为光标与边界距离。

4)说明:gui();通过for循环与光标的移动输出分割线,然后再通过光标的移动输出游戏规则;dong();通过for循环和i,x,y的变化和Sleep()的停顿效果,改变光标的位置,从外围到内部逐次输出,达到动画的效果。

3.2 游戏开始与结束选择模块和 游戏难度选择模块

1)函数原型:int kai( ); int xuan( );

2)功 能:利用for循环语句和printf,COORD函数完成对游戏屏幕左侧的清空,然后利用printf,scanf函数输出游戏的选择类型和输入玩家的选择。

3)变量及类型:

COORD coord; 定义坐标结构体,通过它来移动光标的坐标。

int x,y,lp,xx; 定义x,y两个变量,用来传递坐标的位置;lp里存入玩家的选择,是kai( )函数的返回值;xx是xuan( )函数的返回值。

4)说明:通过for循环和光标的移动,输出空格使屏幕为空,然后printf函数输出提示语,scanf函数让玩家输入他的选择,最后返回玩家的选择。

3.3 游戏主体模块

1)函数原型:int youxi(int xx,int bei);

2)功 能:利用for循环语句,while循环语句,if-else,if语句,break语句,结构体,一维,二维数组,printf,rand,kbhit,getch,COORD函数,完成整个游戏运行的功能。

3)变量及类型:

struct node{

int x,y;

}shen[1000]; 定义贪吃蛇的每个身体坐标。

COORD coord;定义坐标结构体,通过它来移动光标的坐标。

char map[60][30];定义游戏地图。

char xiang[4]={'w','a','s','d'}; 存放贪吃蛇的方向。

int x,y,i,R=50,D=24,ll=0,fen=0,p,wu=0,wux,wuy,fafe=0; 定义多个变量,x,y用来传递坐标的位置;i为变量;R,D 用来代表游戏右边界和下边界;ll表示贪吃蛇的长度;fen记录贪吃蛇所吃的食物;p记录贪吃蛇的方向;wux,wuy通过rand函数随机得到食物的位置;wu表示食物的状态,fafe表示游戏的状态。

4)说明:先通过for循环和光标的移动和printf清空游戏界面和设置边界,然后通过rand()函数得到第一个贪吃蛇身体和它的方向;for循环,增加贪吃蛇的3个身体;然后rand()使随机出现一个食物,一个while循环 + Sleep 的停顿效果使游戏暂停,printf输出提示语,kbhit和 getch 判断是否有键盘输入和得到键盘输入值,跳出while进入游戏操作while循环中,通过printf输出没时刻的分数,通过Sleep,结构体,for,printf使贪吃蛇每隔一段时间向前移动一下。kbhit和 getch 判断是否有游戏玩家的方向操作并得到方向值,如果输入空格游戏会暂停,然后可以选择游戏继续或退出。食物被贪吃蛇所吃会再次随机出现一个食物,while循环一直循环直至游戏失败或游戏退出返回游戏得分。

3.4 游戏失败提示模块

1)函数原型:void jiesu( );

2)功 能:利用for循环语句和printf,COORD函数完成对游戏屏幕左侧中间位置的清空,然后利用printf,scanf函数输出游戏失败提示语。

3)变量及类型:

COORD coord; 定义坐标结构体,通过它来移动光标的坐标。

int x,y,i,j; 定义x,y两个变量,用来传递坐标的位置;i,j 为循环变量。

4)说明:通过for循环和光标的移动,输出空格使屏幕为空,然后printf函数输出提示语。

代码:

#include<stdio.h>#include<string.h>#include<stdlib.h>#include<conio.h>#include<time.h> #include<windows.h>#define CX coord.X=x#define CY coord.Y=y#define SCP SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord)char map[60][30];struct node{int x,y;}shen[1000];char xiang[4]={'w','a','s','d'}; void dong();void gui();int kai();int youxi();void xiao();int main(){system("color 74");COORD coord;int x,y;gui();dong();while (1){int lp=kai();if (lp==2){printf("\n\n\n谢谢来玩贪吃蛇!!\n欢迎下次再玩-.-\n");return 0;}int kp=youxi();x=51;y=14;CX;CY;SCP;printf(" ");xiao();if (kp==-1){x=51;y=12;CX;CY;SCP;printf("游戏中退出-.-请重新选择...");}else{x=51;y=12;CX;CY;SCP;printf("本次游戏得分为:%d",kp*10);}}Sleep(100000);return 0;}void dong(){COORD coord;int x,y,i,j,R=24;coord.X=20;coord.Y=11;SCP;printf("贪吃蛇!"); for (i=0;i<10;i++){x=i;for (y=x;y<R-x;y++){coord.X=x*2;coord.Y=y;SCP;printf("0 ");}x=(23-i);for (y=i;y<=x;y++){coord.X=x*2;coord.Y=y;SCP;printf("0 "); }x=i;for (y=x;y<R-x;y++){coord.Y=x;coord.X=y*2;SCP;printf("0 ");}x=(23-i);for (y=i;y<=x;y++){coord.Y=x;coord.X=y*2;SCP;printf("0 ");}Sleep(100);}return ;}void gui(){COORD coord;int x=50,y;for (y=0;y<26;y++){CX;CY;SCP;printf("|");}x=52;y=0;CX;CY;SCP;printf("游戏规则:");x=52;y=2;CX;CY;SCP;printf("1. w a s d 键控制贪吃蛇的方向.");x=52;y=4;CX;CY;SCP;printf("2.当贪吃蛇碰到结界或自身会死亡.");x=52;y=6;CX;CY;SCP;printf("3.游戏中间按空格键会暂停.");x=52;y=8;CX;CY;SCP;printf("4.游戏暂停后,按'1'键游戏继续,按'0'键会回到刚开始游戏时的选择界面.");}int kai(){COORD coord;int x,y;for (x=0;x<50;x++)for (y=0;y<24;y++){CX;CY;SCP;printf(" ");}x=0;y=0;CX;CY;SCP;printf("1.游戏开始\n");printf("2.游戏结束\n");printf("请选择:");int lp;scanf("%d",&lp);return lp;}int youxi(){memset(map,0,sizeof(map));COORD coord;int x,y,i;for (x=0;x<50;x++)for (y=0;y<25;y++){CX;CY;SCP;printf(" ");}y=25;//蛇x为0-49,,y为0-24; int R=50,D=25;for (x=0;x<50;x++){CX;CY;SCP;printf("="); }int ll=0,fen=0;shen[ll].x=rand()%8+20;shen[ll++].y=rand()%6+10;x=shen[ll-1].x;y=shen[ll-1].y;CX;CY;SCP;map[x][y]='+';printf("+");int p=rand()%4;if (xiang[p]=='w'){for (i=0;i<3;i++){shen[ll].x=shen[ll-1].x;shen[ll].y=shen[ll-1].y-1;ll++;x=shen[ll-1].x;y=shen[ll-1].y;CX;CY;SCP;map[x][y]='+';printf("+");}}else if (xiang[p]=='a'){for (i=0;i<3;i++){shen[ll].x=shen[ll-1].x-1;shen[ll].y=shen[ll-1].y;ll++;x=shen[ll-1].x;y=shen[ll-1].y;CX;CY;SCP;map[x][y]='+';printf("+");}}else if (xiang[p]=='s'){for (i=0;i<3;i++){shen[ll].x=shen[ll-1].x;shen[ll].y=shen[ll-1].y+1;ll++;x=shen[ll-1].x;y=shen[ll-1].y;CX;CY;SCP;map[x][y]='+';printf("+");}}else if (xiang[p]=='d'){for (i=0;i<3;i++){shen[ll].x=shen[ll-1].x+1;shen[ll].y=shen[ll-1].y;ll++;x=shen[ll-1].x;y=shen[ll-1].y;CX;CY;SCP;map[x][y]='+';printf("+");}}int wux,wuy;int fafe=0;//用1和0模拟 bool 类型 int wu=0;char zhiling,kp;while (1){wux=rand()%50;wuy=rand()%25;if (map[wux][wuy]!='+'){x=wux;y=wuy;CX;CY;SCP;map[x][y]='#';printf("#");break;}}while (1){x=51;y=12;CX;CY;SCP;printf("按任意方向键开始游戏...");if(kbhit()){kp=getch();if (kp=='w'||kp=='a'||kp=='s'||kp=='d'){x=51;y=12;CX;CY;SCP;printf(" ");break;}}Sleep(100);}while (1){x=51;y=14;CX;CY;SCP;printf("得分:%d",fen*10);if(kbhit())//kbhit函数判断是否有键盘输入 zhiling=getch();if (zhiling!=xiang[p]){if (p<2&&zhiling!=xiang[p+2]||p>1&&zhiling!=xiang[p-2])//不然方向直接反向时,会直接死亡。。。 {if (zhiling=='w')p=0;else if (zhiling=='a')p=1;else if (zhiling=='s')p=2;else if (zhiling=='d')p=3;else if (zhiling==' '){zhiling='1'; while (1){if(kbhit()){kp=getch();if (kp=='1')break;else if (kp=='0'){return -1;}}Sleep(100);}}/*else if (zhiling==' ')//有点小难 {system("pause");while (kp=getch()){if (kp=='1')break;if (kp=='0')return; }}/*elsesystem("pause");*/}}if (xiang[p]=='w'){shen[ll].x=shen[ll-1].x;shen[ll].y=shen[ll-1].y-1;ll++;x=shen[ll-1].x;y=shen[ll-1].y;if (x==-1||x==R||y==-1||y==D||map[x][y]=='+'){fafe=1;break;}if (map[x][y]=='#'){fen++;wu=1;}CX;CY;SCP;map[x][y]='+';printf("+");if (!wu){x=shen[0].x;y=shen[0].y;CX;CY;SCP;map[x][y]=' ';printf(" ");for (i=0;i<ll-1;i++)shen[i]=shen[i+1];ll--;}}else if (xiang[p]=='a'){shen[ll].x=shen[ll-1].x-1;shen[ll].y=shen[ll-1].y;ll++;x=shen[ll-1].x;y=shen[ll-1].y;if (x==-1||x==R||y==-1||y==D||map[x][y]=='+'){fafe=1;break;}if (map[x][y]=='#'){fen++;wu=1;}CX;CY;SCP;map[x][y]='+';printf("+");if (!wu){x=shen[0].x;y=shen[0].y;CX;CY;SCP;map[x][y]=' ';printf(" ");for (i=0;i<ll-1;i++)shen[i]=shen[i+1];ll--;}}else if (xiang[p]=='s'){shen[ll].x=shen[ll-1].x;shen[ll].y=shen[ll-1].y+1;ll++;x=shen[ll-1].x;y=shen[ll-1].y;if (x==-1||x==R||y==-1||y==D||map[x][y]=='+'){fafe=1;break;}if (map[x][y]=='#'){fen++;wu=1;}CX;CY;SCP;map[x][y]='+';printf("+");if (!wu){x=shen[0].x;y=shen[0].y;CX;CY;SCP;map[x][y]=' ';printf(" ");for (i=0;i<ll-1;i++)shen[i]=shen[i+1];ll--;}}else if (xiang[p]=='d'){shen[ll].x=shen[ll-1].x+1;shen[ll].y=shen[ll-1].y;ll++;x=shen[ll-1].x;y=shen[ll-1].y;if (x==-1||x==R||y==-1||y==D||map[x][y]=='+'){fafe=1;break;}if (map[x][y]=='#'){fen++;wu=1;}CX;CY;SCP;map[x][y]='+';printf("+");if (!wu){x=shen[0].x;y=shen[0].y;CX;CY;SCP;map[x][y]=' ';printf(" ");for (i=0;i<ll-1;i++)shen[i]=shen[i+1];ll--;}}if (wu){while (1){wux=rand()%50;wuy=rand()%25;if (map[wux][wuy]!='+'){x=wux;y=wuy;CX;CY;SCP;map[x][y]='#';printf("#");break;}}wu=0;}if (fafe)break;Sleep(200);//sleep函数功能: 执行 挂起(暂停)一段时间分为sleep (秒) Sleep (毫秒) 和 usleep (微秒) 这点最重要 }return fen;}void xiao(){COORD coord;int x,y;for (x=0;x<50;x++)for (y=0;y<24;y++){CX;CY;SCP;printf(" ");}}

vc版-.-所有定义都要在最前面。。。

#include<stdio.h>#include<string.h>#include<memory.h>#include<stdlib.h>#include<conio.h>#include<time.h> #include<windows.h>#define CX coord.X=x#define CY coord.Y=y#define SCP SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord)char map[60][30];struct node{int x,y;}shen[1000];char xiang[4]={'w','a','s','d'}; void dong();void gui();int kai();int youxi();int xuan();void jiesu();int main(){COORD coord;int x,y,xx,lp,kp,nan[4]={300,400,200,100};system("color 74");gui();dong();while (1){lp=kai();if (lp==2){printf("\n\n\n谢谢来玩贪吃蛇!!\n欢迎下次再玩-.-\n");return 0;}xx=xuan();kp=youxi(nan[xx],xx);x=51;y=14;CX;CY;SCP;printf(" ");//消游戏中显示的得分... if (kp==-1){x=51;y=12;CX;CY;SCP;printf("游戏中退出-.-请重新选择...");}else{x=51;y=12;CX;CY;SCP;printf("本次游戏得分为:%d",kp*10*xx);}}Sleep(100000);return 0;}int xuan()//困难程度{COORD coord;int x,y,xx;for (x=0;x<50;x++)for (y=0;y<24;y++){CX;CY;SCP;printf(" ");}x=0;y=0;CX;CY;SCP;printf("请选择游戏困难程度:\n");printf("1.简单模式\n");printf("2.一般模式\n");printf("3.困难模式\n");scanf("%d",&xx);return xx;}void dong()//开始动画 {COORD coord;int x=20,y=11,i,R=24;CX;CY;SCP;printf("贪吃蛇!"); for (i=0;i<10;i++){x=i;for (y=x;y<R-x;y++){coord.X=x*2;coord.Y=y;SCP;printf("0 ");}x=(23-i);for (y=i;y<=x;y++){coord.X=x*2;coord.Y=y;SCP;printf("0 "); }x=i;for (y=x;y<R-x;y++){coord.Y=x;coord.X=y*2;SCP;printf("0 ");}x=(23-i);for (y=i;y<=x;y++){coord.Y=x;coord.X=y*2;SCP;printf("0 ");}Sleep(100);}return ;}void gui()//游戏规则{COORD coord;int x=50,y;for (y=0;y<26;y++){CX;CY;SCP;printf("|");}x=52;y=0;CX;CY;SCP;printf("游戏规则:");x=52;y=2;CX;CY;SCP;printf("1. w a s d 键控制贪吃蛇的方");y=3;CX;CY;SCP;printf("向.");x=52;y=4;CX;CY;SCP;printf("2.当贪吃蛇碰到结界或自身会");y=5;CX;CY;SCP;printf("死亡.");x=52;y=6;CX;CY;SCP;printf("3.游戏中间按空格键会暂停.");x=52;y=8;CX;CY;SCP;printf("4.游戏暂停后,按'1'键游戏继");y=9;CX;CY;SCP;printf("续,按'0'键会回到刚开始游戏");y=10;CX;CY;SCP;printf("时的选择界面.");}int kai()//开始&&结束{COORD coord;int x,y,lp;for (x=0;x<50;x++)for (y=0;y<24;y++){CX;CY;SCP;printf(" ");}x=0;y=0;CX;CY;SCP;printf("1.游戏开始\n");printf("2.游戏结束\n");printf("请选择:");scanf("%d",&lp);return lp;}int youxi(int xx,int bei)//游戏主体 {int x,y;char ma[60][30];COORD coord;inti,R=50,D=24,ll=0,fen=0,p,wu=0,wux,wuy,fafe=0;//fafe用1和0模拟 bool 类型,,,R是右边界 D是下边界.. char zhiling,kp;for (x=0;x<60;x++)for (y=0;y<30;y++)ma[x][y]=' ';for (x=0;x<50;x++)for (y=0;y<25;y++){CX;CY;SCP;printf(" ");}y=24;//蛇x为0-49,,y为0-23; for (x=0;x<50;x++){CX;CY;SCP;printf("="); }shen[ll].x=rand()%8+20;shen[ll++].y=rand()%6+10;x=shen[ll-1].x;y=shen[ll-1].y;CX;CY;SCP;map[x][y]='+';printf("+");p=rand()%4;if (xiang[p]=='w'){for (i=0;i<3;i++){shen[ll].x=shen[ll-1].x;shen[ll].y=shen[ll-1].y-1;ll++;x=shen[ll-1].x;y=shen[ll-1].y;CX;CY;SCP;map[x][y]='+';printf("+");}}else if (xiang[p]=='a'){for (i=0;i<3;i++){shen[ll].x=shen[ll-1].x-1;shen[ll].y=shen[ll-1].y;ll++;x=shen[ll-1].x;y=shen[ll-1].y;CX;CY;SCP;map[x][y]='+';printf("+");}}else if (xiang[p]=='s'){for (i=0;i<3;i++){shen[ll].x=shen[ll-1].x;shen[ll].y=shen[ll-1].y+1;ll++;x=shen[ll-1].x;y=shen[ll-1].y;CX;CY;SCP;map[x][y]='+';printf("+");}}else if (xiang[p]=='d'){for (i=0;i<3;i++){shen[ll].x=shen[ll-1].x+1;shen[ll].y=shen[ll-1].y;ll++;x=shen[ll-1].x;y=shen[ll-1].y;CX;CY;SCP;map[x][y]='+';printf("+");}}while (1){wux=rand()%R;wuy=rand()%D;if (map[wux][wuy]!='+'){x=wux;y=wuy;CX;CY;SCP;map[x][y]='#';printf("#");break;}}while (1){x=51;y=12;CX;CY;SCP;printf("按任意方向键开始游戏...");if(kbhit()){kp=getch();if (kp=='w'||kp=='a'||kp=='s'||kp=='d'){x=51;y=12;CX;CY;SCP;printf(" ");break;}}Sleep(100);}while (1){x=51;y=14;CX;CY;SCP;printf("得分:%d",fen*10*bei);if(kbhit())//kbhit函数判断是否有键盘输入 zhiling=getch();if (zhiling!=xiang[p]){if (p<2&&zhiling!=xiang[p+2]||p>1&&zhiling!=xiang[p-2])//不然方向直接反向时,会直接死亡。。。 {if (zhiling=='w')p=0;else if (zhiling=='a')p=1;else if (zhiling=='s')p=2;else if (zhiling=='d')p=3;else if (zhiling==' ')//暂停 {zhiling='1';//改变一下-.-不然一直暂停... while (1){if(kbhit()){kp=getch();if (kp=='1')break;else if (kp=='0'){return -1;}}Sleep(100);}}}}if (xiang[p]=='w'){shen[ll].x=shen[ll-1].x;shen[ll].y=shen[ll-1].y-1;ll++;x=shen[ll-1].x;y=shen[ll-1].y;if (x==-1||x==R||y==-1||y==D||map[x][y]=='+'){fafe=1;break;}if (map[x][y]=='#'){fen++;wu=1;}CX;CY;SCP;map[x][y]='+';printf("+");if (!wu){x=shen[0].x;y=shen[0].y;CX;CY;SCP;map[x][y]=' ';printf(" ");for (i=0;i<ll-1;i++)shen[i]=shen[i+1];ll--;}}else if (xiang[p]=='a'){shen[ll].x=shen[ll-1].x-1;shen[ll].y=shen[ll-1].y;ll++;x=shen[ll-1].x;y=shen[ll-1].y;if (x==-1||x==R||y==-1||y==D||map[x][y]=='+'){fafe=1;break;}if (map[x][y]=='#'){fen++;wu=1;}CX;CY;SCP;map[x][y]='+';printf("+");if (!wu){x=shen[0].x;y=shen[0].y;CX;CY;SCP;map[x][y]=' ';printf(" ");for (i=0;i<ll-1;i++)shen[i]=shen[i+1];ll--;}}else if (xiang[p]=='s'){shen[ll].x=shen[ll-1].x;shen[ll].y=shen[ll-1].y+1;ll++;x=shen[ll-1].x;y=shen[ll-1].y;if (x==-1||x==R||y==-1||y==D||map[x][y]=='+'){fafe=1;break;}if (map[x][y]=='#'){fen++;wu=1;}CX;CY;SCP;map[x][y]='+';printf("+");if (!wu){x=shen[0].x;y=shen[0].y;CX;CY;SCP;map[x][y]=' ';printf(" ");for (i=0;i<ll-1;i++)shen[i]=shen[i+1];ll--;}}else if (xiang[p]=='d'){shen[ll].x=shen[ll-1].x+1;shen[ll].y=shen[ll-1].y;ll++;x=shen[ll-1].x;y=shen[ll-1].y;if (x==-1||x==R||y==-1||y==D||map[x][y]=='+'){fafe=1;break;}if (map[x][y]=='#'){fen++;wu=1;}CX;CY;SCP;map[x][y]='+';printf("+");if (!wu){x=shen[0].x;y=shen[0].y;CX;CY;SCP;map[x][y]=' ';printf(" ");for (i=0;i<ll-1;i++)shen[i]=shen[i+1];ll--;}}if (wu){while (1){wux=rand()%R;wuy=rand()%D;if (map[wux][wuy]!='+'){x=wux;y=wuy;CX;CY;SCP;map[x][y]='#';printf("#");break;}}wu=0;}if (fafe)break;Sleep(xx);//sleep函数功能: 执行 挂起(暂停)一段时间分为sleep (秒) Sleep (毫秒) 和 usleep (微秒) 这点最重要 }jiesu();return fen;}void jiesu()//结束显示 {COORD coord;int xx=20,yy=11,x,y,i,j;for (i=0;i<6;i++){x=xx+i;for (j=0;j<3;j++){y=yy+j;CX;CY;SCP;printf(" ");}}x=20;y=11;CX;CY;SCP;printf(">_>游戏失败");Sleep(2000);}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。