贪吃蛇c语言代码.docx_第1页
贪吃蛇c语言代码.docx_第2页
贪吃蛇c语言代码.docx_第3页
贪吃蛇c语言代码.docx_第4页
贪吃蛇c语言代码.docx_第5页
已阅读5页,还剩20页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

贪吃蛇c语言代码#include #include #include #include #define NULL 0#define UP 18432#define DOWN 20480#define LEFT 19200#define RIGHT 19712#define ESC 283#define ENTER 7181 struct snakeint centerx; int centery; int newx; int newy; struct snake *next;struct snake *head; int grade=60; /*控制速度的*/int a,b; /* 背静遮的 位置*/void *far1,*far2,*far3,*far4; /* 蛇身指针 背静遮的指针 虫子*/int size1,size2,size3,size4; /* *全局变量*/int ch=RIGHT; /*存按键 开始蛇的方向为RIGHT*/int chy=RIGHT; int flag=0; /*判断是否退出游戏*/int control=4; /*判断上次方向和下次方向不冲突*/int nextshow=1; /*控制下次蛇身是否显示*/int scenterx; /*随即矩形中心坐标*/int scentery;int sx; /*在a b 未改变前 得到他们的值 保证随机矩形也不在此出现*/int sy; /*蛇身初始化*/void snakede() struct snake *p1,*p2; head=p1=p2=(struct snake *)malloc(sizeof(struct snake); p1-centerx=80; p1-newx=80; p1-centery=58; p1-newy=58; p1=(struct snake *)malloc(sizeof(struct snake); p2-next=p1; p1-centerx=58; p1-newx=58; p1-centery=58; p1-newy=58; p1-next=NULL;/*end*/void welcome() /*游戏开始界面,可以选择速度*/ int key; int size; int x=240; int y=300; int f; void *buf; setfillstyle(SOLID_FILL,BLUE); bar(98,100,112,125); setfillstyle(SOLID_FILL,RED); bar(98,112,112,114); setfillstyle(SOLID_FILL,GREEN); bar(100,100,110,125); size=imagesize(98,100,112,125); buf=malloc(size); getimage(98,100,112,125,buf); cleardevice(); setfillstyle(SOLID_FILL,BLUE); bar(240,300,390,325); outtextxy(193,310,speed:); setfillstyle(SOLID_FILL,RED); bar(240,312,390,314); setcolor(YELLOW); outtextxy(240,330,DOWN); outtextxy(390,330,UP); outtextxy(240,360,ENTER to start. ); outtextxy(270,200,SNAKE); fei(220,220); feiyang(280,220); yang(340,220); putimage(x,y,buf,COPY_PUT); setcolor(RED); rectangle(170,190,410,410); while(1) if(bioskey(1) /*8选择速度部分*/ key=bioskey(0); switch(key) case ENTER: f=1; break; case DOWN: if(x=240) putimage(x-=2,y,buf,COPY_PUT); grade+; key=0; break; case UP: if(xcenterx&scentery=p-centery|scenterx=sx&scentery=sy) show=0; jump=1; break; else p=p-next; if(jump=1) break; if(show=1) putimage(scenterx-11,scentery-11,far3,COPY_PUT); nextshow=0; break; /*过关动画*/void donghua() int i; cleardevice(); setbkcolor(BLACK); randomize(); while(1) for(i=0;i=5;i+) putpixel(random(640),random(80),13); putpixel(random(640),random(80)+80,2); putpixel(random(640),random(80)+160,3); putpixel(random(640),random(80)+240,4); putpixel(random(640),random(80)+320,1); putpixel(random(640),random(80)+400,14); setcolor(YELLOW); settextstyle(0,0,4); outtextxy(130,200,Wonderful!); setfillstyle(SOLID_FILL,10); bar(240,398,375,420); feiyang(300,400); fei(250,400); yang(350,400); if(bioskey(1) if(bioskey(0)=ESC) flag=1; break; /*end*/ /*初始化图形系统*/void init()int a=DETECT,b; int i,j; initgraph(&a,&b,);/*end*/ /*画立体边框效果函数*/void tline(int x1,int y1,int x2,int y2,int white,int black) setcolor(white); line(x1,y1,x2,y1); line(x1,y1,x1,y2); setcolor(black); line(x2,y1,x2,y2); line(x1,y2,x2,y2); /*end*/ /*飞洋标志*/int feiyang(int x,int y) int feiyang1818= 0,0,0,0,0,0,1,1,1,1,1,1,0,1,1,0,0,0, 0,0,0,0,0,1,1,1,0,0,1,1,1,1,1,0,0,0, 0,0,0,0,1,1,1,0,0,0,0,0,1,1,1,0,0,0, 0,0,0,1,1,1,0,0,0,0,0,0,0,1,1,0,0,0, 0,0,0,1,1,0,0,0,0,0,0,1,1,0,0,0,0,0, 0,0,1,1,0,0,0,0,0,0,1,1,1,0,0,0,0,0, 0,0,1,1,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,0,0,1, 0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,1,1, 0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0;int i,j;for(i=0;i=17;i+) for(j=0;j=17;j+) if (feiij=1) putpixel(j+x,i+y,BLUE); /*洋字*/ int yang(int x,int y)int yang1818=0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,1,0,0,0,0,1,1,1,0,0,0,1,1,0,0,0,0,0,1,1,1,0,0,0,1,1,1,0,1,1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,1,1,1,0,0,0,1,0,0,0,0,0,1,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,1,1,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,1,0,0,0,0,1,1,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,0,0,0,0,0,1,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0; int i,j; for(i=0;i=17;i+) for(j=0;jnewx-11,p1-newy-11,far1,COPY_PUT); p1=p1-next; /*end*/*蛇身刷新变化 游戏关键部分 */void snakechange()struct snake *p1,*p2,*p3,*p4,*p5; int i,j; static int n=0; static int score; static int left=20; char sscore5; char sleft1; p2=p1=head; while(p1!=NULL) p1=p1-next; if(p1-next=NULL) a=p1-newx; b=p1-newy; /*记录最后节点的坐标*/ sx=a; sy=b; p1-newx=p2-centerx; p1-newy=p2-centery; p2=p1; p1=head;while(p1!=NULL)p1-centerx=p1-newx; p1-centery=p1-newy; p1=p1-next; /*判断按键方向*/if(bioskey(1) ch=bioskey(0); if(ch!=RIGHT&ch!=LEFT&ch!=UP&ch!=DOWN&ch!=ESC) /*chy为上一次的方向*/ ch=chy; switch(ch) case LEFT: if(control!=4) head-newx=head-newx-22; head-centerx=head-newx; control=2; if(head-newxnewx=head-newx+22; head-centerx=head-newx; control=4; if(head-newx377) gameover(); chy=ch; break; case DOWN: if(control!=1) head-newy=head-newy+22; head-centery=head-newy; control=3; if(head-newy377) gameover(); else head-newy=head-newy-22; head-centery=head-newy; control=1; if(head-newynewx=head-newx+22; head-centerx=head-newx; control=4; if(head-newx377) gameover(); else head-newx=head-newx-22; head-centerx=head-newx; control=2; if(head-newxnewy=head-newy-22; head-centery=head-newy; control=1; if(head-newynewy=head-newy+22; head-centery=head-newy; control=3; if(head-newy377) gameover(); chy=ch; break; case ESC: flag=1; break; /* if 判断是否吃蛇*/if(flag!=1) if(head-newx=scenterx&head-newy=scentery) p3=head; while(p3!=NULL) p4=p3; p3=p3-next; p3=(struct snake *)malloc(sizeof(struct snake); p4-next=p3; p3-centerx=a; p3-newx=a; p3-centery=b; p3-newy=b; p3-next=NULL; a=500; b=500; putimage(409,62,far4

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论