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

📄 game.cpp

📁 C++台球游戏代码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
		SafeDelete(m_pSphere[i]);

	SafeDelete(m_pSoundHitBar);
	SafeDelete(m_pSoundHitCue0);
	SafeDelete(m_pSoundHitCue1);
	SafeDelete(m_pSoundFallInHole);
	SafeDelete(m_pSoundBimpacting0);
	SafeDelete(m_pSoundBimpacting1);

	LogInfo("<li>CleanUpGame finished.");
}

void CGame::ProcessMouse()
{
	DIMOUSESTATE MouseState;

	if(FAILED(m_pMouse->GetDeviceState(sizeof(MouseState),(LPVOID)&MouseState)))
		return;
   
	if(!m_bPlanform && m_pSphere[0]->GetSphereState())
	{
		//**************************************
		//***********Set Camera Scale***********
	
	    //Is the right mouse button down?
	    if(MOUSEBUTTONDOWN(MouseState.rgbButtons[MOUSEBUTTON_RIGHT]))
			m_iMouseRight=1;
	    else
		    m_iMouseRight=0;

	    if(m_iMouseRight==1)
		{
			m_fScaleTemp+=MouseState.lY/20.0f;
		    if(m_fScaleTemp>0.5 && m_fScaleTemp<2)
				m_fScale=m_fScaleTemp;
		    else if(m_fScaleTemp<0.5)
				m_fScaleTemp=0.5f;
		    else if(m_fScaleTemp>2)
			    m_fScaleTemp=2.0f;
		}
	    //**************************************
	    //**************************************

	
	    //**************************************
	    //*************MoveCue******************

	    if(!m_bCtrl)
		{
			m_iMouseY=5;
		    m_iMoveCue=5;
		}
	    else if(m_iMouseY<=32)
		    m_iMoveCue=m_iMouseY;
	    else 
		    m_iMouseY=32;

	   if(m_iMoveCue<4 && !m_bSendBall)
	   {
		   m_fCueVelocity=0.0f;
		   m_iMouseY=5;
		   m_iMoveCue=5;


		  //You can control Max cue speed at here.
		  m_fCueVelocity=fabsf((float)MouseState.lY)/26.0f;
		  if(m_fCueVelocity>CUEMAXSPEED)
			  m_fCueVelocity=CUEMAXSPEED;

		  m_pSphere[0]->SetSphereVelocityDir(-m_vecCueDir);
		  m_pSphere[0]->SetSphereVelocity(m_fCueVelocity);
	      m_bSendBall=TRUE;

		  if(m_fCueVelocity > 0.05f && m_bSoundCardState)
		  {
			  if(m_fCueVelocity < F_T_CUEMAXSPEED)
				  m_pSoundHitCue0->Play();
			  else
				  m_pSoundHitCue1->Play();
		  }
	   }

	   //**************************************
	   //**************************************
	}//For if(!m_bPlanform)

	if(m_bPlanform && MOUSEBUTTONDOWN(MouseState.rgbButtons[MOUSEBUTTON_LEFT]))
		if(CheckWhiteBallInAvailableArea() && !m_bCtrl)
			m_bPlanform=FALSE;

	
	m_iMouseX+=MouseState.lX;
	m_iMouseY+=MouseState.lY;

	m_iMouseStateX=MouseState.lX;
	m_iMouseStateY=MouseState.lY;
}

void CGame::ProcessKeyboard()
{
	char KeyboardState[256];

	if(FAILED(m_pKeyboard->GetDeviceState(sizeof(KeyboardState),(LPVOID)&KeyboardState)))
		return;

	if(KEYDOWN(KeyboardState,DIK_ESCAPE))
		m_bQuit=TRUE;

	if(KEYDOWN(KeyboardState,DIK_LCONTROL) || 
	   KEYDOWN(KeyboardState,DIK_RCONTROL))
		m_bCtrl=TRUE;
	else
		m_bCtrl=FALSE;

	if(KEYDOWN(KeyboardState,DIK_MULTIPLY))
	{
		m_fScaleTemp=1.0f;
		m_fScale=1.0f;
	}
}
void CGame::InitilizeSpheresPosition()
{
	m_pSphere[0]->SetSpherePosition(15.0f , 0.0f , 0.0f);

	m_pSphere[1]->SetSpherePosition(-12.0f , 0.0f ,  0.0f);
	m_pSphere[2]->SetSpherePosition(-14.3f , 0.0f , -1.2f);
	m_pSphere[3]->SetSpherePosition(-14.3f , 0.0f ,  1.2f);
	m_pSphere[4]->SetSpherePosition(-16.5f , 0.0f , -2.4f);
	m_pSphere[5]->SetSpherePosition(-16.5f , 0.0f ,  0.0f);
	m_pSphere[6]->SetSpherePosition(-16.5f , 0.0f ,  2.4f);
	m_pSphere[7]->SetSpherePosition(-18.7f , 0.0f , -1.2f);
	m_pSphere[8]->SetSpherePosition(-18.7f , 0.0f ,  1.2f);
	m_pSphere[9]->SetSpherePosition(-20.9f , 0.0f ,  0.0f);
	
	//add code for others's position.
}

