📄 ball.cpp
字号:
// Ball.cpp: implementation of the CBall class.
//
//////////////////////////////////////////////////////////////////////
#include "Ball.h"
#include <e32math.h>
#define PI 3.1415926
CBall::CBall()
{
Init();
}
CBall::~CBall()
{
}
void CBall::ConstructL(CFbsBitmap* aBmp,CFbsBitmap* aBmpMask,TInt aCellWidth/*=0*/,TInt aCellHeight/*=0*/)
{
CSprite::ConstructL(aBmp,aBmpMask,aCellWidth,aCellHeight);
}
CBall* CBall::NewL( CFbsBitmap* aBmp,CFbsBitmap* aBmpMask,TInt aCellWidth/*=0*/,TInt aCellHeight/*=0*/ )
{
CBall* self=CBall::NewLC(aBmp,aBmpMask,aCellWidth,aCellHeight);
CleanupStack::Pop();
return self;
}
CBall* CBall::NewLC( CFbsBitmap* aBmp,CFbsBitmap* aBmpMask,TInt aCellWidth/*=0*/,TInt aCellHeight/*=0*/ )
{
CBall* self=new(ELeave)CBall;
CleanupStack::PushL(self);
self->ConstructL(aBmp,aBmpMask,aCellWidth,aCellHeight);
return self;
}
void CBall::Init()
{
iX=0;
iY=0;
iSpeedX=0;
iSpeedY=0;
iRect.SetRect(0,0,0,0);
}
void CBall::SetActiveRect( const TRect& aRect )
{
iRect=aRect;
}
void CBall::SetSpeed( TInt aX,TInt aY )
{
iSpeedX=aX;
iSpeedY=aY;
}
void CBall::CollideWithActiveRect()
{
//预计位置
TInt px=iX+iSpeedX;
TInt py=iY+iSpeedY;
TBool reachTop=EFalse;
TBool reachRight=EFalse;
TBool reachLeft=EFalse;
TBool reachBottom=EFalse;
if (py<iRect.iTl.iY)
{
reachTop=ETrue;
}
if (px+iCellWidth>iRect.iBr.iX)
{
reachRight=ETrue;
}
if (px<iRect.iTl.iX)
{
reachLeft=ETrue;
}
if (py+iCellHeight>iRect.iBr.iY)
{
reachBottom=ETrue;
}
if (reachTop)//到项部
{
py=iRect.iTl.iY;
iSpeedY=-iSpeedY;
}
if (reachRight)
{
px=iRect.iBr.iX-iCellWidth;
iSpeedX=-iSpeedX;
}
if (reachLeft)
{
px=iRect.iTl.iX;
iSpeedX=-iSpeedX;
}
if (reachBottom)
{
py=iRect.iBr.iY-iCellHeight;
iSpeedY=-iSpeedY;
}
iX=px;
iY=py;
}
void CBall::CollideWithBall(CBall& aBall)
{
//预计位置
TInt px=iX+iSpeedX;
TInt py=iY+iSpeedY;
//圆心的距离
TInt disX=px+iCellWidth-(aBall.iX+aBall.iSpeedX+aBall.iCellWidth);
TInt disY=py+iCellHeight-(aBall.iY+aBall.iSpeedY+aBall.iCellHeight);
TReal dis2=disX*disX+disY*disY;
if (dis2<(iCellWidth/2+aBall.iCellWidth/2)*(iCellWidth/2+aBall.iCellWidth/2))
{
TInt tempX=iSpeedX;
TInt tempY=iSpeedY;
aBall.GetSpeed(iSpeedX,iSpeedY);
aBall.SetSpeed(tempX,tempY);
}
}
void CBall::GetSpeed( TInt& aX,TInt& aY )
{
aX=iSpeedX;
aY=iSpeedY;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -