void Updata()//更新界面信息 { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); int i; SetPos(53,0); cout<<"生命值:"; SetPos(53,1); for(i=0;i<5;i++) { if(i<player.Life) cout<<"■"; else cout<<" "; } SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_GREEN); SetPos(53,3); cout<<"移动速度:"; SetPos(53,4); for(i=0;i<5;i++) { if(i<player.GetSpeed()) cout<<"■"; else cout<<" "; } SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED); SetPos(53,5); cout<<"火力:"; SetPos(53,6); for(i=0;i<5;i++) { if(i<player.GetFire()) cout<<"■"; else cout<<" "; } SetPos(53,8); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); cout<<"杀敌数:"<<Kill; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED); SetPos(53,9); cout<<"杀死红坦克:"<<KillRed; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_GREEN); SetPos(53,10); cout<<"杀死绿坦克:"<<KillGreen; }
int main() { Init(); HideCurSor(); Welcome(); DrawMap(); HANDLE temp; 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); } else { if(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.Judge(player.GetHotX()+1,player.GetHotY(),newD)==0) { player.ChangePos(player.GetHotX()+1,player.GetHotY()); player.ChangeDirection(newD); } else { if(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_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); } else { if(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_LEFT))//按键左,同上 { WaitForSingleObject(Mutex,INFINITE); newD=Left; player.Redraw(); if(player.Judge(player.GetHotX(),player.GetHotY()-1,newD)==0) { player.ChangePos(player.GetHotX(),player.GetHotY()-1); player.ChangeDirection(newD); } else { if(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))//按键空格,发射子弹 { WaitForSingleObject(Mutex,INFINITE); if(player.FireEnable==1)//如果可以发射 { HANDLE bullet=CreateThread(NULL,0,Bulletfly,&(player.ID),0,NULL);//创建玩家子弹进程 CloseHandle(bullet); player.FireEnable=0; } ReleaseMutex(Mutex); } if(EnemyExist==0&&GameOver==0)//如果敌人不存在生成新敌人 { WaitForSingleObject(Mutex,INFINITE); EnemyExist=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<<"杀死绿坦克"<<KillGreen<<endl; return 0; }
/*********************子弹线程函数*******************/ DWORD WINAPI Bulletfly(LPVOID lpParameter) { int *ID=(int *)lpParameter;//ID用来获取发射子弹坦克的ID int Pos[2];//子弹活动点 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.GetFire(); Pos[0]=player.GetHotX(); Pos[1]=player.GetHotY(); } else if(*ID==Enemy)//如果是敌人坦克 { type=EnemyBullet; direction=enemy.GetDirection(); Speed=enemy.GetFire(); Pos[0]=enemy.GetHotX(); Pos[1]=enemy.GetHotY(); } if(direction==Up)//根据坦克的位置和方向确定子弹的初始坐标 { Pos[0]--; Pos[1]++; } else if(direction==Down) { Pos[0]+=3; Pos[1]++; } else if(direction==Left) { Pos[0]++; Pos[1]--; } else if(direction==Right) { Pos[0]++; Pos[1]+=3; } //子弹的运行 while(1) { WaitForSingleObject(Mutex,INFINITE);//这个不再注释了。。。。。 if(flag==1&&hit!=1)//擦除原位置 { map[oldx][oldy]=Empty; SetPos((oldy+1)*2,oldx+1); cout<<" "; } if(GameOver==1) return 0; if(hit==1||Pos[0]<0||Pos[0]>22||Pos[1]<0||Pos[1]>22)//如果击中 { ReleaseMutex(Mutex); Sleep(500); if(type==PlayerBullet) player.FireEnable=1; else if(type=EnemyBullet) enemy.FireEnable=1; break; } switch(map[Pos[0]][Pos[1]])//子弹经过的MAP的标记 { case Empty://如果是空位置就绘制子弹 map[Pos[0]][Pos[1]]=type; SetPos((Pos[1]+1)*2,Pos[0]+1); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); cout<<"■"; break; case Player://如果是玩家位置 if(type!=PlayerBullet) { player.Life--;//生命减少 if(player.Life<=0) GameOver=1; } Updata(); hit=1; break; case Enemy://如果是敌人位置 if(type!=PlayerBullet) hit=1; else { hit=1; Kill++; if(Kill%20==0&&player.Life<5)//击杀数++ player.Life++; if(enemy.Type==Red)//如果击杀红坦克 { KillRed++; if(KillRed%10==0&&player.GetFire()<5) player.IncreaseFire(); } if(enemy.Type==Green)///如果击杀绿坦克 { KillGreen++; if(KillGreen%10==0&&player.GetSpeed()<5) player.IncreaseSpeed(); } enemy.Redraw();//擦除敌人 enemy.Life=0;//敌人死亡 } Updata(); break; } oldx=Pos[0]; oldy=Pos[1]; if(direction==Up)//子弹移动 Pos[0]--; else if(direction==Down) Pos[0]++; else if(direction==Left) Pos[1]--; else if(direction==Right) Pos[1]++; ReleaseMutex(Mutex); flag=1; Sleep(60-10*Speed); } return 0; }