⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cman.cpp

📁 以前写的一个bughunt小游戏
💻 CPP
字号:
#include "iostream.h"

#include "time.h"

#include "bitmap.h"

#include "CBug.h"

#include "CMan.h"

#include "uniform.h"

CMan::CMan()
{

nAoutoAttackState=0;

hp=0;

nPic=0;

Drection=0;

Speed=0;

x=0;

y=0;

AttackDrection=0;

ArrowSpeed=0;

xArrow=-2;

yArrow=-2;

tProtected=0;

bProtected=FALSE;

}

void CMan::Create(SimpleWindow &W,float &HP,float &speed,float &arrowspeed,
				  
				  const float &X,const float &Y)

{

bmp.SetWindow(W);

bmpAttack.SetWindow(W);

nPic=0;

nAoutoAttackState=0;

hp=HP;

Drection=Uniform(0,3);

Speed=speed;

x=X;

y=Y;

AttackDrection=Uniform(0,3);

ArrowSpeed=arrowspeed;

xArrow=-2;

yArrow=-2;

tProtected=0;

bProtected=TRUE;

bAttack=FALSE;

}

void CMan::Draw()

{
	
nPic++;

if(nPic>3)

nPic=0;

bmp.Erase();

switch(Drection)
{
	case 0: 

		switch(nPic)
		{
		case 0:

bmp.Load("a01.bmp");

         break;

		case 1:

bmp.Load("a02.bmp");

         break;

		case 2:

bmp.Load("a03.bmp");

         break;

		case 3:

bmp.Load("a04.bmp");

         break;

default:

		break;

}

    break;

	case 1: 

		switch(nPic)
		{
		case 0:

bmp.Load("b01.bmp");

         break;

		case 1:

bmp.Load("b02.bmp");

         break;

		case 2:

bmp.Load("b03.bmp");

         break;

		case 3:

bmp.Load("b04.bmp");

         break;

default:

		break;

}
    break;

	case 2: 

		switch(nPic)
		{
		case 0:

bmp.Load("c01.bmp");

         break;

		case 1:

bmp.Load("c02.bmp");

         break;

		case 2:

bmp.Load("c03.bmp");

         break;

		case 3:

bmp.Load("c04.bmp");

         break;

default:

		break;

}
    break;

	case 3: 

		switch(nPic)
		{
		case 0:

bmp.Load("d01.bmp");

         break;

		case 1:

bmp.Load("d02.bmp");

         break;

		case 2:

bmp.Load("d03.bmp");

         break;

		case 3:

bmp.Load("d04.bmp");

         break;

default:

		break;

}
    break;

	default:

		break;

}
Position pos;

pos.SetXDistance(x);

pos.SetYDistance(y);

bmp.SetPosition(pos);

bmp.Draw();

}

void CMan::Move()
{

switch(Drection)
{

	case 0: 

   if(x>0)

    x-=Speed;

    break;
	
	case 1: 

	if(y>0)

    y-=Speed;

    break;

	case 2: 
 
  if(x<18-Speed)

   x+=Speed;

    break;

	case 3: 

  if(y<15-Speed)

     y+=Speed;

    break;

	default:

		break;

}

}
void CMan::PrepareAttack()
{

AttackDrection=Drection;

switch(Drection)
{

case 0:

xArrow=x-bmpAttack.GetWidth();

yArrow=y+bmp.GetHeight()/2-bmpAttack.GetHeight()/2;

break;

case 1:

xArrow=x+bmp.GetWidth()/2-bmpAttack.GetWidth()/2;

yArrow=y-bmpAttack.GetHeight();

break;

case 2:

xArrow=x+bmp.GetWidth();

yArrow=y+bmp.GetHeight()/2-bmpAttack.GetHeight()/2;

break;

case 3:

xArrow=x+bmp.GetWidth()/2-bmpAttack.GetWidth()/2;

yArrow=y+bmp.GetHeight();

break;

default:

	break;

}

bAttack=TRUE;

}
void CMan::Attack()
{

bmpAttack.Erase();


switch(AttackDrection)
{
   case 0: 

   if(xArrow>0)
   {

    bmpAttack.Load("z.bmp");
	
    xArrow-=ArrowSpeed;

   }

   else 
   {
   
bAttack=FALSE;

xArrow=-2;

yArrow=-2;
   }

    break;
	
	case 1: 

if(yArrow>0)

	{

    yArrow-=ArrowSpeed;

    bmpAttack.Load("s.bmp");

	}

   else 
   {
   
bAttack=FALSE;

xArrow=-2;

yArrow=-2;
   }

    break;

	case 2: 
 
  if(xArrow<18)
  {

xArrow+=ArrowSpeed;

bmpAttack.Load("y.bmp");

  }
   else 
   {
   
bAttack=FALSE;

xArrow=-2;

yArrow=-2;
   }


    break;

	case 3: 

  if(yArrow<18)
  {

yArrow+=ArrowSpeed;

bmpAttack.Load("x.bmp");

  }
   else 
   {
   
bAttack=FALSE;

xArrow=-2;

yArrow=-2;
   }

   
    break;

	default:

		break;
}


Position pos;

pos.SetXDistance(xArrow);

pos.SetYDistance(yArrow);

bmpAttack.SetPosition(pos);

bmpAttack.Draw();

if(bAttack==FALSE
   
&&!bmpAttack.Erase())

bmpAttack.Erase();

}

