鱼C论坛

 找回密码
 立即注册
查看: 2274|回复: 14

[技术交流] 贪吃蛇(纯c编写,新手请进)

[复制链接]
发表于 2017-3-17 16:12:23 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 qwe1231069 于 2017-3-18 11:37 编辑

我花了好几天写了这玩意(第一次写),我敢保证这游戏写的是最烂的。玩了一会,游戏体验太差。反正是给咱们新手看的,无所谓了。能玩就行了。

首先我是参考1012662902的帖子http://bbs.fishc.com/thread-49160-1-1.html(虽然有些依旧不懂,留着吧。后面再看看再改)
遇到了一些问题,也有好心人解答。@人造人和@lumber2388779(回帖我给你们评分哦)

由于好些地方代码写的不好,所以吃掉一个后要再按一下方向键才出现下一个。wasd(小写控制移动),任意键暂停。
还有好多应该改进的地方,因为我太懒了所以就拖着吧。

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include<windows.h>
  4. #include <conio.h>
  5. #include<time.h>

  6. #define WX 50
  7. #define WY 23

  8. void gotoxy(int x, int y);


  9. typedef struct snake{
  10. int x;
  11. int y;
  12. int length;
  13. struct snake *next;
  14. }snake, *Snake;

  15. typedef struct food{
  16. int x;
  17. int y;
  18. struct food *next;
  19. }food, *Food;

  20. void clean(Snake head);
  21. void test(Snake head);
  22. int getvalue(Snake o, int n);
  23. int getvaluey(Snake o, int n);
  24. void changevalue(Snake n, int i, int s);
  25. void changevaluey(Snake n, int i, int s);
  26. void control(Snake a);
  27. void auto_move(Snake head,char direction, Food k);
  28. void *show_food(Snake head);
  29. int getlength(Snake k);
  30. void the_end(Snake head);
  31. int f = 0;//是否有食物

  32. //创建动态链表,在链表末尾添加一个x,y坐标
  33. void createL(Snake head, int x, int y)
  34. {
  35.     Snake swap, newl;
  36.     swap = head;
  37.     while(swap->next != NULL)
  38.     {
  39.         swap = swap->next;
  40.     }

  41.     newl = (Snake) malloc(sizeof(snake));
  42.     if(newl == NULL )
  43.     {
  44.         printf("error\n");
  45.     }
  46.     newl->x = x;
  47.     newl->y = y;
  48.     newl ->length = swap ->length + 1;
  49.     swap->next = newl;
  50.     newl->next = NULL;

  51. }
  52. void* _snake()//初始化蛇
  53. {
  54.     int i;
  55.     Snake head;
  56.     head =(Snake) malloc(sizeof(snake));
  57.     head->next = NULL;
  58.     head->x = 8;
  59.     head->y = 3;
  60.     head ->length = 1;

  61.     for(i = 0; i < 3; i++)
  62.     {
  63.         createL(head, 7 - i, 3);
  64.     }
  65.     return (head);

  66. }


  67. void draw_snake(Snake head)
  68. {
  69.     gotoxy(head->x, head->y);
  70.     putchar('@');//这是脑袋
  71.     head = head->next;
  72.     for( ;head->next != NULL;head = head->next)
  73.     {
  74.         gotoxy(head->x, head->y);
  75.         putchar('*');//这是身体
  76.     }
  77.     gotoxy(head->x, head->y);
  78.     putchar('*');//这是尾巴
  79. }

  80. int getlength(Snake head)
  81. {
  82.     Snake swap = head;
  83.     int n = 0;
  84.      for( ; swap->next != NULL ;swap = swap ->next)
  85.          {
  86.              n = swap->length;
  87.              /** 不包括蛇头 n 是蛇身的长度
  88.              */
  89.          }
  90.          return n;
  91. }
  92. void *show_food(Snake head)
  93. {
  94.     srand(time(NULL));
  95.     Food apple;

  96.     apple = (Food)malloc(sizeof(food));
  97.     apple ->next = NULL;
  98.     while(1)
  99.     {
  100.         apple -> x = rand() % (WX - 1) + 1;
  101.         apple -> y = rand() % (WY - 1) + 1;
  102.         while(head ->next != NULL)
  103.         {
  104.             if(head->x == apple->x || head->y == apple->y)
  105.                 break;
  106.             head = head ->next;
  107.         }
  108.         break;
  109.     }

  110.     gotoxy(apple -> x, apple -> y);
  111.     putchar('#');

  112.     return (apple);
  113. }
  114. int getvalue(Snake swap, int n)
  115. {
  116.     while(n--)
  117.     {
  118.         swap = swap ->next;
  119.     }
  120.     return swap->x;
  121. }

  122. int getvaluey(Snake swap, int n)
  123. {
  124.     while(n--)
  125.     {
  126.         swap = swap ->next;
  127.     }
  128.     return swap->y;
  129. }

  130. void changevalue(Snake swap, int i, int n)
  131. {
  132.    while(n--)
  133.    {
  134.        swap = swap ->next;
  135.    }
  136.    swap ->x = i;
  137. }
  138. void changevaluey(Snake swap, int i, int n)
  139. {
  140.    while(n--)
  141.    {
  142.        swap = swap ->next;
  143.    }
  144.    swap ->y = i;
  145. }

  146. void auto_move(Snake head,char direction,Food apple)
  147. {
  148.     Snake swap;
  149.     int x = 0, y = 0, n = 0;
  150.     while(!kbhit())
  151.    {

  152.        swap = head;//初始化不要忘了


  153.         if(head ->x == apple->x && head ->y == apple->y)
  154.         {
  155.             f = 0;
  156.             n = getlength(head);
  157.             x = getvalue(swap,n);
  158.             y = getvaluey(swap,n);/**不懂为啥n+ 1不对,还不太明白*/
  159.             createL(head,x , y);
  160.         }
  161.         clean(head);//我先清理的所以刚开始蛇身只有两节
  162.         /**
  163.          *  把链表倒数第二个值赋值给倒数第一个
  164.          *  这样就不会把值覆盖了
  165.             最后一个链表存储当前蛇的长度
  166.          */
  167.          n = getlength(head);

  168.         while(n--)
  169.         {
  170.             x = getvalue(swap,n);//得到第n个链表的值
  171.             y = getvaluey(swap,n);
  172.             changevalue(swap, x, n+1);//把第n个链表的值写入第n+1个链表
  173.             changevaluey(swap, y, n+1);
  174.         }
  175.         switch(direction)
  176.         {
  177.             case 'w': head ->y -= 1;break;
  178.             case 'a': head ->x -= 1;break;
  179.             case 's': head ->y += 1;break;
  180.             case 'd': head ->x += 1;break;
  181.         }



  182.         Sleep(160);
  183.         draw_snake(head);
  184.         the_end(head);
  185.    }
  186. }

  187. void control(Snake head)
  188. {
  189.     char c, o = 0;
  190.     Food apple;
  191.     while(1)
  192.     {
  193.         if(!f)//判断是否有食物
  194.         {
  195.             f = 1;
  196.             apple = show_food(head);
  197.         }

  198.        c = getch();
  199.         if(c == 'w' || c == 'a' || c == 's' || c == 'd' )
  200.         {
  201.             if((c == 'w' && o != 's') || (c == 'a' && o != 'd') || (c == 'd' && o != 'a') || (c == 's' && o != 'w'))
  202.                 {
  203.                     o = c;
  204.                     auto_move(head, c, apple);
  205.                 }

  206.         }

  207.     }
  208. }
  209. void test(Snake head)
  210. {
  211.     for(; head->next != NULL;head = head->next )
  212.     {
  213.         printf("%d,%d\n",head->x,head->y);
  214.     }
  215.     printf("%d,%d\n",head->x,head->y);
  216. }
  217. void clean(Snake head)
  218. {
  219.     Snake swa;
  220.     swa = head;
  221.     while(swa->next != NULL)
  222.     {
  223.         swa = swa->next;
  224.     }
  225.     gotoxy(swa->x, swa->y);
  226.     putchar(' ');
  227. }

  228. void the_end(Snake head)
  229. {
  230.     Snake swap = head;
  231.     if(getlength(head) > 50)
  232.     {
  233.         system("cls");
  234.         gotoxy(10,20);
  235.         printf("YOU WIN!!");
  236.         Sleep(2000);
  237.         exit(0);
  238.     }
  239.     while(swap ->next != NULL)
  240.     {
  241.         swap = swap ->next;
  242.         if(head ->x == swap ->x && head ->y == swap->y)
  243.         {
  244.             system("cls");
  245.             gotoxy(10,20);
  246.             printf("YOU DEATH!!");
  247.             Sleep(2000);
  248.             exit(0);
  249.         }

  250.     }
  251.     if(head->x >= WX || head->y >= WY || head->x == 0 || head->y == 0)
  252.     {
  253.         system("cls");
  254.         gotoxy(10,20);
  255.         printf("YOU DEATH!!");
  256.         Sleep(2000);
  257.         exit(0);
  258.     }
  259. }
  260. void gotoxy(int x, int y)//定义光标位置
  261. {
  262.     COORD wei;
  263.     wei.X = x;
  264.     wei.Y = y;
  265.     //Windows±à3ì
  266.     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), wei);
  267. }
  268. void wall()
  269. {
  270.     int x,y;

  271.     for(x = 0; x <= WX; x++)
  272.     {
  273.         if(x == 0 || x == WX)
  274.         {
  275.             for(y =0 ;y <= WY; y++)
  276.             {
  277.                 gotoxy(x , y);
  278.                 printf("+");
  279.             }
  280.         }
  281.         gotoxy(x, 0);
  282.         printf("+");
  283.         gotoxy(x, WY);
  284.         printf("+");

  285.     }

  286. }
  287. int main()
  288. {
  289.     Snake p;//链表头指针p

  290.     wall();
  291.      p = _snake();
  292.     draw_snake(p);
  293.     control(p);
  294.     system("pause");
  295.     return 0;
  296. }
