C++版 小球自由落体的实现
来源:互联网 发布:世界图书出版社 知乎 编辑:程序博客网 时间:2024/04/30 15:30
/**@*complier:vc++ 2008*Date:8/11/2013*Author:pjgan*//**Param Describe: *m_ballreboundCount: the rebound count*m_ballnextpos: next position;*KBALLDESTINATIONPOS: the bottom of the ground*KBALLINITSPEED: the speed of the ball*m_pAcorn: the object( the ball)*m_ballState: the ball's state*/void CSEGameMainPage::balldrop(VINT32 idestionpos) { // the ball drop if(m_pAcorn[0]) { VINT32 iballcurrentYpos = m_pAcorn[0]->GetYpos(); if(iballcurrentYpos > idestionpos) { m_ballreboundCount++; //the rebount count VDOUBLE dreboundCountDistance = Global_pow(0.5, m_ballreboundCount); m_ballnextpos = KBALLDESTINATIONPOS - (KBALLDESTINATIONPOS * dreboundCountDistance);//set the rebound position m_ballState = BALLSTATE_REBOUND; // } else { /* *set the drop speed; */ m_InitballSpeed /= KBALLINITSPEED; m_InitballSpeed += KBALLINITSPEED*2; if((m_CurTime - m_LastTime) > 20) { m_pAcorn[0]->SetYpos(iballcurrentYpos + m_InitballSpeed); m_LastTime = m_CurTime; } } }}void CSEGameMainPage::ballrebound(VINT32 idestionpos) { // the ball rebound if(m_pAcorn[0]) { VINT32 iballcurrentYpos = m_pAcorn[0]->GetYpos(); if(iballcurrentYpos <= idestionpos) { m_ballnextpos = KBALLDESTINATIONPOS; m_ballState = BALLSTATE_DROP; } else { /* *set the rebound speed */ m_InitballSpeed /= KBALLINITSPEED; m_InitballSpeed += KBALLINITSPEED*2; if((m_CurTime - m_LastTime) > 20) { m_pAcorn[0]->SetYpos(iballcurrentYpos - m_InitballSpeed); m_LastTime = m_CurTime; } } }} switch(m_ballState) { case BALLSTATE_INITSTATE: SquirrelsRun(); break; case BALLSTATE_DROP: balldrop(m_ballnextpos); break; case BALLSTATE_REBOUND: if(m_ballnextpos < KBALLDESTINATIONPOS) { ballrebound(m_ballnextpos); } else { m_ballState = BALLSTATE_STOP; } break; case BALLSTATE_STOP: break; default: GEASSERT(0); break; }