c语言实现贪吃蛇.zip

  • 老牛99
    了解作者
  • C/C++
    开发工具
  • 3KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 0
    下载次数
  • 2018-09-09 19:32
    上传日期
用c语言实现了贪吃蛇游戏,实测可用,用的是visual C++软件
c语言实现贪吃蛇.zip
  • c语言实现贪吃蛇
  • 贪吃蛇加强版
  • snake.c
    8.4KB
内容介绍
#include<stdio.h> #include<windows.h> #include<stdlib.h> #include<time.h> #include<ctype.h> #pragma comment(lib,"winmm.lib") #define U 1 #define D 2 #define L 3 #define R 4 typedef struct SNAKE { int x; int y; struct SNAKE *nexthead; }snake; snake *head,*food;//蛇头指针,食物指针 snake *q;//备用检测变量 int score=0,add=10;//得分,每次吃到食物增加的分数 int sleeptime=200,status;//延时时间,方向状态参数 int endgamestatus=0;//游戏结束参数 int replay=0;//用于确定用户是否继续玩游戏的参数 int scoremax=0;//当前历史最高分 int scoremaxlast=0;//上次历史最高分 void Pos(int x,int y); void endgame(); void welcometogame(); void creatMap(); void initsnake(); void createfood(); void gamestart(); void snakemove(); void gamecircle(); void endgame(); void pause(); void cantcrosswall(); int historymax()//读历史最高分 { int max=0; FILE *fp; if((fp=fopen("d:\\exmp1.txt","r"))==NULL) { printf("cannot open file\n"); } fscanf(fp,"%d",&max); fclose(fp); return max; } void Writehistorymax(int s)//写历史最高分 { int max=0; FILE *fp; if((fp=fopen("d:\\exmp1.txt","w"))==NULL) { printf("cannot open file\n"); } fprintf(fp,"%d",s); fclose(fp); } void Pos(int x,int y) { 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;i<58;i=i+2)//横的 { Pos(i,0); printf("■"); Pos(i,26); printf("■"); } for(i=1;i<26;i++)//竖的 { Pos(0,i); printf("■"); Pos(56,i); printf("■"); } } void initsnake()//初始化蛇身,构建链表 { snake *tail; int i=0; tail=(snake*)malloc(sizeof(snake)); tail->x=24; tail->y=5; tail->nexthead=NULL; for(i=1;i<7;i++) { head=(snake*)malloc(sizeof(snake)); head->x=24+2*i; head->y=5; head->nexthead=tail; tail=head; } while(tail!=NULL)//从头开始,由头到尾 { Pos(tail->x,tail->y); printf("■"); tail=tail->nexthead; } } void cantcrosswall()//不能穿墙 { if(head->x==0||head->x>=56||head->y==0||head->y>=26) { endgamestatus=1; endgame(); } } int biteself()//判断是否咬到了自己 { snake *self; self=head->nexthead; while(self!=NULL) { if(head->x==self->x&&head->y==self->y) { return 1; } self=self->nexthead; } 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()%24+1; q=head; while(q!=NULL) { if(q->x==food_1->x&&q->y==food_1->y) { free(food_1); createfood();//递归调用,不断调用自身 } q=q->nexthead; } food=food_1; Pos(food->x,food->y); printf("■"); } void pause() { Sleep(200); while(1) { if(GetAsyncKeyState(VK_SPACE)) break; } } void snakemove()//蛇的移动,分为四个方向 { snake *nexttou; nexttou=(snake*)malloc(sizeof(snake)); if(status==U)//一开始写成status=U,弄得我忙了半天呐,注意是测试等于。 { nexttou->x=head->x; nexttou->y=head->y-1; nexttou->nexthead=head; head=nexttou; if(nexttou->x==food->x&&nexttou->y==food->y) { q=head; while(q!=NULL) { Pos(q->x,q->y); printf("■"); q=q->nexthead; } score=score+add; createfood(); } else { q=head; while(q->nexthead->nexthead!=NULL) { Pos(q->x,q->y); printf("■"); q=q->nexthead; } Pos(q->nexthead->x,q->nexthead->y); printf(" "); free(q->nexthead); q->nexthead=NULL; } } if(status==D) { nexttou->x=head->x; nexttou->y=head->y+1; nexttou->nexthead=head; head=nexttou; if(nexttou->x==food->x&&nexttou->y==food->y) { q=head; while(q!=NULL) { Pos(q->x,q->y); printf("■"); q=q->nexthead; } score=score+add; createfood(); } else { q=head; while(q->nexthead->nexthead!=NULL) { Pos(q->x,q->y); printf("■"); q=q->nexthead; } Pos(q->nexthead->x,q->nexthead->y); printf(" "); free(q->nexthead); q->nexthead=NULL; } } if(status==L) { nexttou->x=head->x-2; nexttou->y=head->y; nexttou->nexthead=head; head=nexttou; if(nexttou->x==food->x&&nexttou->y==food->y) { q=head; while(q!=NULL) { Pos(q->x,q->y); printf("■"); q=q->nexthead; } score=score+add; createfood(); } else { q=head; while(q->nexthead->nexthead!=NULL) { Pos(q->x,q->y); printf("■"); q=q->nexthead; } Pos(q->nexthead->x,q->nexthead->y); printf(" "); free(q->nexthead); q->nexthead=NULL; } } if(status==R) { nexttou->x=head->x+2; nexttou->y=head->y; nexttou->nexthead=head; head=nexttou; if(nexttou->x==food->x&&nexttou->y==food->y) { q=head; while(q!=NULL) { Pos(q->x,q->y); printf("■"); q=q->nexthead; } score=score+add; createfood(); } else { q=head; while(q->nexthead->nexthead!=NULL) { Pos(q->x,q->y); printf("■"); q=q->nexthead; } Pos(q->nexthead->x,q->nexthead->y); printf(" "); free(q->nexthead); q->nexthead=NULL; } } if(score>scoremax) { } } void gamecircle() { Pos(60,6); printf("Shift为加速键"); Pos(60,7); printf("Ctrl为减速键"); Pos(60,8); printf("空格键暂停"); Pos(60,9); printf("ESC键退出"); Pos(60,10); printf("不能穿墙或咬到自己"); Pos(60,11); printf("好好玩吧!"); status=R; while(1) { Pos(60,3); printf("得分:%d ",score); Pos(60,4); printf("食物分:%d",add); scoremax=historymax(); scoremaxlast=scoremax; Pos(60,5); printf("历史最高分:%d",scoremax); if(score>scoremax) { Writehistorymax(score); } if(GetAsyncKeyState(VK_UP)&&status!=D) { status=U; } if(GetAsyncKeyState(VK_DOWN)&&status!=U) { status=D; } if(GetAsyncKeyState(VK_LEFT)&&status!=R) { status=L; } if(GetAsyncKeyState(VK_RIGHT)&&status!=L) { status=R; } if(GetAsyncKeyState(VK_SHIFT)) { if(sleeptime>50) { sleeptime=sleeptime-30; add=add+2; if(sleeptime==320) { add=2; } } } if(GetAsyncKeyState(VK_CONTROL)) { if(sleeptime<350) { sleeptime=sleeptime+30; add=add-2; if(sleeptime==350) { add=1; } } } if(GetAsyncKeyState(VK_SPACE)) { pause(); } if(GetAsyncKeyState(VK_ESCAPE) )//按ESC键退出游戏 { endgamestatus=3; endgame(); } snakemove(); Sleep(sleeptime); if(biteself()==1)//判断是否咬到自己 { endgamestatus=2; endgame(); } cantcrosswall();//判断是否撞墙 if(replay==1) { system("cls"); replay=0; score=0; PlaySound("F:\\wav音乐\\心愿.wav",NULL,SND_FILENAME|SND_ASYNC|SND_LOOP); gamestart(); gamecircle(); } } } void endgame()//游戏结束 { int i; for(i=0;i<2;i++) { system("color 48"); Sleep(1000); system("color 0F"); Sleep(1000); } system("cls"); Pos(24,5); if(endgamestatus==1) { printf("对不起,你撞墙了\n"); } if(endgamestatus==2) { printf("对不起,你咬到自己了\n"); } if(endgamestatus==3) { printf("欢迎再次玩游戏\n"); } Pos(24,7); printf("你的得分是:%d",score); if(score>scoremaxlast) { Pos(24,8); printf("新成绩!恭喜你打破了历史记录!"); } Sleep(1000); Pos(24,8); printf("你想继续玩游戏吗,若是则按y键并按回车键,否则按其它键退出"); if(getchar()=='y'||getchar()=='Y') { replay=1; } else exit(0); } void welcometogame()//开始界面 { syst
评论
    相关推荐
    • snowglobe-src-viewer-2.0.0-r0.tar.gz
      国外的开源游戏引擎,能够承载上千人,属于社交类的游戏
    • 彩球射手.rar
      该游戏适合于平时娱乐之用,娱乐时只需将大于等于三个的连在一块就得分,直到最地层触底为止.
    • 是男人就下一百层SHY.rar
      制作的第一款休闲类的手机游戏,适合初学者参考
    • Java拼图游戏源码.rar
      Java拼图游戏源码,挺经典是一个源码,内附解压密码
    • UnrealEngine3.tar.gz
      虚幻引擎3(UnrealEngine3)源代码,包含编辑器和引擎部分的源代码。比较完整。
    • Sudoku.zip
      数独题集 分上下册,上册为120道数独题,下册为答案 提高智力的最简单方式
    • Engine_v0.2.tar.gz
      一个巨cool的3D游戏引擎源代码。其0.3版本开始收费,卖10000美金,其功能有bsp/portal,particle system,physics,skelet-animation,energe unit,lod.四叉树/八叉树,shaders(glsl/hlsl)
    • tiaoqi.zip
      跳棋游戏
    • com_comprofiler.zip
      最受欢迎的Joomla社区用户管理收费插件 - Commnity Builder 1.2 RC2。 Community Builder suite (CB) extends the Joomla! user management system. Key features: extra fields in profile, enhanced registration workflows, user lists, connection paths between users, admin defined tabs and user profiles, image upload, front-end workflow management, integration with other components, like PMS, Newsletter, Forum, Galleries. Release 1.1 is compatible with all Joomla! 1.0.x (including 1.0.15) and Mambo 4.5.2-4.6.4. New CB version 1.2 RC brings Joomla 1.5 native compatibility (and keeping Joomla 1.0.x and Mambo 4.5.2-4.6.4 native compatibility), multi-criteria user searches, full field controls of core fields, ajax email and username checkers, new lost username/password form and many more features and fixes. Please see announcement on our homepage.
    • fchess_src.zip
      供两个人网上对弈的网络五子棋源码