复制代码

评分

参与人数 2荣誉 +7 鱼币 +7 贡献 +5 收起 理由
人造人 + 5 + 5 + 5 支持楼主!
lumber2388779 + 2 + 2 支持楼主!

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-3-17 16:30:06 | 显示全部楼层

回帖奖励 +2 鱼币

好样的 我当年也是用ASCII打印实现的

评分

参与人数 1荣誉 +1 鱼币 +1 收起 理由
qwe1231069 + 1 + 1 热爱鱼C^_^

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2017-3-17 19:34:05 | 显示全部楼层

回帖奖励 +2 鱼币

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-3-18 09:00:19 | 显示全部楼层

回帖奖励 +2 鱼币

感谢楼主   希望以后在配一张图更好
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-3-18 11:23:46 | 显示全部楼层

回帖奖励 +2 鱼币

學習學習
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-3-18 14:20:49 | 显示全部楼层

回帖奖励 +2 鱼币

我的编译器报错了啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-3-19 19:02:44 | 显示全部楼层
桃花飞舞 发表于 2017-3-18 14:20
我的编译器报错了啊

有的函数没声明
如果是其他原因,就不知道了
链表或者定义问题?你不发出错误原因,我猜不到
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-3-20 10:34:49 | 显示全部楼层

回帖奖励 +2 鱼币

