예제 #1
0
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;
}
예제 #2
0
/*************************敌人线程函数***************************/
DWORD WINAPI TankRuning(LPVOID lpParameter)
{
	Sleep(400);
	int Pos;
	int Start[2];//敌人起始地址
	int typ;
	int fire;
	int spe;
	while(1)
	{
		if(GameOver==1)
			return 0;
	    srand((int)time(0));//随机出敌人起始地址
	    Pos=rand()%4;
	    if(Pos==0)
	    {
		     Start[0]=2;
		     Start[0]=2;
	    }
	    else if(Pos==1)
	    {
	     	Start[0]=2;
	    	Start[1]=18;
	     }
	     else if(Pos==2)
	    {
		    Start[0]=18;
		    Start[1]=2;
	     }
	     else if(Pos==3)
	    {
		   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;
}