贪吃蛇
来源:互联网 发布:centos重置root密码 编辑:程序博客网 时间:2024/04/30 10:03
#include <stdio.h>#include <windows.h>#include <time.h>#include <conio.h>#include <stdlib.h>int snake_len=1;//蛇的长度int snake_loc[50][2]={31,12};//整条蛇的位置,最长为50int snake_head[2]={31,12};//蛇头位置,初始值为11,12;int food[2];//食物位置char snake_direction='s';int delay=200; //蛇每delay个时间走一步int eat_flag=0;//1表示吃了食物,0表示未吃int life=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 welcome_w() //欢迎界面; {hidden();//隐藏光标; gotoxy(22,10);printf("Welcome to GREEDY SNAKER!");gotoxy(22,17);system("pause");system("cls");for(int i=3;i>0;i--){gotoxy(33,10);printf("%d",i);Sleep(1000);}gotoxy(33,10);printf("GO!");Sleep(1000);system("cls");}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 init()//初始化{int i;snake_len=1;//蛇的长度snake_loc[0][0]=31;//整条蛇的位置snake_loc[0][1]=12;snake_head[0]=31;//蛇头位置,初始值为31,12;snake_head[1]=12;snake_direction='s';delay=200;eat_flag=0;life=0;for(i=1;i<50;i++){snake_loc[i][0]=0;//整条蛇的位置snake_loc[i][1]=0;}}void update_score()//更新分数,蛇吃一个食物为10分; {gotoxy(73,3);printf("%2d",(snake_len-1)*10);}void create_food()//产生食物的位置{time_t t;srand(time(&t)); //以时间为随机种子; while(1){food[0]=rand()%62+1;//生成1~62之间的随机数food[1]=rand()%22+1;//生成1~22之间的随机数if(food[0]!=snake_head[0]&&food[1]!=snake_head[1])break;}gotoxy(food[0],food[1]);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;i<19;i++)printf(" ");}else if(keyhit==27){ system("cls");exit(0);}}void state()//判定蛇死没死{if(snake_head[0]<1||snake_head[0]>62||snake_head[1]<1||snake_head[1]>22)life=1;}void eat()//判定蛇吃没吃上,并对根据方向对蛇头位置进行更新{switch(snake_direction){case 'w':snake_head[1]--;break;case 's':snake_head[1]++;break;case 'a':snake_head[0]--;break;case 'd':snake_head[0]++;break;}if((food[0]==snake_head[0]) && (food[1]==snake_head[1]) ) //如果蛇把食物吃了; {eat_flag=1;switch(snake_direction){case 'w':snake_head[1]--;break;case 's':snake_head[1]++;break;case 'a':snake_head[0]--;break;case 'd':snake_head[0]++;break;}}}void show_snake()//更新蛇在屏幕中的位置{gotoxy(snake_head[0],snake_head[1]);printf("*");gotoxy(snake_loc[snake_len-1][0],snake_loc[snake_len-1][1]);printf(" ");}void update_maxtrix()//更新存储蛇位置的数组{int i;if(eat_flag!=1){for(i=snake_len-1;i>0;i--){snake_loc[i][0]=snake_loc[i-1][0];snake_loc[i][1]=snake_loc[i-1][1];}}else{snake_len++;if(snake_len>3 && delay>100)delay-=30;for(i=snake_len-1;i>1;i--){snake_loc[i][0]=snake_loc[i-2][0];snake_loc[i][1]=snake_loc[i-2][1];}snake_loc[1][0]=food[0];snake_loc[1][1]=food[1];eat_flag=0;create_food();update_score();}snake_loc[0][0]=snake_head[0];snake_loc[0][1]=snake_head[1];}int main(){welcome_w(); LP:system("cls");init(); //初始化游戏数据; create_window(); //创建游戏窗口; gotoxy(73,3); //在输出分数位置,输出0; printf("0");hidden(); //隐藏鼠标; create_food(); //输出第一个食物的位置; while(1){int i;Sleep(delay); direction(); //控制方向函数; eat(); //判定蛇吃没吃上,并对根据方向对蛇头位置进行更新show_snake(); //更新蛇在屏幕中的位置; update_maxtrix(); //更新存储蛇位置的数组(为下一次) ; state(); //判断蛇死没有; if(life==1) //如果蛇死了,用空格将蛇的身体覆盖,达到消失的目的; {for(i=1;i<snake_len;i++){gotoxy(snake_loc[i][0],snake_loc[i][1]);printf(" ");}gotoxy(food[0],food[1]); //将食物覆盖; printf(" ");gotoxy(30,12);printf("Game over!\n"); //输出游戏结束标志; gotoxy(25,13);printf("继续请按y,退出请按n"); //向玩家给出提示信息; while(1) //用死循环,防止玩家输入其他按键游戏无反应; {i=getch();if(i=='y')goto LP;//判断是否重新开始游戏;else if(i=='n')system("cls");break;}break;}}}
0 0
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- git下载和上传代码
- CocoaPods安装和使用及问题:Setting up CocoaPods master repo
- ListView中convertView和ViewHolder的工作原理
- ShaderLab Tutorials-Matrix
- Json解析
- 贪吃蛇
- 找到linux下当前进程启动的目录
- MYSQL 校对集 collate
- jQuery锚点滚动js
- PHP引用问题
- Servlet--表单、超链接、转发、重定向4种情况的路径
- android:在android studio中使用lambda,android lambda,retrolambda
- 树形DP的一些总结
- 腾讯Unity3D手游 dll加密分析