找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3078|回复: 3
打印 上一主题 下一主题
收起左侧

贪吃蛇运行程序 C语言编写

[复制链接]
跳转到指定楼层
楼主
ID:197463 发表于 2017-5-6 21:03 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. #include <stdio.h>
  2. #include <windows.h>
  3. #include <time.h>
  4. #include <conio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. int  snake_len=1;//蛇的长度
  8. int  snake_loc[50][2]={31,12};//整条蛇的位置,最长为50
  9. int  snake_head[2]={31,12};//蛇头位置,初始值为11,12;
  10. int  food[2];//食物位置
  11. char snake_direction='s';
  12. int  delay=200; //蛇每delay个时间走一步
  13. int  eat_flag=0;//1表示吃了食物,0表示未吃
  14. int  liv_stat=0;//1表示死了,游戏该结束了;0表示还活着
  15. void gotoxy(int x, int y)//定位光标,x为行坐标,y为列坐标
  16. {
  17. COORD pos = {x,y};
  18. HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  19. SetConsoleCursorPosition(hOut, pos);
  20. }
  21. void hidden()//隐藏光标
  22. {
  23.      HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  24.      CONSOLE_CURSOR_INFO cci;
  25. GetConsoleCursorInfo(hOut,&cci);
  26. cci.bVisible=0;//赋1为显示,赋0为隐藏
  27. SetConsoleCursorInfo(hOut,&cci);
  28. }
  29. void init()//初始化
  30. {
  31. int i;
  32. snake_len=1;//蛇的长度
  33. snake_loc[0][0]=31;//整条蛇的位置
  34. snake_loc[0][1]=12;
  35. snake_head[0]=31;//蛇头位置,初始值为11,12;
  36. snake_head[1]=12;
  37. snake_direction='s';
  38. delay=200;
  39. eat_flag=0;
  40. liv_stat=0;
  41. for(i=1;i<50;i++)
  42. {
  43.   snake_loc[i][0]=0;//整条蛇的位置
  44.   snake_loc[i][1]=0;
  45. }

  46. }

  47. void create_window()//创建窗口
  48. {
  49. gotoxy(0,0);
  50. printf("********************************************************************************");
  51. printf("*                                                              *               *");
  52. printf("*                                                              *               *");
  53. printf("*                                                              *   分数:1     *");
  54. printf("*                                                              *   按键说明:  *");
  55. printf("*                                                              *   上:w       *");
  56. printf("*                                                              *   下:s       *");
  57. printf("*                                                              *   左:a       *");
  58. printf("*                                                              *   右:d       *");
  59. printf("*                                                              *   暂停:空格  *");
  60. printf("*                                                              *   退出:Esc键 *");
  61. printf("*                                                              *               *");
  62. printf("*                                                              *               *");
  63. printf("*                                                              *               *");
  64. printf("*                                                              *               *");
  65. printf("*                                                              *               *");
  66. printf("*                                                              *               *");
  67. printf("*                                                              *               *");
  68. printf("*                                                              *               *");
  69. printf("*                                                              *               *");
  70. printf("*                                                              *               *");
  71. printf("*                                                              *               *");
  72. printf("*                                                              *               *");
  73. printf("********************************************************************************");
  74. }
  75. void update_score()//更新分数
  76. {
  77. gotoxy(73,3);
  78. printf("%2d",snake_len);
  79. }
  80. void create_food()//产生食物的位置
  81. {
  82. time_t t;
  83. srand(time(&t));
  84. while(1)
  85. {
  86.   food[0]=rand()%62+1;//生成1~62之间的随机数,其中random函数生成0~77之间的随机数
  87.   food[1]=rand()%22+1;//生成1~22之间的随机数,其中random函数生成0~17之间的随机数
  88.   if(food[0]!=snake_head[0]&&food[1]!=snake_head[1])break;
  89. }
  90. gotoxy(food[0],food[1]);
  91. printf("*");
  92. }
  93. void direction()
  94. {
  95. char keyhit=0,i;
  96. while(kbhit()!=0)keyhit=getch();
  97. if( ((keyhit=='a') || (keyhit=='d') || (keyhit=='w') || (keyhit=='s')) && (abs(snake_direction/16-keyhit/16)==1) )snake_direction=keyhit;
  98. else if(keyhit==' ')
  99. {
  100.   gotoxy(30,24);
  101.   system("pause");
  102.   gotoxy(30,24);
  103.   for(i=0;i<19;i++)printf(" ");
  104. }
  105. else if(keyhit==27)exit(0);
  106. }
  107. void state()//判定蛇死没死
  108. {
  109. if(snake_head[0]<1||snake_head[0]>62||snake_head[1]<1||snake_head[1]>22)liv_stat=1;
  110. }
  111. void eat()//判定蛇吃没吃上,并对根据方向对蛇头位置进行更新
  112. {
  113. switch(snake_direction)
  114.   {
  115.    case 'w': snake_head[1]--;break;
  116.    case 's': snake_head[1]++;break;
  117.    case 'a': snake_head[0]--;break;
  118.    case 'd': snake_head[0]++;break;
  119.   }
  120. if((food[0]==snake_head[0]) && (food[1]==snake_head[1]) )
  121. {
  122.   eat_flag=1;
  123.   switch(snake_direction)
  124.   {
  125.    case 'w': snake_head[1]--;break;
  126.    case 's': snake_head[1]++;break;
  127.    case 'a': snake_head[0]--;break;
  128.    case 'd': snake_head[0]++;break;
  129.   }
  130. }
  131.   
  132. }
  133. void show_snake()//更新蛇在屏幕中的位置
  134. {
  135.   gotoxy(snake_head[0],snake_head[1]);
  136.   printf("*");
  137.   gotoxy(snake_loc[snake_len-1][0],snake_loc[snake_len-1][1]);
  138.   printf(" ");
  139. }
  140. void update_maxtrix()//更新存储蛇位置的数组
  141. {
  142. int i;
  143. if(eat_flag!=1)
  144. {
  145.   for(i=snake_len-1;i>0;i--)
  146.   {
  147.    snake_loc[i][0]=snake_loc[i-1][0];
  148.    snake_loc[i][1]=snake_loc[i-1][1];
  149.   }
  150. }
  151. else
  152. {
  153.   snake_len++;
  154.   if(snake_len>3 && delay>100)delay-=30;
  155.   for(i=snake_len-1;i>1;i--)
  156.   {
  157.    snake_loc[i][0]=snake_loc[i-2][0];
  158.    snake_loc[i][1]=snake_loc[i-2][1];
  159.   }
  160.   snake_loc[1][0]=food[0];
  161.   snake_loc[1][1]=food[1];
  162.   eat_flag=0;
  163.   create_food();
  164.   update_score();
  165. }
  166. snake_loc[0][0]=snake_head[0];
  167. snake_loc[0][1]=snake_head[1];
  168. }
  169. void main()
  170. {
  171. LOOP:
  172.    system("cls");
  173.    init();
  174.    create_window();
  175.    hidden();
  176.    create_food();
  177. while(1)
  178. {
  179.   int i;
  180.   Sleep(delay);
  181.   direction();
  182.   eat();
  183.   show_snake();
  184.   update_maxtrix();
  185.   state();
  186.   if(liv_stat==1)
  187.   {
  188.    for(i=1;i<snake_len;i++)
  189.    {
  190.     gotoxy(snake_loc[i][0],snake_loc[i][1]);
  191.     printf(" ");
  192.    }
  193.    gotoxy(food[0],food[1]);
  194.    printf(" ");
  195.    gotoxy(30,12);
  196.    printf("Game over!\n");
  197.    gotoxy(25,13);
  198.    printf("继续请按y,退出请按n");
  199.    while(1)
  200.    {
  201.     i=getch();
  202.     if(i=='y')goto LOOP;
  203.     else if(i=='n')break;
  204.    }
  205.    break;
  206.   }
  207. }
  208. }
复制代码
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:211258 发表于 2017-6-15 16:12 | 只看该作者
非常好
回复

使用道具 举报

板凳
ID:160895 发表于 2017-7-15 12:25 | 只看该作者
可以运行,非常好,,
回复

使用道具 举报

地板
ID:248240 发表于 2017-11-10 23:10 | 只看该作者
看不懂,学习学习
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表