找回密码
 立即注册

QQ登录

只需一步,快速开始

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

新人报道,第一帖就发个C语言程序吧,用C语言做的贪吃蛇

[复制链接]
跳转到指定楼层
楼主
感兴趣的可以下载共同学习学习


单片机源程序如下:
  1. #include<stdio.h>
  2. #include<windows.h>
  3. #include<dos.h>
  4. #include<time.h>
  5. #include<stdlib.h>
  6. #include <conio.h>
  7. #define WIDTH 20
  8. #define HEIGTH 20
  9. #define SX 20//初始x坐标
  10. #define SY 10//初始y坐标
  11. #define LENGTH 1
  12. #define LEFT 75
  13. #define RIGHT 77
  14. #define UP 72
  15. #define BUTTOM 80
  16. #define DIC UP//初始方向;
  17. #define STEP 1
  18. #define SL 1//每增加多少积分提升难度
  19. #define RED FOREGROUND_RED
  20. #define BLUE FOREGROUND_BLUE
  21. #define GREEN FOREGROUND_GREEN
  22. #define COLOR RED //蛇颜色 可通过RGB相加得出
  23. #define BGCOLOR RED+BLUE+GREEN//背景颜色 可通过RGB相加得出
  24. struct snake
  25. {
  26.         int x;
  27.         int y;
  28. };
  29. struct node
  30. {
  31.         snake point;
  32.         node *next;
  33.        
  34. };
  35. char control;
  36. int total=0,nd;
  37. snake food;

  38. void HideCursor()
  39. {
  40. CONSOLE_CURSOR_INFO cursor_info = {1, 0};
  41. SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
  42. }
  43. void movexy(int x, int y)
  44. {
  45. COORD coord;   
  46. HANDLE hscr;   
  47. coord.X = x;   
  48. coord.Y = y;
  49. hscr = GetStdHandle(STD_OUTPUT_HANDLE);
  50. SetConsoleCursorPosition(hscr, coord);  
  51. }
  52. void bgcolr()
  53. {
  54.         HANDLE hscr = GetStdHandle(STD_OUTPUT_HANDLE);
  55.         SetConsoleTextAttribute(hscr,BGCOLOR);
  56. }
  57. void showsnake(int x,int y)
  58. {
  59.         HANDLE hscr = GetStdHandle(STD_OUTPUT_HANDLE);
  60.         SetConsoleTextAttribute(hscr,FOREGROUND_INTENSITY|COLOR);
  61.         movexy(x,y);
  62.         printf("%s","■") ;
  63.         bgcolr();
  64. }

  65. void dig(node *v,node *n)//蛇向前走
  66. {
  67.         if(n->next!=NULL)
  68.                 dig(n,n->next);
  69.         n->point=v->point;
  70. }
  71. void update(node *head )//更新蛇位置
  72. {
  73.         int step=STEP;
  74.         node *ol=head;
  75.         snake v=head->point;
  76.         if(control!=LEFT&&control!=RIGHT&&control!=BUTTOM&&control!=UP)return;
  77.         while(ol->next!=NULL)
  78.         {
  79.                 ol=ol->next;
  80.         }
  81.                 movexy(ol->point.x,ol->point.y);
  82.                 printf("%s","□");
  83.         dig(head,head->next);
  84.                
  85.        
  86.                
  87.                 switch(control)
  88.                         {
  89.                                 case LEFT:step=-1*step;
  90.                                 case RIGHT:v.x=v.x+2*step;v.y=v.y;head->point=v;break;
  91.                                 case UP:step=-1*step;
  92.                                 case BUTTOM:v.y=v.y+step;v.x=v.x;head->point=v;break;
  93.                         }
  94.                
  95.        

  96. }
  97. node* newlist()//用链表存蛇并显示出来
  98. {
  99.         node *head=(node*)malloc(sizeof(node)),*ol;
  100.         int step=STEP;
  101.         ol=head;
  102.         ol->next=NULL;
  103.         snake v={SX+12,SY+6};
  104.         ol->point=v;
  105.         for(int i=0;i<LENGTH;i++)
  106.                 {
  107.                         node *t=(node*)malloc(sizeof(node));
  108.                         ol->next=t;
  109.                         t->next=NULL;
  110.                         v=ol->point;
  111.                         switch(DIC)
  112.                         {
  113.                                 case LEFT:step=-1*STEP;
  114.                                 case RIGHT:v.x=v.x+2*step;v.y=v.y;break;
  115.                                 case UP:step=-1*STEP;
  116.                                 case BUTTOM:v.y=v.y+step;v.x=v.x;break;
  117.                         }
  118.                         t->point=v;
  119.                         showsnake(v.x,v.y);
  120.                         ol=t;
  121.                 }
  122.                 return head;
  123. }
  124. void add(node *t)//增加蛇身体
  125. {
  126.         node *ol=(node*)malloc(sizeof(node));
  127.         ol->next=NULL;
  128.         while(t->next!=NULL)
  129.         {
  130.                 t=t->next;
  131.         }
  132.         ol->point=t->point;
  133.         t->next=ol;
  134. }
  135. void show(node *node)//显示链表是否正常;
  136. {
  137.         while(node!=NULL)
  138.         {
  139.                 printf("%d,%d\n",node->point.x,node->point.y);
  140.                 node=node->next;
  141.         }
  142. }
  143. void init()//初始化背景,并去掉无关信息
  144. {
  145.         bgcolr();
  146.         srand(time(0));
  147.         movexy(SX+WIDTH-WIDTH/2,SY+HEIGTH+5);
  148.         printf("                                             ");
  149.         HideCursor();
  150.         for(int i=0;i<WIDTH;i++)
  151.         {       
  152.                 movexy(SX,SY+i);
  153.                 for(int j=0;j<HEIGTH;j++)
  154.                         {
  155.                                 printf("%s","□");
  156.                         }
  157.         }
  158. }
  159. void run(node *node)//显示蛇头
  160. {
  161.         //snake v=node->point;
  162.         showsnake(node->point.x,node->point.y);
  163.        
  164.         char *s[]={"简单","困难","噩梦"};
  165.         movexy(SX+2*WIDTH+2,SY+HEIGTH-HEIGTH/2);
  166.         printf("难度:%s    ",s[nd]);
  167.         movexy(SX+2*WIDTH+2,SY+HEIGTH-HEIGTH/2+2);
  168.         if(total%5==9)add(node);
  169.         printf("积分:%d    ",total);
  170. }
  171. void over()
  172. {
  173.         movexy(SX+2*WIDTH-20,SY+HEIGTH+5);
  174.         printf("游戏结束按esc退出或方向键重新游戏");
  175. }
  176. int eatf(node *node)//判断蛇吃食物
  177. {
  178.         if(node->point.x==food.x&&node->point.y==food.y)return 1;
  179.         return 0;
  180. }
  181. int cf(node *node)//食物合法性检查
  182. {
  183.         while(node!=NULL)
  184.         {
  185.                 if(node->point.x!=food.x&&node->point.y!=food.y&&food.x%2==0)return 1;
  186.                 node=node->next;
  187.         }
  188.         return 0;
  189. }

  190. void addfood(node *head)//添加食物并显示
  191. {
  192.        
  193.         int x,y;
  194.         while(1)
  195.         {
  196.                 x=rand()%(2*WIDTH)+SX;
  197.                 y=rand()%(HEIGTH)+SY;
  198.                 food.x=x;
  199.                 food.y=y;
  200.                 if(cf(head))break;
  201.                  
  202.         }
  203.        
  204.         movexy(x,y);
  205.         printf("%s","★");
  206. }
  207. int main(void)
  208. {   
  209.         movexy(SX+WIDTH,SY-4);
  210.         printf("贪吃蛇");
  211.         movexy(SX+WIDTH-WIDTH/2,SY-3);
  212.         printf("可随时按ESC终止游戏");
  213.         movexy(SX+WIDTH-WIDTH/2,SY-2);
  214.         printf("请按方向键开始游戏");
  215.         int sd[]={500,250,100};
  216.         while(1)
  217.         {
  218.                 nd=0;
  219.                 init();
  220.                 node *head=newlist();
  221.                 control=0;
  222.                 total=0;
  223.                 addfood(head);
  224.                 //show(head);
  225.                 while(1)
  226.                 {
  227.                         while(kbhit())
  228.                         {
  229.                                 movexy(SX+WIDTH-WIDTH/2,SY-2);
  230.                                
  231.                                 char c;
  232.                                 if((c=getch())==LEFT||c==RIGHT||c==UP||c==BUTTOM||c==27)
  233.                                 {       
  234.                                         printf("                    ");
  235.                                         if(c==LEFT&&control==RIGHT||control==LEFT&&c==RIGHT)continue;
  236.                                         if(c==UP&&control==BUTTOM||control==UP&&c==BUTTOM)continue;
  237.                                         control=c;
  238.                                 }
  239.                         }
  240.                         if(control==27)break;
  241.                         update(head);
  242.                         if(head->point.x<SX||head->point.y<SY||head->point.x>=SX+2*WIDTH||head->point.y>=SY+HEIGTH)break;
  243.                         if(eatf(head))
  244.                         {
  245.                                 addfood(head);
  246.                                 total++;//积分增加
  247.                                 add(head);
  248.                                 if(nd<2&&total%SL==0)nd++;//难度增加
  249.                         }
  250.                         run(head);
  251.                         Sleep(sd[nd]);
  252.                 }
  253.                 over() ;
  254.                
  255.                 if(getch()==27)break;
  256.         }
  257.         return 0;
  258. }
复制代码

所有资料51hei提供下载:
贪吃蛇.rar (1.12 MB, 下载次数: 10)


评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶1 踩
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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