好厉害,感觉学习新东西就要尝试应用,这样才能越学越有动力,向楼主学习~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-3-21 16:12:51 | 显示全部楼层
qwe1231069 发表于 2017-3-19 19:02
有的函数没声明
如果是其他原因,就不知道了
链表或者定义问题?你不发出错误原因,我猜不到

我现在看不了,等晚上可能了看下。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-3-24 10:34:24 | 显示全部楼层

回帖奖励 +2 鱼币

这么强,什么编译器
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-3-24 16:57:53 | 显示全部楼层

回帖奖励 +2 鱼币

复制代码玩了一下,感觉好萌,玩着挺开心的,谢谢楼主分享。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-3-27 23:28:11 | 显示全部楼层

回帖奖励 +2 鱼币

  1. --------------------Configuration: test - Win32 Debug--------------------
  2. Compiling...
  3. test.c
  4. D:\C语言练习缓存\test\test.c(109) : error C2275: 'Food' : illegal use of this type as an expression
  5.         D:\C语言练习缓存\test\test.c(24) : see declaration of 'Food'
  6. D:\C语言练习缓存\test\test.c(109) : error C2146: syntax error : missing ';' before identifier 'apple'
  7. D:\C语言练习缓存\test\test.c(109) : error C2065: 'apple' : undeclared identifier
  8. D:\C语言练习缓存\test\test.c(111) : warning C4047: '=' : 'int ' differs in levels of indirection from 'struct food *'
  9. D:\C语言练习缓存\test\test.c(112) : error C2223: left of '->next' must point to struct/union
  10. D:\C语言练习缓存\test\test.c(115) : error C2223: left of '->x' must point to struct/union
  11. D:\C语言练习缓存\test\test.c(116) : error C2223: left of '->y' must point to struct/union
  12. D:\C语言练习缓存\test\test.c(119) : error C2223: left of '->x' must point to struct/union
  13. D:\C语言练习缓存\test\test.c(119) : error C2223: left of '->y' must point to struct/union
  14. D:\C语言练习缓存\test\test.c(126) : error C2223: left of '->x' must point to struct/union
  15. D:\C语言练习缓存\test\test.c(126) : error C2223: left of '->y' must point to struct/union
  16. D:\C语言练习缓存\test\test.c(126) : error C2198: 'gotoxy' : too few actual parameters
  17. D:\C语言练习缓存\test\test.c(129) : warning C4047: 'return' : 'void *' differs in levels of indirection from 'int '
  18. 执行 cl.exe 时出错.

  19. test.obj - 1 error(s), 0 warning(s)
复制代码

有错误啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-3-28 11:26:04 | 显示全部楼层

我不会改
像第一个错,你可以定义一个结构体,在定义一个结构体变量。看它报错不
如果报错就不是我的锅了,这锅要编译器来背。我用的 gcc -wall
不报错的话,就是我的过了。只能说我是新手水平太菜了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-3-28 18:43:39 | 显示全部楼层
qwe1231069 发表于 2017-3-28 11:26
我不会改
像第一个错,你可以定义一个结构体,在定义一个结构体变量。看它报错不
如果报错就不是我的锅 ...

我是用VC++6.0的,我也不清楚这错误是什么情况,我也是个菜鸟
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-19 09:30

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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