已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
贪吃蛇代码 以及游戏程序#include #include #include #include #include #include int snake_len=1;/蛇的长度int snake_loc502=31,12;/整条蛇的位置,最长为50int snake_head2=31,12;/蛇头位置,初始值为11,12;int food2;/食物位置char snake_direction=s;int delay=200; /蛇每delay个时间走一步int eat_flag=0;/1表示吃了食物,0表示未吃int liv_stat=0;/1表示死了,游戏该结束了;0表示还活着void gotoxy(int x, int y)/定位光标,x为行坐标,y为列坐标COORD pos = x,y;HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);SetConsoleCursorPosition(hOut, pos); void hidden()/隐藏光标 HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_CURSOR_INFO cci;GetConsoleCursorInfo(hOut,&cci);cci.bVisible=0;/赋1为显示,赋0为隐藏SetConsoleCursorInfo(hOut,&cci);void init()/初始化int i;snake_len=1;/蛇的长度snake_loc00=31;/整条蛇的位置snake_loc01=12;snake_head0=31;/蛇头位置,初始值为11,12;snake_head1=12;snake_direction=s;delay=200;eat_flag=0;liv_stat=0;for(i=1;i50;i+)snake_loci0=0;/整条蛇的位置snake_loci1=0;void create_window()/创建窗口gotoxy(0,0);printf(*);printf(* * *);printf(* * *);printf(* * 分数:1 *);printf(* * 按键说明: *);printf(* * 上:w *);printf(* * 下:s *);printf(* * 左:a *);printf(* * 右:d *);printf(* * 暂停:空格 *);printf(* 袁楚瑶捉蛇 * 退出:Esc键 *);printf(* * *);printf(* * *);printf(* * *);printf(* * *);printf(* * *);printf(* 高敏放蛇 * *);printf(* * *);printf(* * *);printf(* * *);printf(* * *);printf(* * *);printf(* * *);printf(*);void update_score()/更新分数gotoxy(73,3);printf(%2d,snake_len);void create_food()/产生食物的位置time_t t;srand(time(&t);while(1)food0=rand()%62+1;/生成162之间的随机数,其中random函数生成077之间的随机数food1=rand()%22+1;/生成122之间的随机数,其中random函数生成017之间的随机数if(food0!=snake_head0&food1!=snake_head1)break;gotoxy(food0,food1);printf(*);void direction()char keyhit=0,i;while(kbhit()!=0)keyhit=getch();if( (keyhit=a) | (keyhit=d) | (keyhit=w) | (keyhit=s) & (abs(snake_direction/16-keyhit/16)=1) )snake_direction=keyhit;else if(keyhit= )gotoxy(30,24);system(pause);gotoxy(30,24);for(i=0;i19;i+)printf( );else if(keyhit=27)exit(0);void state()/判定蛇死没死if(snake_head062|snake_head122)liv_stat=1;void eat()/判定蛇吃没吃上,并对根据方向对蛇头位置进行更新switch(snake_direction)case w:snake_head1-;break;case s:snake_head1+;break;case a:snake_head0-;break;case d:snake_head0+;break;if(food0=snake_head0) & (food1=snake_head1) )eat_flag=1;switch(snake_direction)case w:snake_head1-;break;case s:snake_head1+;break;case a:snake_head0-;break;case d:snake_head0+;break;void show_snake()/更新蛇在屏幕中的位置gotoxy(snake_head0,snake_head1);printf(*);gotoxy(snake_locsnake_len-10,snake_locsnake_len-11);printf( );void update_maxtrix()/更新存储蛇位置的数组int i;if(eat_flag!=1)for(i=snake_len-1;i0;i-)snake_loci0=snake_loci-10;snake_loci1=snake_loci-11;elsesnake_len+;if(snake_len3 & delay100)delay-=30;for(i=snake_len-1;i1;i-)snake_loci0=snake_loci-20;snake_loci1=snake_loci-21;snake_loc10=food0;snake_loc11=food1;eat_flag=0;create_food();update_score();snake_loc00=snake_head0;snake_loc01=snake_head1;void main()LOOP:system(cls);init();create_window();hidden();create_food();while(1)int i;Sleep(delay);direction();eat();show_snake();update_maxtrix();state();if(liv_stat=1)for(i=1;isnake_len;i+)gotoxy(snake_loci0,snake_loci1);printf( );
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025安徽大学博士及高层次人才招聘282人笔试考试参考题库附答案解析
- 2025甘肃张掖民乐县麓源社会服务中心招聘3人考试笔试备考试题及答案解析
- 2025贵州贵阳综保产业发展有限公司(第二批)对外招聘11人笔试考试备考题库及答案解析
- 2025年11月福建厦门大学医学中心(厦大翔安医院) 招聘辅助岗位人员13人考试笔试模拟试题及答案解析
- 职业技术学院教学质量提升计划
- 2025河南省息县公证处招聘公证员3人考试笔试备考题库及答案解析
- 2025福建省乡总农业集团有限公司员工招聘1人考试笔试模拟试题及答案解析
- 2026年山东农商行招聘考试内容笔试考试备考试题及答案解析
- 2025中国远程办公软件市场竞争格局与投资价值评估报告
- 2025中国语音交互技术应用场景拓展及用户体验报告
- 低温液体泵培训课件
- 国企员工违法违纪案件警示教育心得体会
- 劳务派遣劳务外包服务方案(技术方案)
- 2025中国单机游戏市场现状报告
- 诉讼案件备案管理制度
- 《HTML5+CSS3 Web开发案例教程》课件-第11章 经典DIV+CSS网页布局方法
- 手工刺绣教学课件
- 山东省高级农艺师考试试题及答案
- 社区村委会物业管理制度
- 股东退出减资协议书
- 2024-2025学年高一下学期《人生拍卖体验官》主题班会课件
评论
0/150
提交评论