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

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

回复 贪吃蛇完整版 c语言游戏 rss

标题 提交人 语言 时间
Re: 贪吃蛇完整版 c语言游戏 Thundering Iguana c 2 月 前.

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

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

captcha