C语言程序设计 飞行小鸟游戏

通过C语言程序设计开发的一款飞行小鸟游戏,拥有3种游戏模式:玩家操作模式,自动游戏模式,人工智能模式

代码如下:

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

 

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值