void  CGame::SphereHitBarProcess(int NumOfSphere)
{
	int i;
	m_pSphere[NumOfSphere]->GetSpherePosition(m_vecRayPos);
	for(i=0;i<8;i++)
	{
		D3DXIntersect(m_pMesh,&m_vecRayPos,&m_vecRayDir[i],&m_bHitBar,
			          NULL,NULL,NULL,&m_fDistance,NULL,NULL);

		if((i==0 || i==2) && m_fDistance<1.1f)
		{
			m_pSphere[NumOfSphere]->ContradictoryZv();
			m_pSphere[NumOfSphere]->ReduceSphereVelocity(0.85f);
			m_pSphere[NumOfSphere]->GetSavedSpherePosition(m_vecRayPos);
			m_pSphere[NumOfSphere]->SetSpherePosition(m_vecRayPos.x,m_vecRayPos.y,m_vecRayPos.z);
			//*********************************************************************
			//*********************Code for spehre stick bar*********************** 
			if(m_vecRayPos2==m_vecRayPos)
			{
				D3DXVECTOR3 temp;
                temp=(m_fDistance-1.1f)*m_vecRayDir[i]+m_vecRayPos;
				m_pSphere[NumOfSphere]->SetSpherePosition(temp.x,temp.y,temp.z);
                m_pSphere[NumOfSphere]->SaveSpherePosition();
			}
			else if(m_bSoundCardState)
				m_pSoundHitBar->Play();
			m_vecRayPos2=m_vecRayPos;
			//*********************************************************************
			//*******************************end***********************************
			break;
		}
		else if((i==1 || i==3) && m_fDistance<1.1f)
		{
			m_pSphere[NumOfSphere]->ContradictoryXv();
			m_pSphere[NumOfSphere]->ReduceSphereVelocity(0.85f);
			m_pSphere[NumOfSphere]->GetSavedSpherePosition(m_vecRayPos);
			m_pSphere[NumOfSphere]->SetSpherePosition(m_vecRayPos.x,m_vecRayPos.y,m_vecRayPos.z);
            //*********************************************************************
			//*********************Code for spehre stick bar***********************			
			if(m_vecRayPos2==m_vecRayPos)
			{
				D3DXVECTOR3 temp;
                temp=(m_fDistance-1.1f)*m_vecRayDir[i]+m_vecRayPos;
				m_pSphere[NumOfSphere]->SetSpherePosition(temp.x,temp.y,temp.z);
                m_pSphere[NumOfSphere]->SaveSpherePosition();
			}
			else if(m_bSoundCardState)
				m_pSoundHitBar->Play();
			m_vecRayPos2=m_vecRayPos;
			//*********************************************************************
			//*******************************end***********************************
			break;
		}
		//Code for 15,165,195,345 degree
		//Code for 45,135,225,315 degree
		else if(m_fDistance<1.1f) 
		{
			m_pSphere[NumOfSphere]->MirrorVAoubtAxis(-m_vecRayDir[i]);
			m_pSphere[NumOfSphere]->ReduceSphereVelocity(0.85f);
			m_pSphere[NumOfSphere]->GetSavedSpherePosition(m_vecRayPos);
			m_pSphere[NumOfSphere]->SetSpherePosition(m_vecRayPos.x,m_vecRayPos.y,m_vecRayPos.z);
			//*********************************************************************
			//*********************Code for spehre stick bar***********************
			if(m_vecRayPos2==m_vecRayPos)
			{
				D3DXVECTOR3 temp;
                temp=(m_fDistance-1.1f)*m_vecRayDir[i]+m_vecRayPos;
				m_pSphere[NumOfSphere]->SetSpherePosition(temp.x,temp.y,temp.z);
                m_pSphere[NumOfSphere]->SaveSpherePosition();
			}
			else if(m_bSoundCardState)
				m_pSoundHitBar->Play();
			m_vecRayPos2=m_vecRayPos;
			//*********************************************************************
			//*******************************end***********************************
			break;
		}		
	}
	if(m_fDistance>=1.1f)
		m_pSphere[NumOfSphere]->SaveSpherePosition();
}