void CMan::Action()

{

Draw();

Move();

if(bAttack)

Attack();

}
void CMan::AoutoMove(CBug &bug)
{

if(bug.x+bug.bmp.GetWidth()/2-bmp.GetWidth()/2>x)

{	
	if(bug.y+bug.bmp.GetHeight()/2-bmp.GetHeight()/2>y)
	
	{
		   Drection=3;

		if(bug.x+bug.bmp.GetWidth()/2-bmp.GetWidth()/2-x>Speed)

			x+=Speed;

			else

            x+=(bug.x+bug.bmp.GetWidth()/2-bmp.GetWidth()/2-x);

		if(bug.y+bug.bmp.GetHeight()/2-bmp.GetHeight()/2-y>Speed)

			y+=Speed;

			else

        	y+=(bug.y+bug.bmp.GetHeight()/2-bmp.GetHeight()/2-y);

	}

else if(bug.y+bug.bmp.GetHeight()/2-bmp.GetHeight()/2<y)

	{
		    Drection=2;

		if(bug.x+bug.bmp.GetWidth()/2-bmp.GetWidth()/2-x>Speed)

			x+=Speed;

			else

            x+=(bug.x+bug.bmp.GetWidth()/2-bmp.GetWidth()/2-x);

		if(y-(bug.y+bug.bmp.GetHeight()/2-bmp.GetHeight()/2)>Speed)

			y-=Speed;

			else
			
        	y-=(y-(bug.y+bug.bmp.GetHeight()/2-bmp.GetHeight()/2));


	}

}
else if(bug.x+bug.bmp.GetWidth()/2-bmp.GetWidth()/2<x)
{
    if(bug.y+bug.bmp.GetHeight()/2-bmp.GetHeight()/2>y)
		{
		    Drection=0;

		if(x-(bug.x+bug.bmp.GetWidth()/2-bmp.GetWidth()/2)>Speed)

			x-=Speed;

			else

            x-=(x-(bug.x+bug.bmp.GetWidth()/2-bmp.GetWidth()/2));

	   if(bug.y+bug.bmp.GetHeight()/2-bmp.GetHeight()/2-y>Speed)

			y+=Speed;

			else

        	y+=(bug.y+bug.bmp.GetHeight()/2-bmp.GetHeight()/2-y);

		}

	else if(bug.y+bug.bmp.GetHeight()/2-bmp.GetHeight()/2<y)

		{  
			Drection=1;

		if(x-(bug.x+bug.bmp.GetWidth()/2-bmp.GetWidth()/2)>Speed)

			x-=Speed;

			else

            x-=(x-(bug.x+bug.bmp.GetWidth()/2-bmp.GetWidth()/2));
			
		if(y-(bug.y+bug.bmp.GetHeight()/2-bmp.GetHeight()/2)>Speed)

			y-=Speed;

			else

        	y-=(y-(bug.y+bug.bmp.GetHeight()/2-bmp.GetHeight()/2));

		}

}
		
}

void CMan::AoutoAttack(SimpleWindow &W,CBug &bug , int &i,int &j)

