基于VC的贪吃蛇游戏的C语言代码

上传人:QQ15****706 文档编号:52446794 上传时间:2018-08-21 格式:DOCX 页数:11 大小:35.10KB
返回 下载 相关 举报
基于VC的贪吃蛇游戏的C语言代码_第1页
第1页 / 共11页
基于VC的贪吃蛇游戏的C语言代码_第2页
第2页 / 共11页
基于VC的贪吃蛇游戏的C语言代码_第3页
第3页 / 共11页
基于VC的贪吃蛇游戏的C语言代码_第4页
第4页 / 共11页
基于VC的贪吃蛇游戏的C语言代码_第5页
第5页 / 共11页
点击查看更多>>
资源描述

《基于VC的贪吃蛇游戏的C语言代码》由会员分享,可在线阅读,更多相关《基于VC的贪吃蛇游戏的C语言代码(11页珍藏版)》请在金锄头文库上搜索。

1、#include #include #include #include#define U 1 #define D 2 #define L 3 #define R 4 /蛇的状态,U:上 ;D:下;L:左 R:右typedef struct SNAKE /蛇身的一个节点 int x;int y;struct SNAKE *next; snake;/全局变量/ int score=0,add=10;/总得分与每次吃食物得分。 int status,sleeptime=200;/每次运行的时间间隔 snake *head, *food;/蛇头指针,食物指针 snake *q;/遍历蛇的时候用到的指针

2、 int endgamestatus=0; /游戏结束的情况,1:撞到墙;2:咬到自己;3: 主动退出游戏。/声明全部函数/ void Pos(); void creatMap(); void initsnake(); int biteself(); void createfood(); void cantcrosswall(); void snakemove(); void pause(); void gamecircle(); void welcometogame(); void endgame(); void gamestart();void Pos(int x,int y)/设置光标位

