版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、贪吃蛇编写思路及C语言源码规则:每吃上一个点,就长大一点,不能撞墙或者撞上自己身体。教训:在有限的空间里,可以贪吃,但是要注意安全哦。这个游戏是我刚工作一年的时候写的,当时对C语言有了些了解,想做点东西,刚好看到同事的手机上有这个游戏(那时我还没有手机呢,呵呵),觉得挺好玩,要实现的话也还比较简单,就在电脑上用TC上做出来了。刚做出来的时候,那种喜悦,确实不是玩别人的游戏所能带来的。这个程序是入门级别的,适合编程刚入门的朋友们试一试。高手达人们请跳过。下面我简单说一下我的思路,理解下面几点会比较清楚一些。1,基础:你首先要能画出一个带颜色的方块。举一反三:可以画一个就可以画很多个了。(TC中画
2、方块用到了EGAVGA.BGI这个文件)2,移动:一个方块消失,相邻地方一个方块出现,在视觉上就是移动了。3,消失:用背景颜色在同样的地方画同样大小的方块。4,相对坐标:视觉上像素这个单位太小,用方块的大小作为相对坐标的单位。5,随机点:使用伪随机函数,参数一般用上系统当前时间,你再随意捏造个四则运算,就会产生出独一无二的随机数了。6,链表:这个是精髓啊,你看那蛇不是就像一个链表吗,这个可是我认为在这个游戏中使用的最高深的结构了,呵呵。7,长大:链表头遇上一个食物(随机产生的方块),链表上添加一个节点。8,死亡:链表头撞上了自身或者撞墙。也就这么多,理解了这几点,整个框架也就出来了。下面就是源
3、代码:#include <stdio.h>#include <graphics.h>#include <stdlib.h>#include <ctype.h>#define LEN sizeof(struct list)#define uchar unsigned charint n,sco,r,t,speed=8800;uchar i,a,x1,y1,hit,long1=16,cycy=0,str1200,xisu;struct list int x,y; struct list * next,*
4、 last;struct list * p1,* p2,* head,* eof1;void up(void); void down();void left(); void right();v
5、oid unit(uchar,uchar,uchar); void score();void dl(int); void save1();void load1(); void save();
6、main() int driver=VGA,mode=VGAHI;unsigned char ss8;initgraph(&driver,&mode,"c:tc");setfillstyle(1,8);setcolor(8);bar(2,2,600,400);setfillstyle(1,7); setcolor(7); bar(117,117,170,140);setfillstyle(1,8); setcolor(8);bar(118,
7、118,170,140);setcolor(14);/* settextstyle(2,0,5);*/sprintf(ss,"enter");outtextxy(120,125,ss);setcolor(0);line(118,141,171,141);line(118,142,172,142);line(171,118,171,141);line(172,118,172,142);setfillstyle(1,14);setcolor(14);circle(15,15,10); sprintf(ss," fast");
8、; /* three statuses,select use 'Tab' */outtextxy(30,15,ss); circle(15,55,10); sprintf(ss," normol");outtextxy(30,55,ss); circle(15,95,10); sprintf(ss," slow");outtextxy(30,95,ss);cir
9、cle(15,15,6);floodfill(15,15,14);xisu=0;do a=getch();if(a=9) /* Tab */ setcolor(14);setfillstyle(1,14); a=8; xisu+;
10、60; circle(15,15+(xisu%3)*40,6); floodfill(15,15+(xisu%3)*40,14); setfillstyle(1,7); setcolor(7); circle(15,15+(xisu-1)%3)*40,6); floodfill(15,15+(xisu-1)%3)*40,7);if(a=76)|(a=108)
11、; /* 'L'or'l' load the status of last time */ load1(); xisu=2; goto LOOP; while(a!=13);setcolor(8);line(118,141,171,141); line(171,118,171,141);dl(speed);p2=(struct list *)malloc(L
12、EN); hit=1; /* head of snake */ p1=p2; head=p2; p2->x=20;p2->y=20; p2->last=NULL; p2->next=NULL;rectangle(0,0,600,450);setfillstyle(1,0); setcol
13、or(0);bar(1,1,599,449); setfillstyle(1,14);setcolor(14);speed=speed*(xisu%3+1);for(i=0;i<15;i+)
14、0; /* 16 units when creating */ p2=(struct list *)malloc(LEN);p2->x=21+i;p2->y=20; unit(21+i,20,14); p1->next=p2; p2->last=p1; p2->next=NULL;eof1=p2;p1=p
15、2;unit(head->x,head->y,14);/*srand(100);*/LOOP: while(1) if(hit=1) /* create a random unit */ randomize(); x1=random(30); y1=random(20); x1=x1+3; y1=y1+3; p2=head; p1=
16、p2; while(p1->next!=NULL) if(x1=p2->x)&&(y1=p2->y) /* make the unit not in the snake */ x1=random(30)+3;y1=random(20)+3;p2=head;p1=p2; /* if in,new */ else
17、; p1=p2; p2=p1->next; unit(x1,y1,14); hit=0;if(kbhit()!=0) a=getch();switch(a)
18、 /* do with key-down */ case 72 : up(); break; case 80 : down(); break; case 75 : left(); break; case 77 : right(); break; case 83 : case
19、 115: dl(500); save1(); break; /*'S'or's' save status and exit */if(cycy=1) break;do a=getch();while(a>14)|(a<13); /* while 'Enter',exit */closegraph();void up(void) p2=head->next;p1=p2;while(p1->next!=NU
20、LL) if(head->x=p2->x)&&(head->y-1=p2->y) /* if hit self ,out */ score(); break;else p1=p2; p2=p1->next;if(hit=0)&&(head->x=x1)&&(head->y-1=y1) /* if hit new unit ,add */ p2=(struct list *
21、)malloc(LEN); p2->x=x1;p2->y=y1;p2->next=head;head->last=p2;head=p2; hit=1;long1+;else p2=eof1;eof1=p2->last;/*p2->last=NULL;*/eof1-
22、>next=NULL; /* eof1 disappear,head add 1 unit */unit(p2->x,p2->y,0);p2->x=head->x;p2->y=head->y-1;p2->next=head; head->last=p2;head=p2; head->last=NULL;unit(p2->x,p2->y,14);if(p2->y<1) score(); dl(speed);void down(vo
23、id) p2=head->next;p1=p2;while(p1->next!=NULL) if(head->x=p2->x)&&(head->y+1=p2->y) /* hit self ,out */ score(); break;else p1=p2; p2=p1->next;if(hit=0)&&(head->x=x1)&&(head->y+1=y1) /* hi
24、t unit ,add */p2=(struct list *)malloc(LEN); p2->x=x1;p2->y=y1; p2->next=head;head->last=p2;head=p2; hit=1;long1+;else p2=eof1;eof1=p2->last;eof1->next=NULL;unit(p2->x,p2->y,0);p2->next=head;head->l
25、ast=p2; p2->x=head->x;p2->y=head->y+1;head=p2; head->last=NULL;unit(p2->x,p2->y,14);if(p2->y>=30) /* hit wall ,out */ score();dl(
26、speed);void left(void) p2=head->next;p1=p2;while(p1->next!=NULL) if(head->x-1=p2->x)&&(head->y=p2->y) score(); break;else p1=p2; p2=p1->next;if(hit=0)&&(head->x-1=x1)&&(head->y=y1)
27、p2=(struct list *)malloc(LEN); p2->x=x1;p2->y=y1; p2->next=head;head->last=p2;head=p2; hit=1;long1+;else p2=eof1;eof1=p2->last;eo
28、f1->next=NULL;unit(p2->x,p2->y,0);p2->next=head;head->last=p2; p2->x=head->x-1;p2->y=head->y;head=p2; head->last=NULL;unit(p2->x,p2->y,14);if(p2->x<1) score();
29、60; dl(speed);void right(void) p2=head->next;p1=p2;while(p1->next!=NULL) if(head->x+1=p2->x)&&(head->y=p2->y) score(); break;else p1=p2; p2=p1->next;if(hit=0)&&(head->x+1=x1)&&(head-&g
30、t;y=y1) p2=(struct list *)malloc(LEN); p2->x=x1;p2->y=y1; p2->next=head;head->last=p2;head=p2;hit=1;long1+;else p2=eof1;eof1=p2->last;eof1->next=NULL;unit(
31、p2->x,p2->y,0);p2->next=head;head->last=p2; p2->x=head->x+1;p2->y=head->y;head=p2; head->last=NULL;unit(p2->x,p2->y,14);if(p2->x>=40)score(); dl(speed);void dl(int a) int r,n; for(r=0;r<a;r+) &
32、#160; for(n=0;n<6000;n+) n+; n-;void unit(uchar x,uchar y,uchar color) setfillstyle(1,color); setcolor(color);bar(x*15,y*15,(x+1)*15-2,(y+1)*15-2);void score(void) uchar ss20;i
33、f(long1>50) sco=(long1-50)*3+(long1-30)*2+14;else if(long1>30) sco=(long1-30)*2+14; else sco=long1-16; sprintf(ss,"your score is %d",sco);outtextxy(50,50,ss);cycy=1;
34、; save();void save(void) FILE * fp;int i;i=0;fp=fopen("snascore.txt","rb+");i=(int)(fgetc(fp)-48;if(fp=NULL) i=0; fclose(fp);if(i<sco) fp=fopen("snascore.txt",
35、"wb+"); fprintf(fp,"%d",sco); fclose(fp); printf("n"); printf("Your score: %d are the highest!",sco);/* getch();*/void save1() FILE *fl1; i=0; dl(1500);
36、 p1=head; p2=p1; while(p1!=NULL) str1i=(char)(p1->x+30); str1i+1=(char)(p1->y+30); i=i+2; p2=p1;p1=p2->next;dl(1500); fl1=fopen("snasave.txt","w+"); if(fl1!=NULL) fwrite(str1,i,1,fl1);dl(1500);fclose(fl1); elseprintf("badn"); dl(1500); cycy=1;void load1() FILE *fl1; int cc; i=0; fl1=fopen("snasave.txt",&quo
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 护理实践中的文化支持
- 护理职业精神与励志教育的实践策略
- 护理学生竞赛辅导指南
- 内科护理试题及答案
- 客户生命周期价值管理与策略制定
- 旅游行业财务分析职位的面试要点
- 零售业库存管理岗位面试要点
- 成都高新未来科技城国际科教园文化设施项目水土保持方案报告表
- 旅游行业策划岗位面试经验
- 临床事务经理培训计划与内容
- 2026复工复产安全培训第9版
- 《TCSUS69-2024智慧水务技术标准》
- GB/T 3098.6-2023紧固件机械性能不锈钢螺栓、螺钉和螺柱
- 女装成衣设计与工艺技术
- 《数字图像与视频处理》第9章 图像与视频的质量评价PPT
- 面瘫诊疗方案优化方案
- 中国图书馆分类法简表
- 新课程的教育理念 义务教育物理课程标准解读 新课标
- 地质灾害防治工程课件
- 糖尿病慢性并发症P课件
- 经皮肾镜碎石术并发脓毒血症的风险与防治
评论
0/150
提交评论