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

📄 ghostthread.cpp

📁 泡泡堂单机版
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// GhostThread.cpp : implementation file
//

#include "stdafx.h"
#include "BubbleII.h"
#include "GhostThread.h"
#include "BubbleThread.h"
#include "GLB.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CGhostThread

IMPLEMENT_DYNCREATE(CGhostThread, CWinThread)

CGhostThread::CGhostThread()
{
	m_iDirect=(int)(rand()*4.0/RAND_MAX);
	m_fSpeed=1.0;
	m_iType=1;
	m_x=((int)(rand()*GLB::iXBlocks))*GLB::iBlockWidth;
	m_y=((int)(rand()*GLB::iYBlocks))*GLB::iBlockHeight;
	m_eState=GHOSTDEAD;
	m_pPartMemDC=NULL;
	m_pPartImageMemDC=NULL;
	m_bHumanCtrl=FALSE;
	m_bSetBubbleNotLeave=FALSE;
	m_iBubbleRadius=0;
	m_iLockedBMPIndex=0;
	m_iLifeLeft=0;
	m_iBubbleNum=0;
	m_iHurryCount=0;

	m_lScore=0;
	m_iWidth=GLB::iBlockWidth*GLB::iXBlocks;
	m_iHeight=GLB::iBlockHeight*GLB::iYBlocks;

	for(int i=0; i<24;i++)
		m_pPartMemBMP[i]=NULL;

	for(i=0;i<16;i++)
	{
		m_pDeadMemBMP[i]=NULL;
	}
	for(i=0; i<5; i++)
	{
		m_pLockedMemBMP[i]=NULL;
	}

	m_iFrameCount=0;
}

CGhostThread::~CGhostThread()
{
	for(int i=0; i<12; i++)
	{
		if(m_pPartMemBMP[i]!=NULL)
			delete m_pPartMemBMP[i];
	}
	for(i=0; i<16; i++)
	{
		delete m_pDeadMemBMP[i];
	}
	for(i=0; i<4; i++)
	{
		delete m_pLockedMemBMP[i];
	}
	delete m_pPartImageMemDC;
}

BOOL CGhostThread::InitInstance()
{
	// TODO:  perform and per-thread initialization here
	return TRUE;
}

int CGhostThread::ExitInstance()
{
	// TODO:  perform any per-thread cleanup here
	return CWinThread::ExitInstance();
}

BEGIN_MESSAGE_MAP(CGhostThread, CWinThread)
	//{{AFX_MSG_MAP(CGhostThread)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGhostThread message handlers

void CGhostThread::SetGhost(int bubblenum, double x, double y, int direct,  double speed, int waittime)
{
	m_x=x;
	m_y=y;
	m_iDirect=direct;
	m_fSpeed=speed;
	m_iWaittime=waittime;
	m_iLastBubbleIndex=0;
	m_iBubbleNum=bubblenum;
	m_iLeftBubbleNum=bubblenum;
	m_eState=ALIVE;
	m_iBubbleRadius=3;
}

void CGhostThread::Go(int iDirect, double speed)
{
	m_iDirect=iDirect;
	switch(iDirect)
	{
	case 0:
		m_y-=speed;
		m_pPartImageMemDC->SelectObject(m_pPartMemBMP[m_iFrameCount]);
		break;
	case 1:
		m_x-=speed;
		m_pPartImageMemDC->SelectObject(m_pPartMemBMP[m_iFrameCount+3]);
		break;
	case 2:
		m_y+=speed;
		m_pPartImageMemDC->SelectObject(m_pPartMemBMP[m_iFrameCount+6]);
		break;
	case 3:	
		m_x+=speed;
		m_pPartImageMemDC->SelectObject(m_pPartMemBMP[m_iFrameCount+9]);
		break;			
	default:
		break;
	}
	m_iFrameCount++;
	m_iFrameCount%=3;
}

