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

📄 player.cpp

📁 射击小游戏
💻 CPP
字号:
#include "stdafx.h"
#include "Player.h"
#include "ShootView.h"
#include "Shoot.h"

// 构造函数
Player::Player(int x_value, int y_value)
:FighterPlane(x_value, y_value)
{
	//初始化贴图坐标
	m_isx = 0;
	m_isy = 0;
	m_w = 29;
	m_h = 33;
	//碰撞检测用矩形尺寸
	cx = 6;
	cy = 11;
	cw = 17;
	ch = 19;
	//初始化生命值
	m_iLife  =  20;
	//初始化速度
	m_speed = 3;
	//杀敌数初始化
	m_iKillEnermy  =  0;
	//得分
	m_iScore  =  0;
	//XY方向的分速度
	m_vx = 0;
	m_vy = 0;
	//是否开火:否
	m_bFiring = false;
	//复活机会
	m_iChances  =  0;
	//初始化子弹类型
	FighterPlane::BulletType = PLAYER_ROUND;
	//调整坐标
	AdjustPosition();
}

// 刷新Player的状态
extern CShootApp theApp;
//更新函数
//类似于前面的介绍
void Player::Update()
{
	CShootView * pView;// = new CShootView;
	pView = (CShootView*)((CFrameWnd*)(theApp.m_pMainWnd))->GetActiveView();//->GetActiveView();
	if(!m_bUsed)
	{
		pView->ResetPlayer();
		return;
	}
	if(m_iLife>0)
	{
		if((rand()%(pView->GetLevel()*100) == 0)&&(m_iLife<20))//这段代码会动态随机增加玩家的生命值
		{
			m_iLife++;
			if(m_iLife>=20)
				m_iLife = 20;
		}
		//如果当前贴图坐标跑到屏幕区之外,就调整它到屏幕内
		if(m_x+m_vx<0)
		{
			m_x = 0;
			m_vx = 0;
		}
		else if(m_y+m_vy<0)
		{
			m_y = 0;
			m_vy = 0;
		}
		else if(m_x+m_w+m_vx>pView->GetClientWidth())
		{
			m_x = pView->GetClientWidth()-m_w-9;
			m_vx = 0;
		}
		else if(m_y+m_h+m_vy>pView->GetClientHeight()-1)
		{
			m_y = pView->GetClientHeight()-m_h;
			m_vy = 0;
		}
	}
	FighterPlane::Update();
}
//调整武器
void Player::ChangeWeapon()
{
	BulletType = (BULLET_TYPE)((BulletType+1)%3);
}
//为战斗机发行动指令
//输入:行为参数
//输出:改变玩家飞机的相应参数
void Player::Action(ACTION NewAction)
{
	if(m_iLife<=0) return;
	switch(NewAction)
	{
	case GO_LEFT://向左走
		m_vx = -m_speed;
		m_vy = 0;
		break;
	case GO_RIGHT://向右走
		m_vx = m_speed;
		m_vy = 0;
		break;
	case GO_UP://向上走
		m_vx = 0;
		m_vy = -m_speed;
		break;
	case GO_DOWN://向下走
		m_vx = 0;
		m_vy = m_speed;
		break;
	case GO_LEFT_UP://向左上方走
		m_vx = -0.7*m_speed;
		m_vy = -0.7*m_speed;
		break;
	case GO_LEFT_DOWN://向左下方走
		m_vx = -0.7*m_speed;
		m_vy = 0.7*m_speed;
		break;
	case GO_RIGHT_UP://向右上方走
		m_vx = 0.7*m_speed;
		m_vy = -0.7*m_speed;
		break;
	case GO_RIGHT_DOWN://向右下方走
		m_vx = 0.7*m_speed;
		m_vy = 0.7*m_speed;
		break;
	case STOP_MOVING://停止移动
		m_vx = 0;
		m_vy = 0;
		break;
	case START_FIRING://开火
		StartFiring();
		break;
	case STOP_FIRING://停火
		StopFiring();
		break;
	case CHANGE_WEAPON://改变武器
		ChangeWeapon();
		break;
	}
}
//开火函数
//调用的是主控程序类的函数PlayerFire()
void Player::Fire()
{
	CShootView * pView;
	pView = (CShootView*)((CFrameWnd*)(theApp.m_pMainWnd))->GetActiveView();
	if((signed) GetTickCount() - m_iLastFireTime < Bullet::GetFireInterval( BulletType ) )
		return;
	pView->PlayerFire(this);
	m_iLastFireTime = GetTickCount();
}

⌨️ 快捷键说明

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