void CGame::SphereHitSphereProcess(int NumOfSphere)
{
	D3DXVECTOR3 sph1,sph2;
    int i;
	BOOL bFlag=FALSE;

	m_pSphere[NumOfSphere]->GetSpherePosition(sph1);
    
	for(i=0;i<SUMBALL;i++)
	{
		if(i != NumOfSphere)
		{
		    m_pSphere[i]->GetSpherePosition(sph2);
		    if( (sph1.x-sph2.x)*(sph1.x-sph2.x) + 
			    (sph1.z-sph2.z)*(sph1.z-sph2.z) < 4.84f)
			{
				m_pSphere[NumOfSphere]->GetSavedSpherePosition2(sph1);
			    m_pSphere[NumOfSphere]->SetSpherePosition(sph1.x,0.0f,sph1.z);
			    m_pSphere[i]->GetSavedSpherePosition2(sph2);
			    m_pSphere[i]->SetSpherePosition(sph2.x,0.0f,sph2.z);
		        CollisionResponseBetweenSphere(NumOfSphere,i);
				bFlag=TRUE;
				
				if(m_bSoundCardState)
				{
					if(m_pSphere[i]->GetSphereVelocity() < HALFCUEMAXSPEED)
						m_pSoundBimpacting0->Play();
					else 
						m_pSoundBimpacting1->Play();
				}

			}			
		}
	}

	if(!bFlag)
	{
		m_pSphere[NumOfSphere]->SaveSpherePosition2();
	}
}

void CGame::CollisionResponseBetweenSphere(int i,int j)
{
	D3DXVECTOR3 n,t,poti,potj;
	D3DXVECTOR3 viDir,vjDir,vi,vj;
	D3DXVECTOR3 xzx,xzz,nni,nnj,tti,ttj;
	float vim,vjm;
	float ni,ti,nj,tj;
	float temp;
	float xi,zi,xj,zj;
	xzx.x=1.0f;
    xzx.y=0.0f;
	xzx.z=0.0f;
	
	xzz.x=0.0f;
    xzz.y=0.0f;
	xzz.z=1.0f;

	m_pSphere[i]->GetSavedSpherePosition2(poti);
	m_pSphere[j]->GetSavedSpherePosition2(potj);
	
	m_pSphere[i]->GetSphereVelocityDir(viDir);
	D3DXVec3Normalize(&viDir,&viDir);
	m_pSphere[j]->GetSphereVelocityDir(vjDir);
	D3DXVec3Normalize(&vjDir,&vjDir);

	vim=m_pSphere[i]->GetSphereVelocity();
    vjm=m_pSphere[j]->GetSphereVelocity();

	vi=vim*viDir;
	vj=vjm*vjDir;
	
	n=poti-potj;
	D3DXVec3Normalize(&n,&n);
	t.x=-n.z;
	t.z=n.x;
	t.y=0.0f;

	ni=D3DXVec3Dot(&vi,&n);
	ti=D3DXVec3Dot(&vi,&t);
	nj=D3DXVec3Dot(&vj,&n);
	tj=D3DXVec3Dot(&vj,&t);

	temp=ni;
	ni=nj;
	nj=temp;

	nni=ni*n;
	tti=ti*t;

	nnj=nj*n;
	ttj=tj*t;

	xi=D3DXVec3Dot(&nni,&xzx)+D3DXVec3Dot(&tti,&xzx);
	zi=D3DXVec3Dot(&nni,&xzz)+D3DXVec3Dot(&tti,&xzz);
	poti.x=xi;
	poti.y=0.0f;
	poti.z=zi;
	D3DXVec3Normalize(&poti,&poti);
    m_pSphere[i]->SetSphereVelocityDir(poti);
	vim=sqrtf(xi*xi+zi*zi)*0.95f;
	m_pSphere[i]->SetSphereVelocity(vim);

	xj=D3DXVec3Dot(&nnj,&xzx)+D3DXVec3Dot(&ttj,&xzx);
	zj=D3DXVec3Dot(&nnj,&xzz)+D3DXVec3Dot(&ttj,&xzz);
	potj.x=xj;
	potj.y=0.0f;
	potj.z=zj;
	D3DXVec3Normalize(&potj,&potj);
	m_pSphere[j]->SetSphereVelocityDir(potj);
	vjm=sqrtf(xj*xj+zj*zj)*0.95f;
	m_pSphere[j]->SetSphereVelocity(vjm);
}