3、置 COORD pos; HANDLE hOutput;pos.X=x;pos.Y=y;hOutput=GetStdHandle(STD_OUTPUT_HANDLE);SetConsoleCursorPosition(hOutput,pos); void creatMap()/创建地图 int i;for(i=0;ix=24;tail-y=5;tail-next=NULL;for(i=1;inext=tail;head-x=24+2*i;head-y=5;tail=head;while(tail!=NULL)/从头到为,输出蛇身Pos(tail-x,tail-y);printf(“);tail

4、=tail-next; int biteself()/判断是否咬到了自己 snake *self;self=head-next;while(self!=NULL)if(self-x=head-x self=self-next;return 0; void createfood()/随机出现食物 snake *food_1;srand(unsigned)time(NULL);food_1=(snake*)malloc(sizeof(snake);while(food_1-x%2)!=0) /保证其为偶数,使得食物能与蛇头对其food_1-x=rand()%52+2;food_1-y=rand()

5、%24+1;q=head;while(q-next=NULL)if(q-x=food_1-x createfood();q=q-next;Pos(food_1-x,food_1-y);food=food_1;printf(“);void cantcrosswall()/不能穿墙 if(head-x=0 | head-x=56 |head-y=0 | head-y=26)endgamestatus=1;endgame(); void snakemove()/蛇前进,上 U,下 D,左 L,右 R snake * nexthead;cantcrosswall();nexthead=(snake*)

6、malloc(sizeof(snake);if(status=U)nexthead-x=head-x;nexthead-y=head-y-1;if(nexthead-x=food-x head=nexthead;q=head;while(q!=NULL)Pos(q-x,q-y);printf(“);q=q-next;score=score+add;createfood();else /如果没有食 物/nexthead-next=head;head=nexthead;q=head;while(q-next-next!=NULL)Pos(q-x,q-y);printf(“);q=q-next; P

7、os(q-next-x,q-next-y);printf(“ “);free(q-next);q-next=NULL;if(status=D)nexthead-x=head-x;nexthead-y=head-y+1;if(nexthead-x=food-x head=nexthead;q=head;while(q!=NULL)Pos(q-x,q-y);printf(“);q=q-next;score=score+add;createfood();else /没有食物nexthead-next=head;head=nexthead;q=head;while(q-next-next!=NULL)

8、Pos(q-x,q-y);printf(“);q=q-next; Pos(q-next-x,q-next-y);printf(“ “);free(q-next);q-next=NULL;if(status=L)nexthead-x=head-x-2;nexthead-y=head-y;if(nexthead-x=food-x head=nexthead;q=head;while(q!=NULL)Pos(q-x,q-y);printf(“);q=q-next;score=score+add;createfood();else /没有食物nexthead-next=head;head=nexthe

9、ad;q=head;while(q-next-next!=NULL)Pos(q-x,q-y);printf(“);q=q-next; Pos(q-next-x,q-next-y);printf(“ “);free(q-next);q-next=NULL;if(status=R)nexthead-x=head-x+2;nexthead-y=head-y;if(nexthead-x=food-x head=nexthead;q=head;while(q!=NULL)Pos(q-x,q-y);printf(“);q=q-next;score=score+add;createfood();else /

10、没有食物nexthead-next=head;head=nexthead;q=head;while(q-next-next!=NULL)Pos(q-x,q-y);printf(“);q=q-next; Pos(q-next-x,q-next-y);printf(“ “);free(q-next);q-next=NULL;if(biteself()=1) /判断是否会咬到自己endgamestatus=2;endgame(); void pause()/暂停 while(1)Sleep(300);if(GetAsyncKeyState(VK_SPACE)break; void gamecircl

11、e()/控制游戏 Pos(64,15);printf(“不能穿墙,不能咬到自己n“);Pos(64,16);printf(“用.分别控制蛇的移动.“);Pos(64,17);printf(“F1 为加速,F2 为减速n“);Pos(64,18);printf(“ESC :退出游戏.space:暂停游戏.“);Pos(64,20); printf(“C 语言研究中心 www.clang.cc“); status=R;while(1)Pos(64,10);printf(“得分:%d “,score);Pos(64,11);printf(“每个食物得分:%d 分“,add);if(GetAsyncK

12、eyState(VK_UP) else if(GetAsyncKeyState(VK_DOWN) else if(GetAsyncKeyState(VK_LEFT)else if(GetAsyncKeyState(VK_RIGHT)else if(GetAsyncKeyState(VK_SPACE)pause();else if(GetAsyncKeyState(VK_ESCAPE)endgamestatus=3;break;else if(GetAsyncKeyState(VK_F1)if(sleeptime=50)sleeptime=sleeptime-30;add=add+2;if(sl

13、eeptime=320)add=2;/防止减到 1 之后再加回来有错else if(GetAsyncKeyState(VK_F2)if(sleeptime350)sleeptime=sleeptime+30;add=add-2;if(sleeptime=350)add=1; /保证最低分为 1Sleep(sleeptime);snakemove(); void welcometogame()/开始界面 Pos(40,12);system(“title C 语言研究中心 www.clang.cc“);printf(“欢迎来到贪食蛇游戏!“);Pos(40,25);printf(“ C 语言研究中

14、心 www.clang.cc.n“);system(“pause“);system(“cls“);Pos(25,12);printf(“用.分别控制蛇的移动, F1 为加速,2 为减速n“);Pos(25,13);printf(“加速将能得到更高的分数。n“);system(“pause“);system(“cls“); void endgame()/结束游戏 system(“cls“);Pos(24,12);if(endgamestatus=1)printf(“对不起,您撞到墙了。游戏结束.“);else if(endgamestatus=2)printf(“对不起,您咬到自己了。游戏结束.“);else if(endgamestatus=3)printf(“您的已经结束了游戏。“);Pos(24,13);printf(“您的得分是%dn“,score);exit(0); void gamestart()/游戏初始化 system(“mode con cols=100 lines=30“);welcometogame();creatMap();initsnake();createfood(); int main() gamestart();gamecircle();endgame(); r

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 学术论文 > 毕业论文

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号