[C] C语言程序设计开发的一款飞行小鸟游戏 →→→→→进入此内容的聊天室

来自 Sweet Parakeet, 2024-03-06, 写在 C, 查看 14 次.
URL http://www.code666.cn/view/138e7130
  1. 通过C语言程序设计开发的一款飞行小鸟游戏,拥有3种游戏模式:玩家操作模式,自动游戏模式,人工智能模式
  2.  
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <conio.h>
  7. #include <time.h>
  8. #include <windows.h>
  9. #define DIS 22
  10. #define BLAN 9
  11. #define M 16
  12. #define N 16
  13. typedef struct bird
  14. {
  15.     COORD pos;
  16.     int score;
  17. }BIRD;
  18. void CheckWall(COORD wall[]);
  19. void PrtBird(BIRD *bird);
  20. int CheckWin(COORD *wall, BIRD *bird);
  21. void Begin(void);
  22. void Gotoxy(int x,int y);
  23. BOOL SetConsoleColor(unsigned int wAttributes);
  24. void HideCursor();
  25. void GameInfo(BIRD *bird,int count,int bestSocre,char op);
  26. int Learning(double a[30][50][2],COORD state,
  27.              COORD newState,int action, BIRD *bird);
  28. int ChooseAction(double a[30][50][2],COORD state);
  29. void Menu(void);
  30. void AIPlayer(void);
  31. void AutoGame(void);
  32. void RolePlayer(void);
  33.  
  34. int main(void)
  35. {
  36.     Menu();
  37.     char op;
  38.     op=getchar();
  39.     switch(op)
  40.     {
  41.     case '1':
  42.         RolePlayer();
  43.         break;
  44.     case '2':
  45.         srand(time(NULL));
  46.         AutoGame();
  47.         break;
  48.     case '3':
  49.         srand(time(NULL));
  50.         AIPlayer();
  51.     }
  52.     return 0;
  53. }
  54.  
  55. void Menu(void)
  56. {
  57.     Begin();
  58.     Gotoxy(5,5);
  59.     printf("欢迎来到飞行小鸟游戏!");
  60.     Gotoxy(5,7);
  61.     printf("请选择游戏模式:");
  62.     Gotoxy(5,9);
  63.     printf("1——玩家操作模式\n");
  64.     Gotoxy(5,11);
  65.     printf("2——自动飞行模式\n");
  66.     Gotoxy(5,13);
  67.     printf("3——人工智能模式\n");
  68. }
  69.  
  70. //设置颜色
  71. BOOL SetConsoleColor(unsigned int wAttributes)
  72. {
  73.     HANDLE hOutput=GetStdHandle(STD_OUTPUT_HANDLE);
  74.     if (hOutput==INVALID_HANDLE_VALUE)
  75.     {
  76.         return FALSE;
  77.     }
  78.     return SetConsoleTextAttribute(hOutput,wAttributes);
  79. }
  80.  
  81. //隐藏光标
  82. void HideCursor()
  83. {
  84.     HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
  85.     CONSOLE_CURSOR_INFO CursorInfo;
  86.     GetConsoleCursorInfo(handle,&CursorInfo);
  87.     CursorInfo.bVisible=0;
  88.     SetConsoleCursorInfo(handle,&CursorInfo);
  89. }
  90.  
  91. //定位光标
  92. void Gotoxy(int x,int y)
  93. {
  94.     COORD pos={x,y};
  95.     HANDLE hOutput=GetStdHandle(STD_OUTPUT_HANDLE);
  96.     SetConsoleCursorPosition(hOutput,pos);
  97. }
  98.  
  99. //显示柱子墙体
  100. void CheckWall(COORD wall[])
  101. {
  102.     int i;
  103.     HideCursor();
  104.     srand(time(NULL));
  105.     COORD temp={wall[2].X+DIS,rand()%13+5}; //随机产生一个新的柱子
  106.     if (wall[0].X<20)
  107.     {
  108.         wall[0]=wall[1];
  109.         wall[1]=wall[2];
  110.         wall[2]=temp;      //第3个柱子换成新柱子
  111.     }
  112.     for (i=0;i<3;++i)
  113.     {
  114.         //显示上半部分柱子墙
  115.         temp.X=wall[i].X+1;
  116.         SetConsoleColor(0x0C);  //设置黑色背景,亮红色前景
  117.         for (temp.Y=2;temp.Y<wall[i].Y;temp.Y++)
  118.         {
  119.             Gotoxy(temp.X,temp.Y);
  120.             printf("※※※※※");
  121.         }
  122.         Gotoxy(temp.X,temp.Y);
  123.         printf("※※※※※");
  124.         //显示下半部分柱子墙
  125.         temp.Y +=BLAN;
  126.         Gotoxy(temp.X,temp.Y);
  127.         printf("※※※※※");
  128.         //temp.X++;
  129.         temp.Y++;
  130.         for (;(temp.Y)<26;temp.Y++)
  131.         {
  132.             Gotoxy(temp.X,temp.Y);
  133.             printf("※※※※※");
  134.         }
  135.     }
  136. }
  137. //显示小鸟
  138. void PrtBird(BIRD *bird)
  139. {
  140.     SetConsoleColor(0x0E);//设置黑色背景,亮黄色前景
  141.     Gotoxy(bird->pos.X,bird->pos.Y);
  142.     printf("o->");  //显示小鸟
  143. }
  144.  
  145. //检测小鸟是否碰撞墙体
  146. int CheckWin(COORD *wall, BIRD *bird)
  147. {
  148.     if (bird->pos.X>=wall->X)
  149.     {
  150.         if (bird->pos.Y<=wall->Y||bird->pos.Y>=wall->Y+BLAN)
  151.         {
  152.             return 0;
  153.         }
  154.     }
  155.     if (bird->pos.Y<1||bird->pos.Y>26)
  156.     {
  157.         return 0;
  158.     }
  159.     (bird->score)++;
  160.     return 1;
  161. }
  162.  
  163. //显示边界
  164. void Begin(void)
  165. {
  166.     system("cls");
  167.     Gotoxy(0,26);   //第26行显示下边界
  168.     for (int i=0;i<100;i++)
  169.         printf("~");
  170.     Gotoxy(0,1);    //第1行显示下边界
  171.     for (int i=0;i<100;i++)
  172.         printf("~");
  173.     for (int i=2;i<26;i++)
  174.     {
  175.         Gotoxy(0,i);
  176.         printf("~");
  177.         Gotoxy(99,i);
  178.         printf("~");
  179.     }
  180.     SetConsoleColor(0x0E);
  181. }
  182.  
  183. //显示游戏信息
  184. void GameInfo(BIRD *bird,int count,int bestSocre,char op)
  185. {
  186.     SetConsoleColor(0x0D);
  187.     Gotoxy(5,5);
  188.     printf("飞行小鸟游戏");
  189.     Gotoxy(5,7);
  190.     switch(op)
  191.     {
  192.     case '1':
  193.         printf("玩家操作模式");
  194.         break;
  195.     case '2':
  196.         printf("自动游戏模式");
  197.         break;
  198.     case '3':
  199.         printf("AI人工智能模式");
  200.         break;
  201.     }
  202.     Gotoxy(5,9);
  203.     printf("回合数:%d",count);
  204.     Gotoxy(5,11);
  205.     if (op=='1'||op=='2')
  206.         printf("当前分:%d",bestSocre);
  207.     else
  208.         printf("最高分:%d",bestSocre);
  209. }
  210.  
  211. //自主学习
  212. int Learning(double a[30][50][2],COORD state,
  213.              COORD newState,int action, BIRD *bird)
  214. {
  215.     double rate=0.3;
  216.     double decay=0.4;
  217.     double st=0;
  218.     int reward=1;
  219.     if (bird->pos.Y<1 || bird->pos.Y>25||
  220.         (newState.X>0 && (newState.Y<0 || newState.Y>=BLAN)))
  221.             reward = -100;
  222.     bird->score++;
  223.     st=(a[newState.X+M][newState.Y+N][0]+a[newState.X+M][newState.Y+N][1])/2;
  224.     a[state.X+M][state.Y+N][action] +=
  225.         rate*((reward+decay*st)-a[state.X+M][state.Y+N][action]);
  226.     return reward==1;
  227. }
  228.  
  229. //选择操作
  230. int ChooseAction(double a[30][50][2],COORD state)
  231. {
  232.     double eGreedy=0.9;
  233.     double action=a[state.X+M][state.Y+N][1]-
  234.                   a[state.X+M][state.Y+N][0];
  235.     if ((double)rand()/RAND_MAX>eGreedy||action==0)
  236.         return rand()%2;
  237.     return action>0;
  238. }
  239.  
  240. //AI模式
  241. void AIPlayer(void)
  242. {
  243.     double table[30][50][2]={0};
  244.     int count=1;
  245.     int bestScore=0;
  246.     while (1)
  247.     {
  248.         BIRD bird={{17,10},0};
  249.         COORD wall[3]={{30,4},{55,13},{80,8}};
  250.         int action = 0;
  251.         COORD state={0,0};
  252.         COORD newState={bird.pos.X-wall[0].X,bird.pos.Y-wall[0].Y};
  253.         do{
  254.             state=newState;
  255.             Begin();
  256.             GameInfo(&bird,count,bestScore,'3');
  257.             CheckWall(wall);
  258.             action=ChooseAction(table,state);
  259.             if (action)
  260.                 bird.pos.Y--;
  261.             else
  262.                 bird.pos.Y++;
  263.             for (int i=0;i<3;i++)
  264.                 wall[i].X--;
  265.             newState.X = bird.pos.X-wall[0].X;
  266.             newState.Y = bird.pos.Y-wall[0].Y;
  267.             PrtBird(&bird);
  268.             Sleep(200);
  269.         }while(Learning(table,state,newState,action,&bird));
  270.         if (bird.score>bestScore)
  271.             bestScore=bird.score;
  272.         count++;
  273.     }
  274. }
  275.  
  276. //自动游戏模式
  277. void AutoGame(void)
  278. {
  279.     int count=1;
  280.     int bestScore=0;
  281.     BIRD bird={{22,10},0};      //小鸟的初始位置
  282.     COORD wall[3]={{40,10},{60,6},{80,8}};      //柱子的初始位置和高度
  283.     int i;
  284.     while (CheckWin(wall,&bird))
  285.     {
  286.         Begin();           //清屏并显示上下边界和分数
  287.         GameInfo(&bird,count,bestScore,'2');        //显示游戏信息
  288.         CheckWall(wall);        //显示柱子墙
  289.         PrtBird(&bird);         //显示小鸟
  290.         Sleep(200);
  291.         bestScore++;
  292.         if (bird.pos.Y>wall[0].Y+BLAN/2)
  293.             bird.pos.Y--;
  294.         else
  295.             bird.pos.Y++;
  296.         for (i=0;i<3;i++)
  297.         {
  298.             wall[i].X--;        //柱子墙左移一格
  299.         }
  300.     }
  301. }
  302.  
  303. //玩家操作模式
  304. void RolePlayer(void)
  305. {
  306.     int count=1;
  307.     int bestScore=0;
  308.     int i;
  309.     char ch,ch1;
  310.     do{
  311.         bestScore=0;
  312.         BIRD bird={{22,10},0};      //小鸟的初始位置
  313.         COORD wall[3]={{40,10},{60,6},{80,8}};      //柱子的初始位置和高度
  314.         while (CheckWin(wall,&bird))
  315.         {
  316.             Begin();           //清屏并显示上下边界和分数
  317.             GameInfo(&bird,count,bestScore,'1');        //显示游戏信息
  318.             CheckWall(wall);        //显示柱子墙
  319.             PrtBird(&bird);         //显示小鸟
  320.             Sleep(200);
  321.             bestScore++;
  322.             if (kbhit())
  323.             {
  324.                 ch=getch();
  325.                 if (ch==' ')
  326.                    bird.pos.Y--;
  327.             }
  328.             else
  329.                 bird.pos.Y++;
  330.             for (i=0;i<3;i++)
  331.             {
  332.                 wall[i].X--;        //柱子墙左移一格
  333.             }
  334.         }
  335.         printf("游戏结束,是否继续下一局?\n");
  336.         printf("继续请按Y,结束请按其它键。\n");
  337.         ch1=getch();
  338.         count++;
  339.     }while(ch1=='y'||ch1=='Y');
  340. }

回复 "C语言程序设计开发的一款飞行小鸟游戏"

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

captcha