void CGame::SphereFallInHoleProcess(int NumOfSphere)
{
	m_pSphere[NumOfSphere]->GetSpherePosition(m_vecRayPos);
	D3DXIntersect(m_pMesh,&m_vecRayPos,&m_vecRayDir[8],&m_bHitBar,
			      NULL,NULL,NULL,&m_fDistance,NULL,NULL);

	if(m_fDistance>1.2f && !m_bPlanform)
		m_pSphere[NumOfSphere]->SetSphereVelocityAt_Y_NegativeAxis();
	
	if(m_pSphere[NumOfSphere]->GetSpherePosAt_Y_Axis() <= -0.3f)
	{
		float Velocity=0.0f;
		m_pSphere[NumOfSphere]->SetSphereStateToFalse();
		m_pSphere[NumOfSphere]->SetSphereVelocity(Velocity);
        m_pSphere[NumOfSphere]->SetSpherePosition(10000.0f,10000.0f,10000.0f);
		if(m_bSoundCardState)
			m_pSoundFallInHole->Play();
	}

	if(!m_pSphere[0]->GetSphereState() && m_fSysEnergy == 0.0f)
	{	
		m_bPlanform=TRUE;
		m_pSphere[0]->SetSphereStateToTrue();
	}
}

BOOL CGame::CheckWhiteBallInAvailableArea()
{
	int i;
	D3DXVECTOR3 sph1,sph2;
	
	m_pSphere[0]->GetSpherePosition(sph1);
	for(i=1;i<SUMBALL;i++)
	{
		m_pSphere[i]->GetSpherePosition(sph2);
		if( (sph1.x-sph2.x)*(sph1.x-sph2.x) + 
			(sph1.z-sph2.z)*(sph1.z-sph2.z) < 4.84f)
			return FALSE;
	}

	for(i=0;i<8;i++)
	{
		D3DXIntersect(m_pMesh,&m_vecRayPos,&m_vecRayDir[i],&m_bHitBar,
			          NULL,NULL,NULL,&m_fDistance,NULL,NULL);
	    if(m_fDistance<=1.1f)
			return FALSE;
	}
    
	D3DXIntersect(m_pMesh,&m_vecRayPos,&m_vecRayDir[8],&m_bHitBar,
			      NULL,NULL,NULL,&m_fDistance,NULL,NULL);
	if(m_fDistance>1.2f)
		return FALSE;

	return TRUE;
}

BOOL CGame::InitializeDirectAudio(HWND hwnd)
{
	LogInfo("<br>Initialize DirectAudio:");

	//Create the DirectAudio performance object
	if(CoCreateInstance(CLSID_DirectMusicPerformance,NULL,
		                CLSCTX_INPROC,IID_IDirectMusicPerformance8,
						(void**)& m_pDirectAudioPerformance) != S_OK)
	{
		LogError("<li>Failed to create the DirectAudio performance object.");
		return FALSE;
	}
	else
		LogInfo("<li>DirectAudio performance object created OK.");

	//Ceate the DirectAudio loader object.
	if(CoCreateInstance(CLSID_DirectMusicLoader,NULL,CLSCTX_INPROC,
		                IID_IDirectMusicLoader8,(void**) &m_pDirectAudioLoader) != S_OK)
	{
		LogError("<li>Failed to create the DirectAudio loader object.");
		return FALSE;
	}
	else
		LogInfo("<li>DirectAudio loader object created OK.");

	//Initialize the performance object.
	if(FAILED(m_pDirectAudioPerformance->InitAudio(NULL,NULL,hwnd,DMUS_APATH_SHARED_STEREOPLUSREVERB,
                                                   64,DMUS_AUDIOF_ALL,NULL)))
	{
		LogError("<li>Failed to initialize the DirectAudio performance object.");
		return FALSE;
	}
	else
		LogInfo("<li>Initialised the DirectAudio performance object OK.");

	//Get the applications "sounds" directory.
	CHAR strSoundPath[MAX_PATH];
	GetCurrentDirectory(MAX_PATH,strSoundPath);
	strcat(strSoundPath,"\\Sounds");

	//Convert the path to unicode.
	WCHAR wstrSoundPath[MAX_PATH];
	MultiByteToWideChar(CP_ACP,0,strSoundPath,-1,wstrSoundPath,MAX_PATH);

	//Set the search directory.
	if(FAILED(m_pDirectAudioLoader->SetSearchDirectory(GUID_DirectMusicAllTypes,
		                                               wstrSoundPath,FALSE)))
	{
		LogError("<li>Failed to set the search directory '%s'.",strSoundPath);
		return FALSE;
	}
	else
		LogInfo("<li>Search directory '%s' set OK.",strSoundPath);

	return TRUE;
}



⌨️ 快捷键说明

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