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

📄 enemy.cpp

📁 Win32项目,类似坦克大战的游戏"忘记喧嚣". 游戏的各系统完整,闪屏,道具.碰撞等等....推荐
💻 CPP
字号:
#include ".\enemy.h"

Enemy::Enemy(HDC hdc, HDC mdc, HDC bufdc):Tack(hdc, mdc, bufdc)
{
	e_hdc = hdc;
	e_mdc = mdc;
	e_bufdc = bufdc;

	t_live   = false;
	t_show   = false;
	t_bullet[0]->b_exit = false;
	t_bullet[1]->b_exit = false;
	for (int i=0; i<8; i++)
	{
		char a[30];
		wsprintf(a, "../res/graphics/en%d.bmp", i);
		e_tackMap[i] = (HBITMAP)::LoadImage(NULL, a, IMAGE_BITMAP, 160, 40, LR_LOADFROMFILE);
	}
	srand((int)time(0));
}

Enemy::~Enemy(void)
{
	for (int i=0; i<8; i++)
	{
		::DeleteObject(e_tackMap[i]);
	}

	SAFE_DELETEDC(e_bufdc);
	SAFE_DELETEDC(e_mdc);
	SAFE_DELETEDC(e_hdc);
}

void Enemy::CreateTack(int startX, int startY)
{	
	t_startX = startX;
	t_startY = startY;
	t_lockX  = startX;
	t_lockY  = startY;
	t_nextX  = startX;                // 探索坐标
	t_nextY  = startY;
	t_veloc  = 5;                     // 速度
	t_grade  = 1;                     // 等级 吃一个道具五星改变
	t_tackMode   = rand()%8;          // 类型
	t_life   = 1;
	t_HP     = 10;                    // 血量值
	t_index  = 1;
	t_live   = true;
	t_show   = true;
	t_stop   = false;
	t_bullet[0]->b_exit = true;
	t_bullet[1]->b_exit = true;
	t_bullet[0]->b_point.x = 0;
	t_bullet[0]->b_point.y = 0;
	t_bullet[1]->b_point.x = 0;
	t_bullet[1]->b_point.y = 0;
}

bool Enemy::MoveAble(Tack *otherTack)
{
	t_nextX = t_lockX;
	t_nextY = t_lockY;
	int probability = rand()%10;

	if (probability>=0 && probability <= 3)  // 左右
	{	
		if (t_lockY <= 470)
		{
			t_index = rand()%4;
		}
		else
		{
			t_index = (t_lockX > 260 ? 3 : 0);
		}
	}
	
	if (probability >= 4 && probability <= 5)
	{
		if (t_nextX < 260)
		{
			t_index = 0;
		}
		if (t_nextX >= 260)
		{
			t_index = 3;
		}
		if (t_nextY <= 470)
		{
			t_index = 1;
		}
		if (t_nextY > 470)
		{
			t_index = 0;
		}	
	}

	if (probability >=6 && probability <= 9)
	{
		t_index = t_index;
	}

	switch (t_index)
	{
	case 0:
		t_nextX += t_veloc;
		break;
	case 1:
		t_nextY += t_veloc;
		break;
	case 2:
		t_nextY -= t_veloc;
		break;
	case 3:
		t_nextX -= t_veloc;
		break;
	default:
		break;
	}

	if (TackHitMap(t_nextX, t_nextY) || (otherTack !=NULL && otherTack->t_live && TackHitTack(otherTack, t_nextX, t_nextY)))
	{
		return false;
	}
	else
	{
		return true;
	}
}

void Enemy::EnemyShoot(void)        // 自动发射子弹频率控制
{
	if (t_live && t_show)
	{
		int n = rand()%20;
		if (n == 1)
		{
			CreatBullet(t_lockX, t_lockY, t_index);
		}
		Shoot();
	}
}

void Enemy::OperateTack(void)
{
	if (t_live && t_show)
	{
		if (!t_stop)
		{
			if (t_ableMove)
			{
				MoveTack(); 
			}
			EnemyShoot();
		}		
		::SelectObject(e_mdc, e_tackMap[t_tackMode]);
		::TransparentBlt(e_bufdc,t_lockX, t_lockY, 40, 40, e_mdc, 40*t_index, 0, 40, 40, RGB(0,255,0));
	}
}

void Enemy::ShowTack(void) 
{
	if (t_live && t_show)
	{
		::SelectObject(e_mdc, e_tackMap[t_tackMode]);
		::TransparentBlt(e_bufdc,t_lockX, t_lockY, 40, 40, e_mdc, 40*t_index, 0, 40, 40, RGB(0,255,0));
	}
}

⌨️ 快捷键说明

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