int CGhostThread::Run(){
	int step=0,i=0;
	int iGoBlocks=0;
	BOOL bBreakOut=FALSE,bStopToWait=FALSE;
	double iTheCenterX=0,iTheCenterY=0;
	double iTempCenterX=0, iTempCenterY=0;
	int	LOCKTIME=4000;
	int LOCKTIMELEFT=LOCKTIME;

	while(GLB::bGameStarted)
	{
		//如果是电脑控制
		if(!m_bHumanCtrl)
		{
			if(m_eState==ALIVE)
			{
				LOCKTIMELEFT=LOCKTIME;
				m_pPartImageMemDC->SelectObject(m_pPartMemBMP[m_iFrameCount]);
				while(m_eState==ALIVE)
				{
					GLB::bGhostsAlive[m_iIndex]=TRUE;
					iGoBlocks=(int)(rand()*2.0/RAND_MAX)+1;
					step=(m_iDirect%2==0)?(iGoBlocks*GLB::iBlockHeight):(iGoBlocks*GLB::iBlockWidth);
					for(i=0; ((i<step) && (m_eState==ALIVE)); i++)
					{
						bBreakOut=FALSE;

						//判断是否到达游戏区域边缘
						bBreakOut=WillGoOut(&m_iDirect);

						//判断是否到达背景障碍边缘
						//对于电脑来说,Bubble是背景障碍的一种
						if(bBreakOut==FALSE)
							bBreakOut=WillGoIntoObstacle(&m_iDirect);

						//判断是否到达其他Ghost
						//自动方式下,电脑将选择掉头
						BOOL bTemp=WillGoIntoGhost(&m_iDirect);

						if(bBreakOut)
							break;
						Go(m_iDirect,m_fSpeed); 
						Sleep(16);
					}
					//此时决定下一步往哪里走
					//应该向人控制的Ghost那里走
					//检查人的位置,向人靠拢
					double r=0;
					for(int i=0; i<(m_iIndex%6)+1; i++)
						r=rand()*4.0/RAND_MAX;

					if(GLB::pGhostThread[28]->m_eState!=GHOSTDEAD ||
						GLB::pGhostThread[29]->m_eState!=GHOSTDEAD)
					{
						int i28X=(int)(GLB::pGhostThread[28]->m_x);
						int	i28Y=(int)(GLB::pGhostThread[28]->m_y);
						int	i29X=(int)(GLB::pGhostThread[29]->m_x);
						int	i29Y=(int)(GLB::pGhostThread[29]->m_y);
						int	i28AbsDelta=(int)((GLB::pGhostThread[28]->m_eState==GHOSTDEAD)*1000
							+abs((int)(i28X-m_x))+abs((int)(i28Y-m_y)));
						int	i29AbsDelta=(int)((GLB::pGhostThread[29]->m_eState==GHOSTDEAD)*1000
							+abs((int)(i29X-m_x))+abs((int)(i29Y-m_y)));					
						//向28靠拢
						if(i28AbsDelta<i29AbsDelta)
						{
							MakeDirect(i28X,i28Y,r);
						}
						//向29靠拢
						else
						{
							MakeDirect(i29X,i29Y,r);
						}
					}
					else
					{
						m_iDirect=(int)(r);
					}
				}
			}
			while(m_eState==LOCKED)
			{
				if(LOCKTIMELEFT>=0)
				{
					m_pPartImageMemDC->SelectObject(m_pLockedMemBMP[m_iLockedBMPIndex/2]);
					m_iLockedBMPIndex=(m_iLockedBMPIndex+1)%8;					
					LOCKTIMELEFT-=150;
					Sleep(150);
					if(m_iLockedBMPIndex==0)
						::PlaySound("SOUND\\HURRY.WAV",NULL,SND_NODEFAULT|SND_ASYNC);

				}
				else
				{
					m_eState=ALIVE;
				}
			}
			if(m_eState==GHOSTDEAD)
			{
				LOCKTIMELEFT=LOCKTIME;
				//刚刚死掉,要死的很壮烈.对美工要求太高...
				if(GLB::eGameType!=GAMEOVER)
					::PlaySound("SOUND\\KILL.WAV",NULL,SND_NODEFAULT|SND_ASYNC);
				for(int i=0; i<16; i++)
				{
					m_pPartImageMemDC->SelectObject(m_pDeadMemBMP[i]);
					Sleep(40);
				}
				
				while(m_eState==GHOSTDEAD)
				{
					GLB::bGhostsAlive[m_iIndex]=FALSE;
					Sleep(40);
				}
			}
		}
		//如果由人来控制
		else
		{
			//做些初始化工作
			if(m_eState==ALIVE)
			{
				m_pPartImageMemDC->SelectObject(m_pPartMemBMP[0]);
				LOCKTIMELEFT=LOCKTIME;
				while(m_eState==ALIVE)
				{
					GLB::bGhostsAlive[m_iIndex]=TRUE;
					bBreakOut=FALSE;

					//判断是否到达游戏区域边缘
					bBreakOut=WillGoOut(&m_iDirect);

					//判断是否到达背景障碍边缘
					if(bBreakOut==FALSE)
						bBreakOut=WillGoIntoObstacle(&m_iDirect);

					//判断是否到达其他Ghost
					if(bBreakOut==FALSE)
						bBreakOut=WillGoIntoGhost(&m_iDirect);

					if(bBreakOut==FALSE)
						bBreakOut=WillGoIntoBubble(&m_iDirect);

					//m_direct>4表示静止状态
					if(m_iDirect<4)// && !bTemp)
					{
						if(bBreakOut)
							Go(m_iDirect,0);
						else
							Go(m_iDirect,m_fSpeed);
					}				
					Sleep(m_iWaittime);
				}
			}
			while(m_eState==LOCKED)
			{
				if(LOCKTIMELEFT>=0)
				{
					m_pPartImageMemDC->SelectObject(m_pLockedMemBMP[m_iLockedBMPIndex/2]);
					m_iLockedBMPIndex=(m_iLockedBMPIndex+1)%8;					
					LOCKTIMELEFT-=150;
					Sleep(150);
					if(m_iLockedBMPIndex==0)
						::PlaySound("SOUND\\HURRY.WAV",NULL,SND_NODEFAULT|SND_ASYNC);
				}
				else
				{
					m_eState=ALIVE;
					LOCKTIMELEFT=LOCKTIME;
				}
			}
			if(m_eState==GHOSTDEAD)
			{
				//刚刚死掉,要死的很壮烈.可惜对美工要求太高.
				LOCKTIMELEFT=LOCKTIME;
				if(GLB::eGameType!=GAMEOVER)
					::PlaySound("SOUND\\KILL.WAV",NULL,SND_NODEFAULT|SND_ASYNC);
				for(int i=0; i<16; i++)
				{
					m_pPartImageMemDC->SelectObject(m_pDeadMemBMP[i]);
					Sleep(40);
				}
				
				while(m_eState==GHOSTDEAD)
				{
					GLB::bGhostsAlive[m_iIndex]=FALSE;
					Sleep(40);
				}
			}
		}
	}
	return 0;
}

BOOL CGhostThread::WillGoOut(int *direct)
{
	switch(*direct)
	{
	case 0:
		if(m_y - m_fSpeed <0)
		{
			m_y=0;
			return TRUE;
		}
		break;
	case 1:
		if(m_x - m_fSpeed <0)
		{

⌨️ 快捷键说明

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