1、#include#include#include#include#includeusing namespace std;HANDLE Mutex=CreateMutex(NULL,FALSE,NULL);/互斥对象int GameOver=0;int level=0;int map2323;/坦克种类,Normal 为玩家坦克#define Normal 0#define Red 1#define Blue 2#define Green 3/方向的宏定义#define Up 0#define Down 1#define Left 2#define Right 3/地图标记的宏定义#define
2、 Empty 0#define Player 1#define PlayerBullet 2#define EnemyBullet 3#define Enemy 4int Kill;int KillRed;int KillGreen;int EnemyExist;void SetPos(int i,int j)/设定光标位置COORD pos=i,j;HANDLE Out=GetStdHandle(STD_OUTPUT_HANDLE);SetConsoleCursorPosition(Out, pos);void HideCurSor(void)/隐藏光标CONSOLE_CURSOR_INFO
3、 info=1,0;HANDLE Out=GetStdHandle(STD_OUTPUT_HANDLE);SetConsoleCursorInfo(Out,int sharp412=0,1,1,0,1,1,1,2,2,0,2,2,0,0,0,2,1,0,1,1,1,2,2,1,0,1,0,2,1,0,1,1,2,1,2,2,0,0,0,1,1,1,1,2,2,0,2,1,;/此数组用来保存坦克各个方向的形状信息DWORD WINAPI Bulletfly(LPVOID lpParameter);/子弹函数申明void Updata();/更新界面信息函数申明class Tank/坦克类priv
4、ate:int Direction;/方向int hotpoint2;/活动点int Speed;/速度int FirePower;/火力public:Tank(int dir,int hot1,int hot2,int typ,int spe,int firepow)/构造函数Direction=dir;hotpoint0=hot1;hotpoint1=hot2;Type=typ;Speed=spe;FirePower=firepow;int Type;/坦克的种类(详见宏定义)int ID;/坦克在 MAP 中的标记(详见宏定义)int FireEnable;/是否可以开火int Life
5、;/生命值void Running();/运行函数int Judge(int x,int y,int ID);/判断是否可以绘制坦克void DrawTank();/重绘坦克void Redraw();/擦除坦克int GetSpeed()/获取速度return Speed;int GetFire()/获取火力return FirePower;int GetDirection()/获取方向return Direction;int GetHotX()/获取活动点坐标return hotpoint0;int GetHotY()return hotpoint1;void IncreaseFire()
6、/火力+FirePower+;void IncreaseSpeed()/速度+Speed+;void ChangeDirection(int newD)/改变方向Direction=newD;void ChangePos(int x,int y)/改变活动点hotpoint0=x;hotpoint1=y;Tank player(Right,0,0,Normal,1,1);/玩家Tank enemy(Left,20,0,Red,1,1);/敌人void Tank:DrawTank()/绘制坦克int i;int nx,ny;if(Type=Red)SetConsoleTextAttribute(
7、GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED);else if(Type=Blue)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_BLUE);else if(Type=Green)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_GREEN);el
8、se if(Type=Normal)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);for(i=0;i=23|ny=23|mapnxny!=Empty)/不能绘制,返回 1return 1;return 0;void Tank:Running()/坦克运行函数int newD;/坦克的运行while(1)if(Life=0)EnemyExist=0;/敌人不存在return;if(G
9、ameOver=1)return;if(FireEnable=1/线程拥有互斥对象FireEnable=0;/设为不可开火HANDLE bullet=CreateThread(NULL,0,Bulletfly,/创建子弹线程CloseHandle(bullet);ReleaseMutex(Mutex);/释放互斥对象Sleep(100);WaitForSingleObject(Mutex,INFINITE);/线程拥有互斥对象srand(int)time(0);newD=rand()%4;if(newD=Up)/随机出新的方向并重新绘制坦克Redraw();if(Judge(hotpoint0
10、-1,hotpoint1,newD)=0)hotpoint0-;Direction=newD;elseif(Judge(hotpoint0,hotpoint1,newD)=0)Direction=newD;else if(newD=Down)Redraw();if(Judge(hotpoint0+1,hotpoint1,newD)=0)hotpoint0+;Direction=newD;elseif(Judge(hotpoint0,hotpoint1,newD)=0)Direction=newD;else if(newD=Left)Redraw();if(Judge(hotpoint0,hot
11、point1-1,newD)=0)hotpoint1-;Direction=newD;elseif(Judge(hotpoint0,hotpoint1,newD)=0)Direction=newD;else if(newD=Right)Redraw();if(Judge(hotpoint0,hotpoint1+1,newD)=0)hotpoint1+;Direction=newD;elseif(Judge(hotpoint0,hotpoint1,newD)=0)Direction=newD;if(GameOver=0ReleaseMutex(Mutex);/释放互斥对象Sleep(500-80
12、*Speed);/*子弹线程函数*/DWORD WINAPI Bulletfly(LPVOID lpParameter)int *ID=(int *)lpParameter;/ID 用来获取发射子弹坦克的 IDint Pos2;/子弹活动点int direction;int Speed;int type;int hit=0;/击中标记int oldx,oldy;/旧活动点int flag=0;/子弹是否有移动的标记if(*ID=Player)/如果是玩家坦克type=PlayerBullet;direction=player.GetDirection();Speed=player.GetFir
13、e();Pos0=player.GetHotX();Pos1=player.GetHotY();else if(*ID=Enemy)/如果是敌人坦克type=EnemyBullet;direction=enemy.GetDirection();Speed=enemy.GetFire();Pos0=enemy.GetHotX();Pos1=enemy.GetHotY();if(direction=Up)/根据坦克的位置和方向确定子弹的初始坐标Pos0-;Pos1+;else if(direction=Down)Pos0+=3;Pos1+;else if(direction=Left)Pos0+;
14、Pos1-;else if(direction=Right)Pos0+;Pos1+=3;/子弹的运行while(1)WaitForSingleObject(Mutex,INFINITE);/这个不再注释了。 。 。 。 。if(flag=1SetPos(oldy+1)*2,oldx+1);cout22|Pos122)/如果击中ReleaseMutex(Mutex);Sleep(500);if(type=PlayerBullet)player.FireEnable=1;else if(type=EnemyBullet)enemy.FireEnable=1;break;switch(mapPos0
15、Pos1)/子弹经过的 MAP 的标记case Empty:/如果是空位置就绘制子弹mapPos0Pos1=type;SetPos(Pos1+1)*2,Pos0+1);SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);cout=1)break;level=x-0-1;int main()Init();HideCurSor();Welcome();DrawMap();HANDLE tem
16、p;int newD;player.DrawTank();while(GameOver=0)if(GetAsyncKeyState(VK_UP)/按键上WaitForSingleObject(Mutex,INFINITE);newD=Up;player.Redraw();if(player.Judge(player.GetHotX()-1,player.GetHotY(),newD)=0)/移动玩家坦克,原理和敌人函数一样player.ChangePos(player.GetHotX()-1,player.GetHotY();player.ChangeDirection(newD);elsei
17、f(player.Judge(player.GetHotX(),player.GetHotY(),newD)=0)player.ChangeDirection(newD);if(GameOver=0)player.DrawTank();ReleaseMutex(Mutex);Sleep(200-player.GetSpeed()*20);/按键延迟,决定玩家坦克的速度else if(GetAsyncKeyState(VK_DOWN)/按键下,同上WaitForSingleObject(Mutex,INFINITE);newD=Down;player.Redraw();if(player.Jud
18、ge(player.GetHotX()+1,player.GetHotY(),newD)=0)player.ChangePos(player.GetHotX()+1,player.GetHotY();player.ChangeDirection(newD);elseif(player.Judge(player.GetHotX(),player.GetHotY(),newD)=0)player.ChangeDirection(newD);if(GameOver=0)player.DrawTank();ReleaseMutex(Mutex);Sleep(200-player.GetSpeed()*
19、20);else if(GetAsyncKeyState(VK_RIGHT)/按键右,同上WaitForSingleObject(Mutex,INFINITE);newD=Right;player.Redraw();if(player.Judge(player.GetHotX(),player.GetHotY()+1,newD)=0)player.ChangePos(player.GetHotX(),player.GetHotY()+1);player.ChangeDirection(newD);elseif(player.Judge(player.GetHotX(),player.GetHo
20、tY(),newD)=0)player.ChangeDirection(newD);if(GameOver=0)player.DrawTank();ReleaseMutex(Mutex);Sleep(200-player.GetSpeed()*20);else if(GetAsyncKeyState(VK_LEFT)/按键左,同上WaitForSingleObject(Mutex,INFINITE);newD=Left;player.Redraw();if(player.Judge(player.GetHotX(),player.GetHotY()-1,newD)=0)player.Chang
21、ePos(player.GetHotX(),player.GetHotY()-1);player.ChangeDirection(newD);elseif(player.Judge(player.GetHotX(),player.GetHotY(),newD)=0)player.ChangeDirection(newD);if(GameOver=0)player.DrawTank();ReleaseMutex(Mutex);Sleep(110-player.GetSpeed()*10);else if(GetAsyncKeyState(VK_SPACE)/按键空格,发射子弹WaitForSin
22、gleObject(Mutex,INFINITE);if(player.FireEnable=1)/如果可以发射HANDLE bullet=CreateThread(NULL,0,Bulletfly,/创建玩家子弹进程CloseHandle(bullet);player.FireEnable=0;ReleaseMutex(Mutex);if(EnemyExist=0EnemyExist=1;temp=CreateThread(NULL,0,TankRuning,NULL,0,NULL);/创建敌人线程CloseHandle(temp);ReleaseMutex(Mutex);system(“cls“);SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_BLUE);SetPos(20,10);cout“游戏结束“endl;SetPos(20,11);cout“杀敌数:“Kill;SetPos(20,12);cout“杀死红坦克“KillRed;SetPos(20,13);cout“杀死绿坦克“KillGreenendl;return 0;