鱼C论坛

 找回密码
 立即注册
查看: 2211|回复: 2

[技术交流] C语言小游戏(仅限Windows)

[复制链接]
发表于 2021-2-12 18:38:14 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 Minecraft程序猿 于 2021-2-13 15:00 编辑

昨天晚上闲来无事写了一个小游戏,今天改进了一下,按WASD,wasd移动小人,按1~5切换小人样式,按Q,q退出程序,有3个随机运动敌人,碰撞检测系统,分数(步数)统计,特殊颜色高亮,Game Over界面,游戏结束标题改变,游戏暂停(按P,p),部分功能简化,写了一下午,编译时需要如果是gcc加上 -lpthread 参数, -std=c99 参数,仅限Windows,附上截图和源代码(268行),不得不说Virsual Studio Code真的好用,编译环境:gcc(MinGW-w64)
  1. #include <time.h>
  2. #include <stdio.h>
  3. #include <conio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <windows.h>
  7. #include <pthread.h>

  8. unsigned int x = 0, y = 0, w = 0, step = 0;
  9. unsigned int x1 = 12, x2 = 32, x3 = 8;
  10. unsigned int *xp = &x, *yp = &y;
  11. unsigned int y1_ = 12, y2 = 10, y3 = 6;
  12. unsigned int join1, join2;
  13. unsigned char key, line;
  14. unsigned char *custom[] = {"*(^_^)*", "*_*", "@^@", "QwQ", "QAQ"};
  15. unsigned char *peoc = "$.$";

  16. HANDLE handle;
  17. CONSOLE_CURSOR_INFO cursor;

  18. void gotoxy(unsigned int x, unsigned int y)
  19. {
  20.     COORD coord = {x, y};
  21.     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
  22. }

  23. void move()
  24. {
  25.     system("cls");
  26.     gotoxy(0, y+25);
  27.     printf("Step:%u", step);
  28.     gotoxy(0, y+27);
  29.     printf("Player\tx:%u, y:%u\n", x, y);
  30.     gotoxy(0, y+29);
  31.     printf("Mob-1\tx:%u, y:%u", x1, y1_);
  32.     gotoxy(0, y+30);
  33.     printf("Mob-2\tx:%u, y:%u", x2, y2);
  34.     gotoxy(0, y+31);
  35.     printf("Mob-3\tx:%u, y:%u", x3, y3);
  36.     gotoxy(x, 0);

  37.     gotoxy(x, y);
  38.     SetConsoleTextAttribute(handle, 0xA);
  39.     printf("%s", custom[w]);
  40.     SetConsoleTextAttribute(handle, 0xF);

  41.     gotoxy(x1, y1_);
  42.     printf("%s", peoc);
  43.     gotoxy(x2, y2);
  44.     printf("%s", peoc);
  45.     gotoxy(x3, y3);
  46.     printf("%s", peoc);
  47. }

  48. void left(unsigned int *x)
  49. {
  50.     if (*x > 0)
  51.     {
  52.         (*x)--;
  53.     }
  54. }
  55. void right(unsigned int *x)
  56. {
  57.     (*x)++;
  58. }
  59. void up(unsigned int *y)
  60. {
  61.     if (*y > 0)
  62.     {
  63.         (*y)--;
  64.     }
  65. }
  66. void down(unsigned int *y)
  67. {
  68.     (*y)++;
  69. }

  70. void peo_move(void)
  71. {
  72.     time_t t;
  73.     static unsigned int *px, *py;
  74.     srand((unsigned)time(&t));
  75.     for(int num = 0; num < 3; num++)
  76.     {
  77.         switch (num)
  78.         {
  79.             case 0: px = &x1; py = &y1_; break;
  80.             case 1: px = &x2; py = &y2; break;
  81.             case 2: px = &x3; py = &y3;
  82.         }

  83.         switch (rand() % 4 + 0)
  84.         {
  85.             case 0: up(py); break;
  86.             case 1: down(py); break;
  87.             case 2: left(px); break;
  88.             case 3: right(px);
  89.         }
  90.     }
  91. }

  92. void gameover(void)
  93. {
  94.     SetConsoleTitle("Game Over...");
  95.     system("cls");
  96.     printf("\n\
  97. ###################################\n\
  98. #+-------------------------------+#\n\
  99. #|                               |#\n\
  100. #|                               |#\n\
  101. #|           GAME OVER           |#\n\
  102. #|            Step:%d        \t |#\n\
  103. #|                               |#\n\
  104. #|                               |#\n\
  105. #+-------------------------------+#\n\
  106. ###################################\n\n", step);
  107.     Sleep(2000);
  108.     SetConsoleTextAttribute(handle, 0x7);
  109.     system("pause && cls");
  110.     exit(0);
  111. }

  112. void player(void)
  113. {
  114.     while (1)
  115.     {
  116.         peo_move();
  117.         gotoxy(0, 0);
  118.         key = getch();
  119.         switch (key)
  120.         {
  121.             case 'W':
  122.             case 'w':
  123.                 step++;
  124.                 up(yp);
  125.                 break;
  126.             case 'S':
  127.             case 's':
  128.                 step++;
  129.                 down(yp);
  130.                 break;
  131.             case 'A':
  132.             case 'a':
  133.                 step++;
  134.                 left(xp);
  135.                 break;
  136.             case 'D':
  137.             case 'd':
  138.                 step++;
  139.                 right(xp);
  140.                 break;
  141.             case '1': w = 0;break;
  142.             case '2': w = 1;break;
  143.             case '3': w = 2;break;
  144.             case '4': w = 3;break;
  145.             case '5': w = 4;break;
  146.             case 'P':
  147.             case 'p':
  148.                 system("cls");
  149.                 printf("\n\
  150. ###################################\n\
  151. #+-------------------------------+#\n\
  152. #|                               |#\n\
  153. #|                               |#\n\
  154. #|          GAME PAUSE           |#\n\
  155. #|            Step:%d        \t |#\n\
  156. #|                               |#\n\
  157. #|                               |#\n\
  158. #+-------------------------------+#\n\
  159. ###################################\n\n", step);
  160.                 system("pause");
  161.                 break;
  162.             case 'Q':
  163.             case 'q':
  164.                 system("cls");
  165.                 SetConsoleTextAttribute(handle, 0x7);
  166.                 exit(0);
  167.         }

  168.         gotoxy(0, 0);
  169.         move();
  170.         if (((y == y1_) && (line = 1)) || ((y == y2) && (line = 2)) || ((y == y3) && (line = 3)))
  171.         {
  172.             for (int i = 0; i < strlen(custom[w]); i++)
  173.             {
  174.                 for (int j = 0; j < 3; j++)
  175.                 {
  176.                     if (((x+i == x1+j) && (line == 1)) || ((x+i == x2+j) && (line == 2)) || ((x+i == x3+j) && (line == 3)))
  177.                     {
  178.                         gameover();
  179.                     }
  180.                 }
  181.             }
  182.         }
  183.     }
  184. }

  185. void HideCursor(void)
  186. {
  187.     handle = GetStdHandle(STD_OUTPUT_HANDLE);
  188.     cursor.bVisible = FALSE;
  189.     cursor.dwSize = sizeof(cursor);
  190.     SetConsoleCursorInfo(handle, &cursor);
  191. }

  192. int main(void)
  193. {
  194.     SetConsoleTitle("Game Fun *(^_^)*");
  195.     system("cls");
  196.     HideCursor();
  197.     gotoxy(0, 0);
  198.     SetConsoleTextAttribute(handle, 0x1);
  199.     printf("Game Fun *(^_^)*\nCopyright 2021 The Little Box Technology\nBy Logic\n2021.2.12(Fri) 15:54\n\n");
  200.     SetConsoleTextAttribute(handle, 0xA);
  201.     printf("     W");
  202.     SetConsoleTextAttribute(handle, 0xF);
  203.     printf("\n1.  ");
  204.     SetConsoleTextAttribute(handle, 0xA);
  205.     printf("A S D");
  206.     SetConsoleTextAttribute(handle, 0xD);
  207.     printf("\tMove the Role\n\n");
  208.     SetConsoleTextAttribute(handle, 0xF);
  209.     printf("2.  ");
  210.     SetConsoleTextAttribute(handle, 0xA);
  211.     printf(" Q");
  212.     SetConsoleTextAttribute(handle, 0xD);
  213.     printf("\t\tQuit the Game\n\n");
  214.     SetConsoleTextAttribute(handle, 0xF);
  215.     printf("3.  ");
  216.     SetConsoleTextAttribute(handle, 0xA);
  217.     printf(" P");
  218.     SetConsoleTextAttribute(handle, 0xD);
  219.     printf("\t\tPause the Game\n\n");
  220.     SetConsoleTextAttribute(handle, 0xF);
  221.     printf("4.  ");
  222.     SetConsoleTextAttribute(handle, 0xA);
  223.     printf("1~5\t");
  224.     SetConsoleTextAttribute(handle, 0xD);
  225.     printf("\tSwitch Roles\n\n\n");
  226.     SetConsoleTextAttribute(handle, 0x7);
  227.     printf("_________________________________________\nPress Any Key To");
  228.     SetConsoleTextAttribute(handle, 0xF);
  229.     printf(" Start the Game...");

  230.     pthread_t people, playerM;
  231.     int thread1, playerT;
  232.     thread1 = pthread_create(&people, NULL, (void *)&peo_move, NULL);
  233.     playerT = pthread_create(&playerM, NULL, (void *)&player, NULL);
  234.     if ((thread1 != 0) && (playerT != 0))
  235.     {
  236.         printf("线程无法正常创建...");
  237.         Sleep(1500);
  238.         exit(1);
  239.     }
  240.     join1 = pthread_join(people, NULL);
  241.     join2 = pthread_join(playerM, NULL);
  242.     if ((join1 != 0) && (join2 != 0))
  243.     {
  244.         printf("线程无法正常运行...");
  245.         Sleep(1500);
  246.         exit(1);
  247.     }

  248.     gotoxy(0, 0);
  249.     printf("%s", custom[w]);

  250.     return 0;
  251. }
复制代码
18-36-00-000.jpg
18-35-57-057.jpg
18-36-33-033.jpg
18-36-35-035.jpg
18-36-38-038.jpg
18-36-43-043.jpg
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-2-12 19:15:11 | 显示全部楼层
编译不出来啊www
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-2-12 22:11:59 From FishC Mobile | 显示全部楼层
溴百里香酚菜 发表于 2021-2-12 19:15
编译不出来啊www

编译时需要加上-lpthread参数导入pthread库哦,而且这个程序仅限Windows
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-2 00:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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