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

📄 playertank.cpp

📁 坦克游戏
💻 CPP
字号:
// PlayerTank.cpp: implementation of the CPlayerTank class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "PlayerTank.h"
#include "MainGame.h"

extern int staticspeed1,staticspeed2;

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CPlayerTank::CPlayerTank(int px,int py,int dir,int type):CBaseTank(this)
{
	boolFlip=1;
	iPos=0;
	appearance=0;		//0 - 79: 只有小星星 80: 坦克原形
						//当坦克现出原形后方可响应键盘
///////////////////
	positionX = px;		//位置
	positionY = py;		
	direction = dir;	//方向
	this->type = type;	//0 --15 各种坦克
	width=28;
	height=28;			//坦克的大小
	speed=1;			//坦克的速度
//////////////////////

	movement=0;
	chaDirection=-1;	//不可改变方向. 0 - 3 有消息了且方向可改变.
//////////////////////

	bulletSpeed = 5;
	power = 10;
	bulletNum = 2;
//////////////////////
	strcpy(name,"PlayerTank");
	clearStatus();
	score=0;
	fireType=1;
}

CPlayerTank::~CPlayerTank()
{
	
}

void CPlayerTank::GetSpeed()
{
	if(speed==1)speed=2;
}
void CPlayerTank::GetAttack()
{
	if(type<7)
		type++;
	bulletSpeed+=2;
}


void CPlayerTank::ShowInformation()
{
/*	int i=type/4;
	
	CMainGame *pWnd=(CMainGame*)AfxGetMainWnd();
	DDSetColorKey(pWnd->lpBKGObject[16],RGB(4,4,4));
////////////////////////////////////
	if(i)	//player1
	{
	pWnd->lpBackBuffer->Blt(
		CRect(0,32*14+6,24,32*14+24+6),//dest rect  22*22
		pWnd->lpBKGObject[17],//src surface
		CRect(24,0,24*2,24),//src rect
		DDBLT_WAIT|DDBLT_KEYSRC, 
		NULL);

/*	
	HDC hdc;
	pWnd->lpBackBuffer->GetDC(&hdc);
	CBrush br;
	br.CreateSolidBrush( RGB( 10, 30, 20 ));
	SetBkColor( hdc, RGB( 0, 0, 255 ) );
	SelectObject(&br);
	Rectangle(hdc,10,20,30,40);
	FloodFill(hdc,10,20,RGB( 0, 0, 0 ));
	SetTextColor( hdc, RGB( 255, 255, 0 ) );
	TextOut( hdc, 220, 200, "Zhujian", lstrlen("Zhujian"));
    pWnd->lpBackBuffer->ReleaseDC(hdc);

	}
	else	//player2
	{
	pWnd->lpBackBuffer->Blt(
		CRect(32*19+8,32*14+6,32*19+8+24,32*14+24+6),//dest rect  22*22
		pWnd->lpBKGObject[17],//src surface
		CRect(0,0,24,24),//src rect
		DDBLT_WAIT|DDBLT_KEYSRC, 
		NULL);
	}
	*/
}

bool CPlayerTank::ActiveObject()
{
	changeDirection();	//时刻检测方向
	
//	ShowInformation();
	if(getStatus())		//运动的
		movement=1;
	else 
		movement=0;		//静止的

	if(type>3 && !staticspeed1)movement=0;
	if(type<4 && !staticspeed2)movement=0;
	return CBaseTank::ActiveObject();
}



//////////////////////////////////////////
//
//	坦克状态函数,配合键盘函数
//
//////////////////////////////////////////

int CPlayerTank::getStatus()
{
	for(int i=0;i<4;i++)
		if(dirStatus[i])return 1;
	return 0;
}


void CPlayerTank::clearStatus()
{
	for(int i=0;i<4;i++)
		dirStatus[i]=0;
	return;
}


void CPlayerTank::changeDirection()
{
	if(chaDirection==-1)return;	//此时不可以改变文方向
	else
	{
		if(iPos==0)//按键时叛断是否可以改变方向
		{
			direction=chaDirection;
			chaDirection=-1;
		}
	}
}




/////////////////////////////////////////////////////
//	键盘处理函数。1-Up,2-Right,3-Down,4-Left,5-Fire
//	
////////////////////按下键,n为什么键/////////////////

void CPlayerTank::KeyPress(int n)
{
	if(appearance<80)return;
	switch(n)
	{
	case 1: clearStatus();dirStatus[0]=1;chaDirection=0;break;
	case 2: clearStatus();dirStatus[1]=1;chaDirection=1;break;
	case 3: clearStatus();dirStatus[2]=1;chaDirection=2;break;
	case 4: clearStatus();dirStatus[3]=1;chaDirection=3;break;
	case 5:	Tankfire(fireType); //break;//开火
	}
}

void CPlayerTank::KeyRelease(int n)
{
	if(appearance<80)return;
	switch(n)
	{
	case 1: dirStatus[0]=0;break;
	case 2: dirStatus[1]=0;break;
	case 3: dirStatus[2]=0;break;
	case 4: dirStatus[3]=0;break;
	}
}

⌨️ 快捷键说明

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