[C] Re: 贪吃蛇完整版 c语言游戏 →→→→→进入此内容的聊天室

来自 Thundering Iguana, 2024-03-07, 写在 C, 查看 20 次. 这张便签是回复 贪吃蛇完整版 c语言游戏 来自 Gray Sloth - 对比版本
URL http://www.code666.cn/view/c320638e
  1.  
  2. // 必要的头文件
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <conio.h>
  6. #include <time.h>
  7. #include <windows.h>
  8. #include <string.h>
  9.  
  10.  
  11. // 定义标记上下左右的明示常量
  12. #define UP 1
  13. #define DOWN 2
  14. #define LEFT 3
  15. #define RIGHT 4
  16. #define ESC 5
  17. #define FOOD 10
  18. // 定义表示位置的结构体类型
  19. typedef struct snake{
  20.         int x;
  21.         int y;
  22.         struct snake *next;
  23. }snake;
  24. // 定义全局变量
  25. int score = 0; // 当前得分
  26. int speed = 200; // 存储当前速度
  27. int status;
  28. snake *tail, *head; // 存储蛇头蛇尾
  29. snake *food, *q;// q用于遍历链表
  30.  
  31. HANDLE hOUT;
  32. void gotoxy(int x, int y); // 设置光标位置
  33. int choice(void); // 载入游戏界面
  34. int color(int c); // 设置字体颜色
  35. void printGame(void); // 打印游戏界面
  36. void printSnake(void); // 打印蛇身
  37. void printFood(void); // 打印食物
  38. void printTips(void); // 打印提示
  39. void snakeMove(void); // 主操作函数
  40. int biteSelf(void); // 判断是否咬到了自己
  41. int encounterWall(void); // 判断是否撞墙
  42. void keyboardControl(void); // 获取击键
  43. void speedUp(void); // 加速
  44. void speedDown(void); // 减速
  45. int endGame(void); // 结束函数;
  46. char *s_gets(char *st, int n); // 读取字符
  47. void frees(snake *); // 释放内存
  48.  
  49. int main(int argc, char *argv[]){
  50.         while (1)
  51.         {
  52.                 if (choice() == 1)
  53.                         keyboardControl();
  54.                 else
  55.                 {
  56.                         gotoxy(5, 15);
  57.                         printf("按任意键返回");
  58.                         getchar(); // 去除前一个前导换行
  59.                         while (1)
  60.                         {
  61.                                 if (getchar())
  62.                                 {
  63.                                         system("cls");
  64.                                         break;
  65.                                 }
  66.                         }
  67.                 }
  68.         }
  69.         frees(head);
  70.  
  71.         return 0;
  72. }
  73.  
  74. void gotoxy(int x, int y)
  75. {
  76.         COORD c;
  77.         c.X = x;
  78.         c.Y = y;
  79.         SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
  80. }
  81.  
  82. int choice(void)
  83. {
  84.         int yourchoice;
  85.         // 画出界面
  86.         gotoxy(35, 5);
  87.         color(11);
  88.         printf("\t贪吃蛇大作战\n");
  89.         printf("\n\n");
  90.         color(13);
  91.         printf("\t\t★★★★★★★★  Snake!");
  92.         printf("\t\t★★★★★★★★  Snake!");
  93.         gotoxy(25, 15);
  94.         color(12);
  95.         printf("1.进入游戏\t2.查看说明\t3.退出游戏\n");
  96.         color(11);
  97.         printf("请选择:");
  98.         scanf("%d", &yourchoice);
  99.         switch (yourchoice)
  100.         {
  101.         case 1:
  102.                 system("cls");
  103.                 // 初始化
  104.                 printGame();
  105.                 printSnake();
  106.                 printFood();
  107.                 break;
  108.         case 2:
  109.                 system("cls");
  110.                 printTips();
  111.                 break;
  112.         case 3:
  113.                 system("cls");
  114.                 gotoxy(30, 10);
  115.                 color(11);
  116.                 printf("Bye!");
  117.                 exit(0);
  118.         default:
  119.                 system("cls");
  120.                 printf("没有此序号,请输入1,2或3\n");
  121.                 Sleep(2000);
  122.                 system("cls");
  123.         }
  124.         return yourchoice;
  125. }
  126. int color(int c)
  127. {
  128.         SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c);        //更改文字颜色
  129.         return 0;
  130. }
  131. void printGame()
  132. {
  133.         int i, j;
  134.         gotoxy(5, 5);
  135.         printf("游戏载入中...请稍后");
  136.         Sleep(2000);
  137.         system("cls");
  138.  
  139.         // 打印上下界面
  140.         for (i = 0; i <= 50; i += 2)
  141.         {
  142.                 gotoxy(i, 0);
  143.                 printf("□");
  144.                 gotoxy(i, 25);
  145.                 printf("□");
  146.         }
  147.         // 打印左右界面
  148.         for (i = 0; i <= 25; i += 1)
  149.         {
  150.                 gotoxy(0, i);
  151.                 printf("□");
  152.                 gotoxy(50, i);
  153.                 printf("□");
  154.         }
  155.         // 打印中间网格
  156.         for (i = 1; i <= 24; i += 1)
  157.         {
  158.                 for (j = 2; j <= 48; j += 2)
  159.                 {
  160.                         gotoxy(j, i);
  161.                         color(11);
  162.                         printf("■");
  163.                 }
  164.         }
  165.         // 打印右侧的规则和计分栏
  166.         gotoxy(60, 13);
  167.         printf("当前分数:%d分,当前速度%d", score, speed);
  168.         gotoxy(60, 15);
  169.         printf("用↑ ↓ ← →分别控制蛇的移动\n");
  170.         gotoxy(60, 18);
  171.         printf("每次获取食物加10分  按下F1加速,F2减速,空格暂停\n");
  172.         gotoxy(60, 20);
  173.         printf("不能撞墙和咬到自己!");
  174.         gotoxy(60, 22);
  175.         printf("速度不低于100,不高于300");
  176. }
  177. void printSnake(void)
  178. {
  179.         int i;
  180.         // 设定蛇尾(16,13),头插入,初始向右
  181.         tail = (snake*)malloc(sizeof(snake));
  182.         tail->x = 16;
  183.         tail->y = 13;
  184.         tail->next = NULL;
  185.         // 设定初始蛇长是4
  186.         for (i = 1; i <= 4; i++)
  187.         {
  188.                 head = (snake*)malloc(sizeof(snake));
  189.                 head->next = tail;
  190.                 head->x = 16 + 2 * i;
  191.                 head->y = 13;
  192.                 tail = head; // 头成为尾
  193.         }
  194.         // 输出蛇身
  195.         while (tail->next)
  196.         {
  197.                 gotoxy(tail->x, tail->y);
  198.                 color(14);
  199.                 printf("★");
  200.                 tail = tail->next;
  201.         }
  202. }
  203. void printFood(void)
  204. {
  205.         srand((unsigned)time(NULL)); // 利用时钟修改种子
  206.         food = (snake*)malloc(sizeof(snake));
  207.         food->x = 1; // 初始化x坐标
  208.         while (food->x % 2 && food->x)
  209.         {
  210.                 food->x = rand() % 46 + 2;// 2-48
  211.         }
  212.         food->y = rand() % 23 + 1; // 1-24
  213.         q = head; // 不改变头遍历链表
  214.         while (q->next)
  215.         {
  216.                 if (q->x == food->x && q->y == food->y)
  217.                 {
  218.                         free(food);
  219.                         printFood();
  220.                 }
  221.                 else
  222.                 {
  223.                         gotoxy(food->x, food->y);
  224.                         color(12);
  225.                         printf("●");
  226.                         break;
  227.                 }
  228.         }
  229. }
  230. void printTips(void)
  231. {
  232.         color(11);
  233.         printf("**************Tips***************\n");
  234.         printf("* 采用合理的速度可以获得更高的分数哦!\n");
  235.         printf("* 一定不要撞到自己或者两边的墙!\n");
  236.         printf("* 空格暂停  ESC退出  F1加速   F2减速\n");
  237.         printf("* 游戏过程中按ESC退出游戏!\n");
  238. }
  239. void snakeMove(void)
  240. {
  241.         snake *snakenext;
  242.         snakenext = (snake*)malloc(sizeof(snake));
  243.         if (biteSelf())
  244.         {
  245.                 gotoxy(60, 11);
  246.                 printf("咬到自己啦!");
  247.                 free(snakenext);
  248.                 Sleep(1500);
  249.                 system("cls");
  250.                 exit(0);
  251.         }
  252.         else if (encounterWall())
  253.         {
  254.                 gotoxy(60, 11);
  255.                 printf("撞到墙啦!");
  256.                 free(snakenext);
  257.                 Sleep(1500);
  258.                 system("cls");
  259.                 exit(0);
  260.         }
  261.         else
  262.         {
  263.                 // 前两个条件判断完成才开始移动
  264.                 Sleep(350 - speed);
  265.                 if (status == UP)
  266.                 {
  267.                         snakenext->x = head->x;
  268.                         snakenext->y = head->y - 1;
  269.                         snakenext->next = head;
  270.                         head = snakenext;
  271.                         q = head;
  272.                         if (snakenext->x == food->x && snakenext->y == food->y)
  273.                         {
  274.                                 while (q)
  275.                                 {
  276.                                         gotoxy(q->x, q->y);
  277.                                         color(14);
  278.                                         printf("★");
  279.                                         q = q->next;
  280.                                 }
  281.                                 score += FOOD;
  282.                                 gotoxy(60, 13);
  283.                                 printf("当前分数:%d分,当前速度%d", score, speed);
  284.                                 printFood();
  285.                         }
  286.                         else
  287.                         {
  288.                                 while (q->next->next)
  289.                                 {
  290.                                         gotoxy(q->x, q->y);
  291.                                         color(14);
  292.                                         printf("★");
  293.                                         q = q->next;
  294.                                 }
  295.                                 gotoxy(q->next->x, q->next->y);
  296.                                 color(11);
  297.                                 printf("■");
  298.                                 free(q->next);
  299.                                 q->next = NULL;
  300.                         }
  301.                 }
  302.                 else if (status == DOWN)
  303.                 {
  304.                         snakenext->x = head->x;
  305.                         snakenext->y = head->y + 1;
  306.                         snakenext->next = head;
  307.                         head = snakenext;
  308.                         q = head;
  309.                         if (snakenext->x == food->x && snakenext->y == food->y)
  310.                         {
  311.                                 while (q)
  312.                                 {
  313.                                         gotoxy(q->x, q->y);
  314.                                         color(14);
  315.                                         printf("★");
  316.                                         q = q->next;
  317.                                 }
  318.                                 score += FOOD;
  319.                                 gotoxy(60, 13);
  320.                                 printf("当前分数:%d分,当前速度%d", score, speed);
  321.                                 printFood();
  322.                         }
  323.                         else
  324.                         {
  325.                                 while (q->next->next)
  326.                                 {
  327.                                         gotoxy(q->x, q->y);
  328.                                         color(14);
  329.                                         printf("★");
  330.                                         q = q->next;
  331.                                 }
  332.                                 gotoxy(q->next->x, q->next->y);
  333.                                 color(11);
  334.                                 printf("■");
  335.                                 free(q->next);
  336.                                 q->next = NULL;
  337.                         }
  338.                 }
  339.                 else if (status == LEFT)
  340.                 {
  341.                         snakenext->x = head->x - 2;
  342.                         snakenext->y = head->y;
  343.                         snakenext->next = head;
  344.                         head = snakenext;
  345.                         q = head;
  346.                         if (snakenext->x == food->x && snakenext->y == food->y)
  347.                         {
  348.                                 while (q)
  349.                                 {
  350.                                         gotoxy(q->x, q->y);
  351.                                         color(14);
  352.                                         printf("★");
  353.                                         q = q->next;
  354.                                 }
  355.                                 score += FOOD;
  356.                                 gotoxy(60, 13);
  357.                                 printf("当前分数:%d分,当前速度%d", score, speed);
  358.                                 printFood();
  359.                         }
  360.                         else
  361.                         {
  362.                                 while (q->next->next)
  363.                                 {
  364.                                         gotoxy(q->x, q->y);
  365.                                         color(14);
  366.                                         printf("★");
  367.                                         q = q->next;
  368.                                 }
  369.                                 gotoxy(q->next->x, q->next->y);
  370.                                 color(11);
  371.                                 printf("■");
  372.                                 free(q->next);
  373.                                 q->next = NULL;
  374.                         }
  375.                 }
  376.                 else if (status == RIGHT)
  377.                 {
  378.                         snakenext->x = head->x + 2;
  379.                         snakenext->y = head->y;
  380.                         snakenext->next = head;
  381.                         head = snakenext;
  382.                         q = head;
  383.                         if (snakenext->x == food->x && snakenext->y == food->y)
  384.                         {
  385.                                 while (q)
  386.                                 {
  387.                                         gotoxy(q->x, q->y);
  388.                                         color(14);
  389.                                         printf("★");
  390.                                         q = q->next;
  391.                                 }
  392.                                 score += FOOD;
  393.                                 gotoxy(60, 13);
  394.                                 printf("当前分数:%d分,当前速度%d", score, speed);
  395.                                 printFood();
  396.                         }
  397.                         else
  398.                         {
  399.                                 while (q->next->next)
  400.                                 {
  401.                                         gotoxy(q->x, q->y);
  402.                                         color(14);
  403.                                         printf("★");
  404.                                         q = q->next;
  405.                                 }
  406.                                 gotoxy(q->next->x, q->next->y);
  407.                                 color(11);
  408.                                 printf("■");
  409.                                 free(q->next);
  410.                                 q->next = NULL;
  411.                         }
  412.                 }
  413.         }
  414. }
  415. int biteSelf(void)
  416. {
  417.         int x = 0; // 默认未咬到自己
  418.         q = head->next;
  419.         // 遍历蛇身
  420.         while (q->next)
  421.         {
  422.                 if (q->x == head->x && q->y == head->y)
  423.                 {
  424.                         x = 1;
  425.                 }
  426.                 q = q->next;
  427.         }
  428.  
  429.         return x;
  430. }
  431. int encounterWall(void)
  432. {
  433.         int x = 0; // 默认未撞到墙
  434.  
  435.         if (head->x == 0 || head->x == 50 || head->y == 0 || head->y == 25)
  436.                 x = 1;
  437.  
  438.         return x;
  439. }
  440. void keyboardControl(void)
  441. {
  442.         status = RIGHT; // 初始蛇向右移动
  443.         while (1)
  444.         {
  445.                 if (GetAsyncKeyState(VK_UP) && status != DOWN) // GetAsyncKeyState函数用来判断函数调用时指定虚拟键的状态
  446.                 {
  447.                         status = UP;           //如果蛇不是向下前进的时候,按上键,执行向上前进操作
  448.                 }
  449.                 else if (GetAsyncKeyState(VK_DOWN) && status != UP) // 如果蛇不是向上前进的时候,按下键,执行向下前进操作
  450.                 {
  451.                         status = DOWN;
  452.                 }
  453.                 else if (GetAsyncKeyState(VK_LEFT) && status != RIGHT) // 如果蛇不是向右前进的时候,按左键,执行向左前进
  454.                 {
  455.                         status = LEFT;
  456.                 }
  457.                 else if (GetAsyncKeyState(VK_RIGHT) && status != LEFT) // 如果蛇不是向左前进的时候,按右键,执行向右前进
  458.                 {
  459.                         status = RIGHT;
  460.                 }
  461.                 if (GetAsyncKeyState(VK_SPACE))// 空格暂停
  462.                 {
  463.                         while (1)
  464.                         {
  465.                                 Sleep(300);
  466.                                 if (GetAsyncKeyState(VK_SPACE)) // 再次按空格改变状态
  467.                                 {
  468.                                         break;
  469.                                 }
  470.  
  471.                         }
  472.                 }
  473.                 else if (GetAsyncKeyState(VK_ESCAPE))
  474.                 {
  475.                         status = ESC; // 按esc键,直接到结束界面
  476.                         if (endGame())
  477.                         {
  478.                                 Sleep(500);
  479.                                 system("cls");
  480.                                 break;
  481.                         }
  482.                 }
  483.                 else if (GetAsyncKeyState(VK_F1)) // 按F1键,加速
  484.                 {
  485.                         speedUp();
  486.                         gotoxy(60, 13);
  487.                         printf("当前分数:%d分,当前速度%d", score, speed);
  488.                 }
  489.                 else if (GetAsyncKeyState(VK_F2)) // 按F2键,减速
  490.                 {
  491.                         speedDown();
  492.                         gotoxy(60, 13);
  493.                         printf("当前分数:%d分,当前速度%d", score, speed);
  494.                 }
  495.                 snakeMove();
  496.         }
  497. }
  498. void speedUp(void)
  499. {
  500.         if (speed <= 280)
  501.                 speed += 20;
  502. }
  503. void speedDown(void)
  504. {
  505.         if (speed >= 120)
  506.                 speed -= 20;
  507. }
  508. int endGame(void)
  509. {
  510.         char x = 0;
  511.         char judge[5];
  512.  
  513.         getchar();
  514.         gotoxy(60, 9);
  515.         printf("确定退出吗?(Yes/No)");
  516.         gotoxy(60, 11);
  517.         s_gets(judge, 5);
  518.  
  519.         if (strcmp(judge, "Yes") == 0)
  520.         {
  521.                 Sleep(250);
  522.                 system("cls");
  523.                 gotoxy(40, 11);
  524.                 printf("\tBye!");
  525.                 x = 1;
  526.         }
  527.         else
  528.                 x = 0;
  529.  
  530.         return x;
  531. }
  532. char *s_gets(char *st, int n)
  533. {
  534.         char *ret_val;
  535.         char *find;
  536.  
  537.         gotoxy(60, 11);
  538.         ret_val = fgets(st, n, stdin);
  539.         if (ret_val)
  540.         {
  541.                 find = strchr(st, '\n');
  542.                 if (find)
  543.                         *find = '\0';
  544.                 else
  545.                 while (getchar() != '\n')
  546.                         continue;
  547.         }
  548.  
  549.         return ret_val;
  550. }
  551. void frees(snake *s)
  552. {
  553.         snake *current = s;
  554.  
  555.         while (current)
  556.         {
  557.                 current = s;
  558.                 s = current->next;
  559.                 free(current);
  560.         }
  561. }

回复 "Re: 贪吃蛇完整版 c语言游戏"

这儿你可以回复上面这条便签

captcha