搜索
查看: 1157|回复: 4
打印 上一主题 下一主题

挖坟挖到纯C语言注释最详细的贪吃蛇源代码

[复制链接]
跳转到指定楼层
楼主
发表于 2014-4-1 21:06:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
不多说,直接上代码
  1. #include<stdio.h>
  2. #include<windows.h>
  3. #include<conio.h>
  4. #include<time.h>
  5. #include<stdlib.h>
  6. #define WIDTH 40
  7. #define HEIGH 12
  8. enum direction{        //方向
  9.         LEFT,
  10.         RIGHT,
  11.         UP,
  12.         DOWN
  13. };
  14. struct Food{        //食物
  15.         int x;
  16.         int y;
  17. };
  18. struct Node{        //画蛇身
  19.         int x;
  20.         int y;
  21.         struct Node *next;
  22. };
  23. struct Snake{        //蛇属性
  24.         int lenth;                        //长度
  25.         enum direction dir;        //方向
  26. };
  27. struct Food *food; //食物
  28. struct Snake *snake;        //蛇属性
  29. struct Node *snode,*tail;                //蛇身
  30. int SPEECH=200;
  31. int score=0;        //分数
  32. int smark=0;        //吃食物标记
  33. int times=0;       
  34. int STOP=0;
  35. void Initfood();        //产生食物
  36. void Initsnake();        //构造snake
  37. void Eatfood();                //头部前进
  38. void Addnode(int x, int y);                //增加蛇身
  39. void display(struct Node *shead);                //显示蛇身坐标
  40. void move();                        //蛇移动
  41. void draw();                        //画蛇
  42. void Homepage();        //主页
  43. void keybordhit();        //监控键盘按键
  44. void Addtail();                //吃到食物
  45. void gotoxy(int x, int y)                //定位光标
  46. {
  47.     COORD pos;
  48.     pos.X = x - 1;
  49.     pos.Y = y - 1;
  50.     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
  51. }
  52. void Initsnake()        //构造snake
  53. {
  54.         int i;
  55.         snake=(struct Snake*)malloc(sizeof(struct Snake));
  56.         tail=(struct Node*)malloc(sizeof(struct Node));
  57.         food = (struct Food*)malloc(sizeof(struct Food));
  58.         snake->lenth=5;                                //初始长度 5
  59.         snake->dir=RIGHT;                        //初始蛇头方向 右
  60.         for(i=2;i<=snake->lenth+2;i++)        //增加 5 个结点
  61.         {
  62.                 Addnode(i,2);
  63.         }
  64. }
  65. void Initfood()                //产生食物
  66. {
  67.         struct Node *p=snode;
  68.         int mark=1;
  69.         srand((unsigned)time(NULL));                //以时间为种子产生随机数
  70.         while(1)
  71.         {
  72.         food->x=rand()%(WIDTH-2)+2;                        //食物X坐标
  73.         food->y=rand()%(HEIGH-2)+2;                        //食物Y坐标
  74.         while(p!=NULL)                                       
  75.         {
  76.                 if((food->x==p->x)&&(food->y==p->y))        //如果食物产生在蛇身上
  77.                 {                                                                        //则重新生成食物
  78.                         mark=0;                //食物生成无效
  79.                         break;
  80.                 }
  81.                 p=p->next;
  82.         }
  83.         if(mark==1)//如果食物不在蛇身上,生成食物,否则重新生成食物
  84.         {
  85.                 gotoxy(food->x,food->y);
  86.                 printf("%c",3);
  87.                 break;
  88.         }
  89.         mark=1;
  90.         p=snode;
  91.         }
  92. }
  93. void move()                //移动
  94. {
  95.         struct Node *q, *p=snode;
  96.         if(snake->dir==RIGHT)
  97.         {
  98.                 Addnode(p->x+1,p->y);
  99.                 if(smark==0)
  100.                 {
  101.                 while(p->next!=NULL)
  102.                 {
  103.                         q=p;
  104.                         p=p->next;
  105.                 }
  106.                 q->next=NULL;
  107.                 free(p);
  108.                 }
  109.         }
  110.         if(snake->dir==LEFT)
  111.         {
  112.                 Addnode(p->x-1,p->y);
  113.                 if(smark==0)
  114.                 {
  115.                 while(p->next!=NULL)
  116.                 {
  117.                         q=p;
  118.                         p=p->next;
  119.                 }
  120.                 q->next=NULL;
  121.                 free(p);
  122.                 }
  123.         }
  124.         if(snake->dir==UP)
  125.         {
  126.                 Addnode(p->x,p->y-1);
  127.                 if(smark==0)
  128.                 {
  129.                 while(p->next!=NULL)
  130.                 {
  131.                         q=p;
  132.                         p=p->next;
  133.                 }
  134.                 q->next=NULL;
  135.                 free(p);
  136.                 }
  137.         }
  138.         if(snake->dir==DOWN)
  139.         {
  140.                 Addnode(p->x,p->y+1);
  141.                 if(smark==0)
  142.                 {
  143.                 while(p->next!=NULL)
  144.                 {
  145.                         q=p;
  146.                         p=p->next;
  147.                 }
  148.                 q->next=NULL;
  149.                 free(p);
  150.                 }
  151.         }
  152. }
  153. void Addnode(int x, int y)                //增加蛇身
  154. {
  155.         struct Node *newnode=(struct Node *)malloc(sizeof(struct Node));
  156.         struct Node *p=snode;
  157.         newnode->next=snode;
  158.         newnode->x=x;
  159.         newnode->y=y;
  160.         snode=newnode;        //结点加到蛇头
  161.         if(x<2||x>=WIDTH||y<2||y>=HEIGH)        //碰到边界
  162.         {
  163.                 STOP=1;
  164.                 gotoxy(10,19);
  165.                 printf("撞墙,游戏结束,任意键退出!\n");                        //失败
  166.                 _getch();
  167.                 free(snode);                //释放内存
  168.                 free(snake);
  169.                 exit(0);
  170.         }
  171.         while(p!=NULL)                                //碰到自身
  172.         {
  173.                 if(p->next!=NULL)
  174.                 if((p->x==x)&&(p->y==y))
  175.                 {
  176.                         STOP=1;
  177.                         gotoxy(10,19);
  178.                         printf("撞到自身,游戏结束,任意键退出!\n");                        //失败
  179.                         _getch();
  180.                         free(snode);                //释放内存
  181.                         free(snake);
  182.                         exit(0);
  183.                 }
  184.                 p=p->next;
  185.         }
  186. }
  187. void Eatfood()                //吃到食物
  188. {
  189.         Addtail();
  190.         score++;
  191. }
  192. void Addtail()                //增加蛇尾
  193. {
  194.         struct Node *newnode=(struct Node *)malloc(sizeof(struct Node));
  195.         struct Node *p=snode;
  196.         tail->next=newnode;
  197.         newnode->x=50;
  198.         newnode->y=20;
  199.         newnode->next=NULL;        //结点加到蛇头               
  200.         tail=newnode;                //新的蛇尾
  201. }
  202. void draw()                        //画蛇
  203. {
  204.         struct Node *p=snode;
  205.         int i,j;
  206.         while(p!=NULL)
  207.         {
  208.                 gotoxy(p->x,p->y);
  209.                 printf("%c",2);
  210.                 tail=p;
  211.                 p=p->next;
  212.         }
  213.         if(snode->x==food->x&&snode->y==food->y)        //蛇头坐标等于食物坐标
  214.         {
  215.                 smark=1;
  216.                 Eatfood();                                //增加结点       
  217.                 Initfood();                        //产生食物
  218.         }
  219.         if(smark==0)       
  220.         {
  221.                 gotoxy(tail->x,tail->y);                //没吃到食物清除之前的尾结点
  222.                 printf("%c",' ');                                //如果吃到食物,不清楚尾结点
  223.         }
  224.         else
  225.         {
  226.                 times=1;       
  227.         }
  228.         if((smark==1)&&(times==1))       
  229.         {
  230.                 gotoxy(tail->x,tail->y);                //没吃到食物清除之前的尾结点
  231.                 printf("%c",' ');                                //如果吃到食物,不清楚尾结点
  232.                 smark=0;
  233.         }
  234.         gotoxy(50,12);
  235.         printf("食物: %d,%d",food->x,food->y);
  236.         gotoxy(50,5);
  237.         printf("分数: %d",score);
  238.         gotoxy(50,7);
  239.         printf("速度: %d",SPEECH);
  240.         gotoxy(15,14);
  241.         printf("按o键加速");
  242.         gotoxy(15,15);
  243.         printf("按p键减速");
  244.         gotoxy(15,16);
  245.         printf("按空格键暂停");
  246. }
  247. void HideCursor()        //隐藏光标
  248. {
  249. CONSOLE_CURSOR_INFO cursor_info = {1, 0};
  250. SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
  251. }
  252. void Homepage()                //绘主页
  253. {
  254.         int x,y;
  255.         HideCursor();        //隐藏光标
  256.         printf("----------------------------------------\n");
  257.         printf("|\t\t\t\t       |\n");
  258.         printf("|\t\t\t\t       |\n");
  259.         printf("|\t\t\t\t       |\n");
  260.         printf("|\t\t\t\t       |\n");
  261.         printf("|\t\t\t\t       |\n");
  262.         printf("|\t\t\t\t       |\n");
  263.         printf("|\t\t\t\t       |\n");
  264.         printf("|\t\t\t\t       |\n");
  265.         printf("|\t\t\t\t       |\n");
  266.         printf("|\t\t\t\t       |\n");
  267.         printf("----------------------------------------\n");
  268.         gotoxy(5,13);
  269.         printf("任意键开始游戏!按W.A.S.D控制方向");
  270.         _getch();
  271.         Initsnake();
  272.         Initfood();
  273.         gotoxy(5,13);
  274.         printf("                                ");
  275. }
  276. void keybordhit()                //监控键盘
  277. {
  278.         char ch;
  279.         if(_kbhit())               
  280.                 {
  281.                         ch=getch();
  282.                         switch(ch)
  283.                         {
  284.                         case 'W':
  285.                         case 'w':if(snake->dir==DOWN)        //如果本来方向是下,而按相反方向无效
  286.                                         {
  287.                                                  break;
  288.                                         }
  289.                                         else
  290.                                                 snake->dir=UP;break;
  291.                         case 'A':
  292.                         case 'a':if(snake->dir==RIGHT)        //如果本来方向是右,而按相反方向无效
  293.                                         {
  294.                                                  break;
  295.                                         }
  296.                                         else
  297.                                                 snake->dir=LEFT;break;
  298.                         case 'S':
  299.                         case 's':if(snake->dir==UP)        //如果本来方向是上,而按相反方向无效
  300.                                         {
  301.                                                  break;
  302.                                         }
  303.                                         else
  304.                                                 snake->dir=DOWN;break;
  305.                         case 'D':
  306.                         case 'd':if(snake->dir==LEFT)        //如果本来方向是左,而按相反方向无效
  307.                                         {
  308.                                                  break;
  309.                                         }
  310.                                         else
  311.                                                 snake->dir=RIGHT;break;
  312.                         case 'O':
  313.                         case 'o':
  314.                                         if(SPEECH>=150)                        //速度加快
  315.                                         {
  316.                                                 SPEECH=SPEECH-50;
  317.                                         }
  318.                                         break;
  319.                         case 'P':
  320.                         case 'p':
  321.                                         if(SPEECH<=400)                        //速度减慢
  322.                                         {
  323.                                                 SPEECH=SPEECH+50;
  324.                                         }
  325.                                         break;
  326.                         case ' ':                                                //暂停
  327.                                         gotoxy(15,18);
  328.                                         printf("游戏已暂停,按任意键恢复游戏");
  329.                                         system("pause>nul");
  330.                                         gotoxy(15,18);
  331.                                         printf("                           ");
  332.                                         break;
  333.                         default:break;
  334.                         }
  335.                 }
  336. }
  337. int main(void)                //程序入口
  338. {
  339.         Homepage();
  340.         while(!STOP)
  341.         {
  342.                 keybordhit();        //监控键盘按键
  343.                 move();                        //蛇的坐标变化
  344.                 draw();                        //蛇的重绘
  345.                 Sleep(SPEECH);        //暂时挂起线程
  346.         }
  347.         return 0;
  348. }
复制代码



贪吃蛇.rar

130.08 KB, 下载次数: 64

贪吃蛇.c

6.65 KB, 下载次数: 22

售价: 2 啊哈币  [记录]

沙发
发表于 2014-4-1 22:59:06 | 只看该作者
有毒?已经被隔离!!!!!!!!

点评

应该是误报的,源代码不是都贴着呢~~!  发表于 2014-4-1 23:19
板凳
发表于 2014-4-4 10:58:32 | 只看该作者
有没有按键设置重来啊。每次都要退出。

点评

自己看源代码呢~~  发表于 2014-4-4 17:39
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

广播台
特别关注
快速回复 返回顶部 返回列表