{ Start[0]=18; Start[1]=18; } if(player.Judge(Start[0],Start[1],Down)==0) break; }
WaitForSingleObject(Mutex,INFINITE); srand((int)time(0));
typ=rand()%3+1;//随机出敌人的种类 if(typ==Blue) { spe=1+level; fire=1+level; }
else if(typ==Red) { spe=1+level; fire=3+level; }
else if(typ==Green) { spe=3+level; fire=1+level; }
enemy=Tank(Down,Start[0],Start[1],typ,spe,fire);//重新生成敌人坦克 enemy.ID=Enemy; enemy.Life=1; enemy.FireEnable=1; ReleaseMutex(Mutex); enemy.Running(); return 0; }
void Init()//初始化函数 { Kill=0; KillRed=0; KillGreen=0; player=Tank(Left,0,0,Normal,1,1); enemy=Tank(Left,0,0,Red,1,1); player.Life=2; player.FireEnable=1;
enemy.Life=0; enemy.FireEnable=1; player.ID=Player; enemy.ID=Enemy; EnemyExist=0; }
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 cout<<\移动速度:\SetPos(53,4); for(i=0;i<5;i++) { if(i 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 cout<<\ } SetPos(53,8); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); cout<<\杀敌数:\ SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED); SetPos(53,9); cout<<\杀死红坦克:\ SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_GREEN); SetPos(53,10); } cout<<\杀死绿坦克:\ void DrawMap()//画界面 { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); system(\ int i; for(i=0;i<25;i++) { SetPos(i*2,0); } cout<<\■\} for(i=1;i<25;i++) { SetPos(0,i); cout<<\■\SetPos(24*2,i); cout<<\■\} for(i=0;i<25;i++) { SetPos(i*2,24); cout<<\■\} Updata(); void Welcome()//欢迎界面 { int x; system(\ SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); SetPos(10,5); cout<<\■■■■■■■■■■■■■■■■■■■■■■■■\ SetPos(10,6); cout<<\■坦克大战控制台版■\SetPos(10,7); cout<<\■■■■■■■■■■■■■■■■■■■■■■■■\SetPos(10,8); cout<<\■方向键移动,空格键射击■\SetPos(10,9); cout<<\■敌人分为3种,蓝色为普通敌人■\SetPos(10,10); cout<<\■红色敌人高射速,绿色敌人高机动性■\SetPos(10,11); cout<<\■每杀死10个红坦克,玩家射速提高(最高五级)■\SetPos(10,12); cout<<\■每杀死10个绿坦克,玩家移动性提高(最高五级)■\SetPos(10,13); cout<<\■每杀死20个坦克,玩家生命+1(最高五格)■\SetPos(10,14); cout<<\■■■■■■■■■■■■■■■■■■■■■■■■\SetPos(10,15); cout<<\■何某作(百度ID:HapHapYear)■\ SetPos(10,16); cout<<\■按1-3选择难度■\ SetPos(10,17); cout<<\■■■■■■■■■■■■■■■■■■■■■■■■\ while(1) { x=getch(); if(x<='3'&&x>='1') break; } level=x-'0'-1; } 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); }