Chinaunix首页 | 论坛 | 博客
  • 博客访问: 164394
  • 博文数量: 35
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 401
  • 用 户 组: 普通用户
  • 注册时间: 2014-11-28 14:09
文章分类

全部博文(35)

文章存档

2015年(27)

2014年(8)

分类: C/C++

2015-08-02 19:14:23


点击(此处)折叠或打开

  1. #include<iostream>
  2. #include<stdlib.h>
  3. #include<windows.h>
  4. #include<time.h>
  5. #include<conio.h>
  6. using namespace std;

  7. HANDLE Mutex=CreateMutex(NULL,FALSE,NULL);//互斥对象

  8. int GameOver=0;
  9. int level=0;
  10. int map[23][23];
  11. //坦克种类,Normal为玩家坦克
  12. #define Normal 0
  13. #define Red 1
  14. #define Blue 2
  15. #define Green 3
  16. //方向的宏定义
  17. #define Up 0
  18. #define Down 1
  19. #define Left 2
  20. #define Right 3
  21. //地图标记的宏定义
  22. #define Empty 0
  23. #define Player 1
  24. #define PlayerBullet 2
  25. #define EnemyBullet 3
  26. #define Enemy 4

  27. int Kill;
  28. int KillRed;
  29. int KillGreen;
  30. int EnemyExist;

  31. void SetPos(int i,int j)//设定光标位置
  32. {
  33.     COORD pos={i,j};
  34.     HANDLE Out=GetStdHandle(STD_OUTPUT_HANDLE);
  35.     SetConsoleCursorPosition(Out, pos);
  36. }

  37. void HideCurSor(void)//隐藏光标
  38. {
  39.     CONSOLE_CURSOR_INFO info={1,0};
  40.     HANDLE Out=GetStdHandle(STD_OUTPUT_HANDLE);
  41.     SetConsoleCursorInfo(Out,&info);
  42. }

  43. int sharp[4][12]=
  44. {
  45.     {0,1,1,0,1,1,1,2,2,0,2,2},
  46.     {0,0,0,2,1,0,1,1,1,2,2,1},
  47.     {0,1,0,2,1,0,1,1,2,1,2,2},
  48.     {0,0,0,1,1,1,1,2,2,0,2,1},
  49. };//此数组用来保存坦克各个方向的形状信息

  50. DWORD WINAPI Bulletfly(LPVOID lpParameter);//子弹函数申明
  51. void Updata();//更新界面信息函数申明

  52. class Tank//坦克类
  53. {
  54. private:
  55.     int Direction;//方向
  56.     int hotpoint[2];//活动点
  57.     int Speed;//速度
  58.     int FirePower;//火力
  59. public:
  60.     Tank(int dir,int hot1,int hot2,int typ,int spe,int firepow)//构造函数
  61.     {
  62.         Direction=dir;
  63.         hotpoint[0]=hot1;
  64.         hotpoint[1]=hot2;
  65.         Type=typ;
  66.         Speed=spe;
  67.         FirePower=firepow;
  68.     }
  69.     int Type;//坦克的种类(详见宏定义)
  70.     int ID;//坦克在MAP中的标记(详见宏定义)
  71.     int FireEnable;//是否可以开火
  72.     int Life;//生命值
  73.     void Running();//运行函数
  74.     int Judge(int x,int y,int ID);//判断是否可以绘制坦克
  75.     void DrawTank();//重绘坦克
  76.     void Redraw();//擦除坦克
  77.     int GetSpeed()//获取速度
  78.     {
  79.         return Speed;
  80.     }
  81.     int GetFire()//获取火力
  82.     {
  83.         return FirePower;
  84.     }
  85.     int GetDirection()//获取方向
  86.     {
  87.         return Direction;
  88.     }
  89.     int GetHotX()//获取活动点坐标
  90.     {
  91.         return hotpoint[0];
  92.     }
  93.     int GetHotY()
  94.     {
  95.         return hotpoint[1];
  96.     }
  97.     void IncreaseFire()//火力+
  98.     {
  99.         FirePower++;
  100.     }
  101.     void IncreaseSpeed()//速度+
  102.     {
  103.         Speed++;
  104.     }
  105.     void ChangeDirection(int newD)//改变方向
  106.     {
  107.         Direction=newD;
  108.     }
  109.     void ChangePos(int x,int y)//改变活动点
  110.     {
  111.         hotpoint[0]=x;
  112.         hotpoint[1]=y;
  113.     }
  114. };

  115. Tank player(Right,0,0,Normal,1,1);//玩家
  116. Tank enemy(Left,20,0,Red,1,1);//敌人

  117. void Tank::DrawTank()//绘制坦克
  118. {
  119.      int i;
  120.      int nx,ny;
  121.      if(Type==Red)
  122.             SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED);
  123.         else if(Type==Blue)
  124.             SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_BLUE);
  125.         else if(Type==Green)
  126.             SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_GREEN);
  127.         else if(Type==Normal)
  128.             SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
  129.      for(i=0;i<6;i++)
  130.     {
  131.         nx=hotpoint[0]+sharp[Direction][i*2];
  132.         ny=hotpoint[1]+sharp[Direction][i*2+1];
  133.         SetPos((ny+1)*2,nx+1);//利用sharp数组相对于点x,y绘制形状
  134.         map[nx][ny]=ID;
  135.         cout<<"■";
  136.     }
  137. }

  138. void Tank::Redraw()//擦除坦克,原理同上
  139. {
  140.      int i;
  141.      int nx,ny;
  142.      for(i=0;i<6;i++)
  143.     {
  144.         nx=hotpoint[0]+sharp[Direction][i*2];
  145.         ny=hotpoint[1]+sharp[Direction][i*2+1];
  146.         map[nx][ny]=Empty;
  147.         SetPos((ny+1)*2,nx+1);
  148.         cout<<" ";
  149.     }
  150. }

  151. int Tank::Judge(int x,int y,int dir)//判断当前是否可以绘制坦克
  152. {
  153.     int i;
  154.     int nx,ny;
  155.     for(i=0;i<6;i++)
  156.     {
  157.         nx=x+sharp[dir][i*2];
  158.         ny=y+sharp[dir][i*2+1];
  159.         if(nx<0||nx>=23||ny<0||ny>=23||map[nx][ny]!=Empty)//不能绘制,返回1
  160.             return 1;
  161.     }
  162.     return 0;
  163. }


  164. void Tank::Running()//坦克运行函数
  165. {
  166.     int newD;
  167.     //坦克的运行
  168.     while(1)
  169.     {
  170.         if(Life==0)
  171.         {
  172.             EnemyExist=0;//敌人不存在
  173.             return;
  174.         }
  175.         if(GameOver==1)
  176.             return;
  177.         if(FireEnable==1&&GameOver==0)//如果可以开火
  178.         {
  179.             WaitForSingleObject(Mutex,INFINITE);//线程拥有互斥对象
  180.             FireEnable=0;//设为不可开火
  181.             HANDLE bullet=CreateThread(NULL,0,Bulletfly,&ID,0,NULL);//创建子弹线程
  182.             CloseHandle(bullet);
  183.             ReleaseMutex(Mutex);//释放互斥对象
  184.             Sleep(100);
  185.         }
  186.         WaitForSingleObject(Mutex,INFINITE);//线程拥有互斥对象
  187.         srand((int)time(0));
  188.      newD=rand()%4;
  189.         
  190.         if(newD==Up)//随机出新的方向并重新绘制坦克
  191.         {
  192.             Redraw();
  193.             if(Judge(hotpoint[0]-1,hotpoint[1],newD)==0)
  194.             {
  195.                 hotpoint[0]--;
  196.                 Direction=newD;
  197.             }
  198.             else
  199.             {
  200.                 if(Judge(hotpoint[0],hotpoint[1],newD)==0)
  201.                     Direction=newD;
  202.             }
  203.         }
  204.         else if(newD==Down)
  205.         {
  206.             Redraw();
  207.             if(Judge(hotpoint[0]+1,hotpoint[1],newD)==0)
  208.             {
  209.                 hotpoint[0]++;
  210.                 Direction=newD;
  211.             }
  212.             else
  213.             {
  214.                 if(Judge(hotpoint[0],hotpoint[1],newD)==0)
  215.                     Direction=newD;
  216.             }
  217.         }
  218.         else if(newD==Left)
  219.         {
  220.             Redraw();
  221.             if(Judge(hotpoint[0],hotpoint[1]-1,newD)==0)
  222.             {
  223.                 hotpoint[1]--;
  224.                 Direction=newD;
  225.             }
  226.             else
  227.             {
  228.                 if(Judge(hotpoint[0],hotpoint[1],newD)==0)
  229.                     Direction=newD;
  230.             }
  231.         }
  232.         else if(newD==Right)
  233.         {
  234.             Redraw();
  235.             if(Judge(hotpoint[0],hotpoint[1]+1,newD)==0)
  236.             {
  237.                 hotpoint[1]++;
  238.                 Direction=newD;
  239.             }
  240.             else
  241.             {
  242.                 if(Judge(hotpoint[0],hotpoint[1],newD)==0)
  243.                     Direction=newD;
  244.             }
  245.         }
  246.         if(GameOver==0&&Life!=0)
  247.          DrawTank();
  248.         ReleaseMutex(Mutex);//释放互斥对象
  249.         Sleep(500-80*Speed);
  250.     }
  251. }

  252. /*********************子弹线程函数*******************/
  253. DWORD WINAPI Bulletfly(LPVOID lpParameter)
  254. {
  255.     int *ID=(int *)lpParameter;//ID用来获取发射子弹坦克的ID
  256.     int Pos[2];//子弹活动点
  257.     int direction;
  258.     int Speed;
  259.     int type;
  260.     int hit=0;//击中标记
  261.     int oldx,oldy;//旧活动点
  262.     int flag=0;//子弹是否有移动的标记
  263.     if(*ID==Player)//如果是玩家坦克
  264.     {
  265.         type=PlayerBullet;
  266.         direction=player.GetDirection();
  267.         Speed=player.GetFire();
  268.         Pos[0]=player.GetHotX();
  269.         Pos[1]=player.GetHotY();
  270.     }
  271.     else if(*ID==Enemy)//如果是敌人坦克
  272.     {
  273.         type=EnemyBullet;
  274.         direction=enemy.GetDirection();
  275.         Speed=enemy.GetFire();
  276.         Pos[0]=enemy.GetHotX();
  277.         Pos[1]=enemy.GetHotY();
  278.     }
  279.     if(direction==Up)//根据坦克的位置和方向确定子弹的初始坐标
  280.     {
  281.         Pos[0]--;
  282.         Pos[1]++;
  283.     }
  284.     else if(direction==Down)
  285.     {
  286.         Pos[0]+=3;
  287.         Pos[1]++;
  288.     }
  289.     else if(direction==Left)
  290.     {
  291.         Pos[0]++;
  292.         Pos[1]--;
  293.     }
  294.     else if(direction==Right)
  295.     {
  296.         Pos[0]++;
  297.         Pos[1]+=3;
  298.     }
  299.     //子弹的运行
  300.     while(1)
  301.     {
  302.         WaitForSingleObject(Mutex,INFINITE);//这个不再注释了。。。。。
  303.         if(flag==1&&hit!=1)//擦除原位置
  304.         {
  305.          map[oldx][oldy]=Empty;
  306.          SetPos((oldy+1)*2,oldx+1);
  307.          cout<<" ";
  308.         }
  309.         if(GameOver==1)
  310.             return 0;
  311.         if(hit==1||Pos[0]<0||Pos[0]>22||Pos[1]<0||Pos[1]>22)//如果击中
  312.         {
  313.             ReleaseMutex(Mutex);
  314.             Sleep(500);
  315.             if(type==PlayerBullet)
  316.                 player.FireEnable=1;
  317.             else if(type=EnemyBullet)
  318.                 enemy.FireEnable=1;
  319.             break;
  320.         }
  321.         switch(map[Pos[0]][Pos[1]])//子弹经过的MAP的标记
  322.         {
  323.             
  324.          case Empty://如果是空位置就绘制子弹
  325.              map[Pos[0]][Pos[1]]=type;
  326.                 SetPos((Pos[1]+1)*2,Pos[0]+1);
  327.                 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
  328.                 cout<<"■";
  329.              break;
  330.          case Player://如果是玩家位置
  331.              if(type!=PlayerBullet)
  332.              {
  333.              player.Life--;//生命减少
  334.                  if(player.Life<=0)
  335.                      GameOver=1;
  336.              }
  337.              Updata();
  338.              hit=1;
  339.              break;
  340.          case Enemy://如果是敌人位置
  341.              if(type!=PlayerBullet)
  342.                     hit=1;
  343.                 else
  344.                 {
  345.                     hit=1;
  346.                     Kill++;
  347.                     if(Kill%20==0&&player.Life<5)//击杀数++
  348.                         player.Life++;
  349.                     if(enemy.Type==Red)//如果击杀红坦克
  350.                     {
  351.                         KillRed++;
  352.                         if(KillRed%10==0&&player.GetFire()<5)
  353.                             player.IncreaseFire();
  354.                     }
  355.                     if(enemy.Type==Green)///如果击杀绿坦克
  356.                     {
  357.                         KillGreen++;
  358.                         if(KillGreen%10==0&&player.GetSpeed()<5)
  359.                             player.IncreaseSpeed();
  360.                     }
  361.                     enemy.Redraw();//擦除敌人
  362.                     enemy.Life=0;//敌人死亡
  363.                 }
  364.                 Updata();
  365.              break;
  366.         }
  367.         oldx=Pos[0];
  368.         oldy=Pos[1];
  369.         if(direction==Up)//子弹移动
  370.          Pos[0]--;
  371.      else if(direction==Down)
  372.          Pos[0]++;
  373.      else if(direction==Left)
  374.          Pos[1]--;
  375.      else if(direction==Right)
  376.          Pos[1]++;
  377.          ReleaseMutex(Mutex);
  378.          flag=1;
  379.          Sleep(60-10*Speed);
  380.     }
  381.     return 0;
  382. }


  383. /*************************敌人线程函数***************************/
  384. DWORD WINAPI TankRuning(LPVOID lpParameter)
  385. {
  386.     Sleep(400);
  387.     int Pos;
  388.     int Start[2];//敌人起始地址
  389.     int typ;
  390.     int fire;
  391.     int spe;
  392.     while(1)
  393.     {
  394.         if(GameOver==1)
  395.             return 0;
  396.      srand((int)time(0));//随机出敌人起始地址
  397.      Pos=rand()%4;
  398.      if(Pos==0)
  399.      {
  400.          Start[0]=2;
  401.          Start[0]=2;
  402.      }
  403.      else if(Pos==1)
  404.      {
  405.          Start[0]=2;
  406.          Start[1]=18;
  407.      }
  408.      else if(Pos==2)
  409.      {
  410.          Start[0]=18;
  411.          Start[1]=2;
  412.      }
  413.      else if(Pos==3)
  414.      {
  415.          Start[0]=18;
  416.          Start[1]=18;
  417.      }
  418.          if(player.Judge(Start[0],Start[1],Down)==0)
  419.              break;
  420.     }
  421.     WaitForSingleObject(Mutex,INFINITE);
  422.     srand((int)time(0));
  423.     typ=rand()%3+1;//随机出敌人的种类
  424.     if(typ==Blue)
  425.     {
  426.         spe=1+level;
  427.         fire=1+level;
  428.     }
  429.     else if(typ==Red)
  430.     {
  431.         spe=1+level;
  432.         fire=3+level;
  433.     }
  434.     else if(typ==Green)
  435.     {
  436.         spe=3+level;
  437.         fire=1+level;
  438.     }
  439.     enemy=Tank(Down,Start[0],Start[1],typ,spe,fire);//重新生成敌人坦克
  440.     enemy.ID=Enemy;
  441.     enemy.Life=1;
  442.     enemy.FireEnable=1;
  443.     ReleaseMutex(Mutex);
  444.     enemy.Running();
  445.     return 0;
  446. }


  447. void Init()//初始化函数
  448. {
  449.     Kill=0;
  450.     KillRed=0;
  451.     KillGreen=0;
  452.     player=Tank(Left,0,0,Normal,1,1);
  453.     enemy=Tank(Left,0,0,Red,1,1);
  454.     player.Life=2;
  455.     player.FireEnable=1;
  456.     enemy.Life=0;
  457.     enemy.FireEnable=1;
  458.     player.ID=Player;
  459.     enemy.ID=Enemy;
  460.     EnemyExist=0;
  461. }

  462. void Updata()//更新界面信息
  463. {
  464.     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
  465.     int i;
  466.     SetPos(53,0);
  467.     cout<<"生命值:";
  468.     SetPos(53,1);
  469.     for(i=0;i<5;i++)
  470.     {
  471.         if(i<player.Life)
  472.             cout<<"■";
  473.         else
  474.             cout<<" ";
  475.     }
  476.     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_GREEN);
  477.     SetPos(53,3);
  478.     cout<<"移动速度:";
  479.     SetPos(53,4);
  480.     for(i=0;i<5;i++)
  481.     {
  482.         if(i<player.GetSpeed())
  483.             cout<<"■";
  484.         else
  485.             cout<<" ";
  486.     }
  487.     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED);
  488.     SetPos(53,5);
  489.     cout<<"火力:";
  490.     SetPos(53,6);
  491.     for(i=0;i<5;i++)
  492.     {
  493.         if(i<player.GetFire())
  494.             cout<<"■";
  495.         else
  496.             cout<<" ";
  497.     }
  498.     SetPos(53,8);
  499.     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
  500.     cout<<"杀敌数:"<<Kill;
  501.     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED);
  502.     SetPos(53,9);
  503.     cout<<"杀死红坦克:"<<KillRed;
  504.     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_GREEN);
  505.     SetPos(53,10);
  506.     cout<<"杀死绿坦克:"<<KillGreen;

  507. }
  508. void DrawMap()//画界面
  509. {
  510.     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
  511.     system("cls");
  512.     int i;
  513.     for(i=0;i<25;i++)
  514.     {
  515.          SetPos(i*2,0);
  516.          cout<<"■";
  517.     }
  518.     for(i=1;i<25;i++)
  519.     {
  520.         SetPos(0,i);
  521.         cout<<"■";
  522.         SetPos(24*2,i);
  523.         cout<<"■";
  524.     }
  525.     for(i=0;i<25;i++)
  526.     {
  527.          SetPos(i*2,24);
  528.          cout<<"■";
  529.     }
  530.     
  531.     Updata();
  532.     
  533. }

  534. void Welcome()//欢迎界面
  535. {
  536.     int x;
  537.     system("cls");
  538.     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
  539.     SetPos(10,5);
  540.     cout<<"■■■■■■■■■■■■■■■■■■■■■■■■";
  541.     SetPos(10,6);
  542.     cout<<"■ 坦克大战控制台版 ■";
  543.     SetPos(10,7);
  544.     cout<<"■■■■■■■■■■■■■■■■■■■■■■■■";
  545.     SetPos(10,8);
  546.     cout<<"■ 方向键移动,空格键射击 ■";
  547.     SetPos(10,9);
  548.     cout<<"■ 敌人分为3种,蓝色为普通敌人 ■";
  549.     SetPos(10,10);
  550.     cout<<"■ 红色敌人高射速,绿色敌人高机动性 ■";
  551.     SetPos(10,11);
  552.     cout<<"■ 每杀死10个红坦克,玩家射速提高(最高五级) ■";
  553.     SetPos(10,12);
  554.     cout<<"■ 每杀死10个绿坦克,玩家移动性提高(最高五级)■";
  555.     SetPos(10,13);
  556.     cout<<"■ 每杀死20个坦克,玩家生命+1(最高五格) ■";
  557.     SetPos(10,14);
  558.     cout<<"■■■■■■■■■■■■■■■■■■■■■■■■";    
  559.     SetPos(10,15);
  560.     cout<<"■ 何某作(百度ID:HapHapYear) ■";
  561.     SetPos(10,16);
  562.     cout<<"■ 按1-3选择难度 ■";
  563.     SetPos(10,17);
  564.     cout<<"■■■■■■■■■■■■■■■■■■■■■■■■";
  565.     while(1)
  566.     {
  567.         x=getch();
  568.         if(x<='3'&&x>='1')
  569.             break;
  570.     }
  571.     level=x-'0'-1;
  572. }

  573. int main()
  574. {
  575.     Init();
  576.     HideCurSor();
  577.     Welcome();
  578.     DrawMap();
  579.     HANDLE temp;
  580.     int newD;
  581.     player.DrawTank();
  582.     while(GameOver==0)
  583.     {
  584.         if(GetAsyncKeyState(VK_UP))//按键上
  585.         {
  586.             WaitForSingleObject(Mutex,INFINITE);
  587.             newD=Up;
  588.             player.Redraw();
  589.             if(player.Judge(player.GetHotX()-1,player.GetHotY(),newD)==0)//移动玩家坦克,原理和敌人函数一样
  590.             {
  591.                 player.ChangePos(player.GetHotX()-1,player.GetHotY());
  592.                 player.ChangeDirection(newD);
  593.             }
  594.             else
  595.             {
  596.                 if(player.Judge(player.GetHotX(),player.GetHotY(),newD)==0)
  597.                     player.ChangeDirection(newD);
  598.             }
  599.             if(GameOver==0)
  600.              player.DrawTank();
  601.             ReleaseMutex(Mutex);
  602.             Sleep(200-player.GetSpeed()*20);//按键延迟,决定玩家坦克的速度
  603.         }
  604.         else if(GetAsyncKeyState(VK_DOWN))//按键下,同上
  605.         {
  606.             WaitForSingleObject(Mutex,INFINITE);
  607.             newD=Down;
  608.             player.Redraw();
  609.             if(player.Judge(player.GetHotX()+1,player.GetHotY(),newD)==0)
  610.             {
  611.                 player.ChangePos(player.GetHotX()+1,player.GetHotY());
  612.                 player.ChangeDirection(newD);
  613.             }
  614.             else
  615.             {
  616.                 if(player.Judge(player.GetHotX(),player.GetHotY(),newD)==0)
  617.                     player.ChangeDirection(newD);
  618.             }
  619.             if(GameOver==0)
  620.              player.DrawTank();
  621.             ReleaseMutex(Mutex);
  622.             Sleep(200-player.GetSpeed()*20);
  623.         }
  624.         else if(GetAsyncKeyState(VK_RIGHT))//按键右,同上
  625.         {
  626.             WaitForSingleObject(Mutex,INFINITE);
  627.             newD=Right;
  628.             player.Redraw();
  629.             if(player.Judge(player.GetHotX(),player.GetHotY()+1,newD)==0)
  630.             {
  631.                 player.ChangePos(player.GetHotX(),player.GetHotY()+1);
  632.                 player.ChangeDirection(newD);
  633.             }
  634.             else
  635.             {
  636.                 if(player.Judge(player.GetHotX(),player.GetHotY(),newD)==0)
  637.                     player.ChangeDirection(newD);
  638.             }
  639.             if(GameOver==0)
  640.              player.DrawTank();
  641.             ReleaseMutex(Mutex);
  642.             Sleep(200-player.GetSpeed()*20);
  643.         }
  644.         else if(GetAsyncKeyState(VK_LEFT))//按键左,同上
  645.         {
  646.             WaitForSingleObject(Mutex,INFINITE);
  647.             newD=Left;
  648.             player.Redraw();
  649.             if(player.Judge(player.GetHotX(),player.GetHotY()-1,newD)==0)
  650.             {
  651.                 player.ChangePos(player.GetHotX(),player.GetHotY()-1);
  652.                 player.ChangeDirection(newD);
  653.             }
  654.             else
  655.             {
  656.                 if(player.Judge(player.GetHotX(),player.GetHotY(),newD)==0)
  657.                     player.ChangeDirection(newD);
  658.             }
  659.             if(GameOver==0)
  660.              player.DrawTank();
  661.             ReleaseMutex(Mutex);
  662.             Sleep(110-player.GetSpeed()*10);
  663.         }
  664.         else if(GetAsyncKeyState(VK_SPACE))//按键空格,发射子弹
  665.         {
  666.             WaitForSingleObject(Mutex,INFINITE);
  667.             if(player.FireEnable==1)//如果可以发射
  668.             {
  669.              HANDLE bullet=CreateThread(NULL,0,Bulletfly,&(player.ID),0,NULL);//创建玩家子弹进程
  670.              CloseHandle(bullet);
  671.              player.FireEnable=0;
  672.             }
  673.             ReleaseMutex(Mutex);
  674.         }
  675.         if(EnemyExist==0&&GameOver==0)//如果敌人不存在生成新敌人
  676.         {
  677.             WaitForSingleObject(Mutex,INFINITE);
  678.             EnemyExist=1;
  679.             temp=CreateThread(NULL,0,TankRuning,NULL,0,NULL);//创建敌人线程
  680.             CloseHandle(temp);
  681.             ReleaseMutex(Mutex);
  682.         }
  683.     }
  684.     system("cls");
  685.     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_BLUE);
  686.     SetPos(20,10);
  687.     cout<<"游戏结束"<<endl;
  688.     SetPos(20,11);
  689.     cout<<"杀敌数:"<<Kill;
  690.     SetPos(20,12);
  691.     cout<<"杀死红坦克"<<KillRed;
  692.     SetPos(20,13);
  693.     cout<<"杀死绿坦克"<<KillGreen<<endl;
  694.     return 0;

阅读(1770) | 评论(0) | 转发(0) |
1

上一篇:数据库

下一篇:俄罗斯方块代码

给主人留下些什么吧!~~