已阅读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年人寿保险行业保险科技创新案例分析报告
- 2025年互联网行业互联网行业区块链技术应用与数字经济发展研究报告及未来发展趋势预测
- 2025年海洋产业海洋资源开发与海洋经济发展研究报告及未来发展趋势预测
- 2026年中国变流量泵行业市场前景预测及投资价值评估分析报告
- 签订委托审计协议书
- 苹果手机upnp协议书
- 个人农田浇水合同协议范本
- 上海拆迁协议书
- 政府师徒结对协议书
- 2025初级商业人像摄影师电商服装人像布光统一性考核试卷
- 刑事非法证据排除课件
- 美容美发行业卫生管理制度
- 2025年秋期人教版五年级上册数学全册核心素养教案(教学反思有内容+二次备课版)
- 2025年博物馆策展人员综合素质考核试卷及答案
- 福建省建筑施工安全生产标准化考评实施细则
- 雪的课件小插图
- 肝脏肿瘤肝叶切除麻醉管理流程
- 低温液体泵培训课件
- 国企员工违法违纪案件警示教育心得体会
- 劳务派遣劳务外包服务方案(技术方案)
- 2025中国单机游戏市场现状报告
评论
0/150
提交评论