{

switch(nAoutoAttackState)
{

case 0:

//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////

if( bug.alive&&bug.bmp.IsInside( Position( bug.x+bug.bmp.GetWidth()/2,
   
 y+bmp.GetHeight()/2 -bug.bmp.GetHeight()/2) ) )

if(x>bug.x)

{

AttackDrection=0;

nAoutoAttackState=1;

string str1="",str2="";

str1+="HelpMan Attack Bug[";

str1+='0'+i;

str1+="]";

str2+="HP: ";

str2+='0'+bug.hp;

j++;

if(j>6)

{
	W.Erase(Position(18.8f,12),6,6);

	j=0;
}


W.RenderText(Position(19,12+0.5*j),Position(24,12.5+0.5*j),str1,Magenta);

j++;

if(j>6)

{
	W.Erase(Position(18.8f,12),6,6);

	j=0;
}

W.RenderText(Position(19,12+0.5*j),Position(24,12.5+0.5*j),str2,Magenta);

bAttack=TRUE;

return;

}
  else
  {

AttackDrection=2;

nAoutoAttackState=1;

string str1="",str2="";

str1+="HelpMan Attack Bug[";

str1+='0'+i;

str1+="]";

str2+="HP: ";

str2+='0'+bug.hp;

j++;

if(j>6)

{
	W.Erase(Position(18.8f,12),6,6);

	j=0;
}

W.RenderText(Position(19,12+0.5*j),Position(24,12.5+0.5*j),str1,Magenta);

j++;

if(j>6)

{
	W.Erase(Position(18.8f,12),6,6);

	j=0;
}

W.RenderText(Position(19,12+0.5*j),Position(24,12.5+0.5*j),str2,Magenta);

bAttack=TRUE;

return;

  }

//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////


if(bug.alive && bug.bmp.IsInside(Position(x+bmp.GetWidth()/2-bug.bmp.GetWidth()/2,
				 
bug.y+bug.bmp.GetHeight()/2) ) )

   if(y>bug.y)
   {

AttackDrection=1;

nAoutoAttackState=1;

string str1="",str2="";

str1+="HelpMan Attack Bug[";

str1+='0'+i;

str1+="]";

str2+="HP: ";

str2+='0'+bug.hp;

j++;

if(j>6)

{
	W.Erase(Position(18.8f,12),6,6);

	j=0;
}

W.RenderText(Position(19,12+0.5*j),Position(24,12.5+0.5*j),str1,Magenta);

j++;

if(j>6)

{
	W.Erase(Position(18.8f,12),6,6);

	j=0;
}


W.RenderText(Position(19,12+0.5*j),Position(24,12.5+0.5*j),str2,Magenta);

bAttack=TRUE;

return;
	
   }

	else
	{
	
AttackDrection=3;

nAoutoAttackState=1;

string str1="",str2="";

str1+="HelpMan Attack Bug[";

str1+='0'+i;

str1+="]";

str2+="HP: ";

str2+='0'+bug.hp;

j++;

if(j>6)

{
	W.Erase(Position(18.8f,12),6,6);

	j=0;
}


W.RenderText(Position(19,12+0.5*j),Position(24,12.5+0.5*j),str1,Magenta);

j++;

if(j>6)

{
	W.Erase(Position(18.8f,12),6,6);

	j=0;
}


W.RenderText(Position(19,12+0.5*j),Position(24,12.5+0.5*j),str2,Magenta);

bAttack=TRUE;

return;

	}

break;

case 1:

	switch(Drection)
{

case 0:

xArrow=x-bmpAttack.GetWidth();

yArrow=y+bmp.GetHeight()/2-bmpAttack.GetHeight()/2;

break;

case 1:

xArrow=x+bmp.GetWidth()/2-bmpAttack.GetWidth()/2;

yArrow=y-bmpAttack.GetHeight();

break;

case 2:

xArrow=x+bmp.GetWidth();

yArrow=y+bmp.GetHeight()/2-bmpAttack.GetHeight()/2;

break;

case 3:

xArrow=x+bmp.GetWidth()/2-bmpAttack.GetWidth()/2;

yArrow=y+bmp.GetHeight();

break;

default:

	break;

}

nAoutoAttackState=2;

break;

case 2:

if(bAttack)

Attack();

if(bAttack==FALSE)

{

nAoutoAttackState=0;

string str="";

str+="Bug[";

str+='0'+i;

str+="] Just Moves away";

j++;

if(j>6)

{
	W.Erase(Position(18.8f,12),6,6);

	j=0;
}


W.RenderText(Position(19,12+0.5*j),Position(24,12.5+0.5*j),str,Magenta);

	
}

if( bug.bmp.IsInside( Position (xArrow+bmpAttack.GetWidth()/2,
		
		yArrow+bmpAttack.GetHeight()/2) ) )

	{

    bAttack=FALSE;

	bmpAttack.Erase();

    xArrow=-2;

    yArrow=-2;

    bug.hp--;

string str1="",str2="";

str1+="Bug[";

str1+='0'+i;

str1+="] Hited by HelpMan";

str2+="HP leave: ";

str2+='0'+bug.hp;

j++;

if(j>6)

{
	W.Erase(Position(18.8f,12),6,6);

	j=0;
}

W.RenderText(Position(19,12+0.5*j),Position(24,12.5+0.5*j),str1,Magenta);

j++;

if(j>6)

{
	W.Erase(Position(18.8f,12),6,6);

	j=0;
}


W.RenderText(Position(19,12+0.5*j),Position(24,12.5+0.5*j),str2,Magenta);

if(bug.hp<=0)
{

bug.bmp.Erase();

bug.alive=FALSE;

string str="Poor Bug , He Dead ";

j++;

if(j>6)

{
	W.Erase(Position(18.8f,12),6,6);

	j=0;
}


W.RenderText(Position(19,12+0.5*j),Position(24,12.5+0.5*j),str,Magenta);

}

    nAoutoAttackState=0;


}

	
break;

default:

	break;

}

}
void CMan::AoutoAction(SimpleWindow &W,CBug &bug,int &i,int &j)
{

Draw();

AoutoAttack(W,bug,i,j);

AoutoMove(bug);

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -