#include "CommonAPI.h"
#include "LessonX.h"
#include
#include
using namespace std;
bool g_bStart = false;
int g_iGameScore = 0;
float g_fGameTime = 0.f;
float g_fOldRotation = 0.f;
float g_fRotateTime = 0.f;
float g_fScreenLeft = 0.f;
float g_fScreenRight = 10.f;
float g_fScreenTop = 0.f;
float g_fScreenBottom = 10.f;
int g_iCount=0;
int flag=0;
int iHighScore[10];
bool cmp(int a,int b)
{
return a>b;
}
struct FlyBug
{
char szName[128];
int iScore;
} FlyBugs[30];
void AddFlyBugs()
{
for(int i=0; i<=11; i++)
{
printf("add %d\n",g_iCount);
if(i>=0&&i<=4)
{
sprintf(FlyBugs[g_iCount].szName, "feichong_%d", g_iCount);
FlyBugs[g_iCount].iScore = 1;
g_iCount++;
}
else
{
sprintf(FlyBugs[g_iCount].szName, "feichong_%d", g_iCount);
FlyBugs[g_iCount].iScore = 2;
g_iCount++;
}
dSetSpriteVisible(FlyBugs[i].szName,1);
}
}
void Flying(const char *szName)
{
int iDir = 0;
iDir = dRandomRange(0,3);
float fSpeed=dRandomRange(10,20);
float ideaFposX=dRandomRange(g_fScreenLeft,g_fScreenRight);
float ideaFposY=dRandomRange(g_fScreenBottom,g_fScreenTop);
switch(iDir)
{
case 0:
{
float fPosX=dRandomRange(g_fScreenLeft, g_fScreenLeft+5.f);
float fPosY=dRandomRange(g_fScreenBottom-5.f, g_fScreenTop+5.f);
dSetSpritePosition(szName,fPosX,fPosY);
dSpriteMoveTo(szName, ideaFposX, ideaFposY, fSpeed,false);
printf("%s ideax:%lf ideay:%lf v:%lf fromx:%lf formy:%lf l:%lf r:%lf t:%lf b:%lf\n",szName,ideaFposX,ideaFposY,fSpeed,fPosX,fPosY,g_fScreenLeft,g_fScreenRight,g_fScreenTop,g_fScreenBottom);
break;
}
case 1:
{
float fPosX=dRandomRange(g_fScreenLeft-5.f, g_fScreenRight+5.f);
float fPosY=dRandomRange(g_fScreenBottom, g_fScreenBottom+5.f);
dSetSpritePosition(szName,fPosX,fPosY);
dSpriteMoveTo(szName, ideaFposX, ideaFposY, fSpeed,false);
printf("%s ideax:%lf ideay:%lf v:%lf fromx:%lf formy:%lf l:%lf r:%lf t:%lf b:%lf\n",szName,ideaFposX,ideaFposY,fSpeed,fPosX,fPosY,g_fScreenLeft,g_fScreenRight,g_fScreenTop,g_fScreenBottom);
break;
}
case 2:
{
float fPosX=dRandomRange(g_fScreenRight-5.f, g_fScreenRight);
float fPosY=dRandomRange(g_fScreenBottom-5.f, g_fScreenTop+5.f);
dSetSpritePosition(szName,fPosX,fPosY);
dSpriteMoveTo(szName, ideaFposX, ideaFposY, fSpeed,false);
printf("%s ideax:%lf ideay:%lf v:%lf fromx:%lf formy:%lf l:%lf r:%lf t:%lf b:%lf\n",szName,ideaFposX,ideaFposY,fSpeed,fPosX,fPosY,g_fScreenLeft,g_fScreenRight,g_fScreenTop,g_fScreenBottom);
break;
}
case 3:
{
float fPosX=dRandomRange(g_fScreenLeft-5.f, g_fScreenRight+5.f);
float fPosY=dRandomRange(g_fScreenTop-5.f, g_fScreenTop);
dSetSpritePosition(szName,fPosX,fPosY);
dSpriteMoveTo(szName, ideaFposX, ideaFposY, fSpeed,false);
printf("%s ideax:%lf ideay:%lf v:%lf fromx:%lf formy:%lf l:%lf r:%lf t:%lf b:%lf\n",szName,ideaFposX,ideaFposY,fSpeed,fPosX,fPosY,g_fScreenLeft,g_fScreenRight,g_fScreenTop,g_fScreenBottom);
break;
}
}
}
int PASCAL WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
if( !dInitGameEngine( hInstance, lpCmdLine ) )
return 0;
dSetWindowTitle("Lesson");
g_fScreenLeft=dGetScreenLeft();
g_fScreenRight=dGetScreenRight();
g_fScreenTop=dGetScreenTop();
g_fScreenBottom=dGetScreenBottom();
g_fOldRotation=dGetSpriteRotation("paizi");
while( dEngineMainLoop() )
{
float fTimeDelta = dGetTimeDelta();
dSetTextValue("time",g_fGameTime);
if(g_bStart)
{
dSetSpriteVisible("kaishi",0);
g_fGameTime -= fTimeDelta;
dSetTextValueFloat("Time",g_fGameTime);
if(g_fGameTime > 0)
{
if(g_fRotateTime > 0)
{
g_fRotateTime -= fTimeDelta;
if(g_fRotateTime <= 0)
{
dSetSpriteRotation("paizi", g_fOldRotation);
}
}
}
else
{
printf("over\n");
g_bStart = false;
}
}
else if(g_bStart== false)
{
for(int i=0; i<g_iCount; i++)
{
char szName[128];
sprintf(szName, "feichong_%d",i);
dDeleteSprite(szName);
}
g_iCount=0;
dSetSpriteVisible("kaishi",1);
flag=0;
iHighScore[5]=g_iGameScore;
g_iGameScore=0;
}
sort(iHighScore,iHighScore+6,cmp);
for(int i=0;i<=4;i++){
char szName[128];
sprintf(szName,"Score%d",i);
dSetTextValue(szName,iHighScore[i]);
}
dSetTextValue("Score",g_iGameScore);
GameMainLoop( fTimeDelta );
}
dShutdownGameEngine();
return 0;
};
void dOnMouseMove( const float fMouseX, const float fMouseY )
{
if(g_bStart)
{
dSetSpritePosition("paizi",fMouseX,fMouseY);
}
OnMouseMove(fMouseX, fMouseY );
}
void dOnMouseClick( const int iMouseType, const float fMouseX, const float fMouseY )
{
if(!g_bStart || iMouseType!=0 || g_fRotateTime>0.f)
{
printf("return\n");
return ;
}
g_fRotateTime=0.2f;
dSetSpriteRotation("paizi", g_fOldRotation-10);
for(int i=0; i<g_iCount; i++)
{
float fPosX=dGetSpritePositionX(FlyBugs[i].szName);
float fPosY=dGetSpritePositionY(FlyBugs[i].szName);
if(dIsPointInSprite("paizi",fPosX,fPosY))
{
g_iGameScore+=FlyBugs[i].iScore;
Flying(FlyBugs[i].szName);
}
}
OnMouseClick(iMouseType, fMouseX, fMouseY);
}
void dOnMouseUp( const int iMouseType, const float fMouseX, const float fMouseY )
{
OnMouseUp(iMouseType, fMouseX, fMouseY);
}
void dOnKeyDown( const int iKey, const int iAltPress, const int iShiftPress, const int iCtrlPress )
{
if(KEY_SPACE == iKey)
{
g_bStart = true;
if(flag==0)
{
AddFlyBugs();
g_fGameTime=15.0f;
flag=1;
}
for(int i=0; i<g_iCount; i++)
{
if(FlyBugs[i].iScore==1)
{
printf("in1\n");
if(dCloneSprite("feichong_muban1",FlyBugs[i].szName)){
printf("yes1\n");
}
}
else if(FlyBugs[i].iScore==2)
{
printf("in2\n");
if(dCloneSprite("feichong_muban2",FlyBugs[i].szName)){
printf("yes2\n");
}
}
dSetSpriteWorldLimit(FlyBugs[i].szName,WORLD_LIMIT_NULL,g_fScreenLeft-10.0f,g_fScreenTop-10.0f,g_fScreenRight+10.0f,g_fScreenBottom+10.0f);
Flying(FlyBugs[i].szName);
}
}
OnKeyDown(iKey, iAltPress, iShiftPress, iCtrlPress);
}
void dOnKeyUp( const int iKey )
{
OnKeyUp(iKey);
}
void dOnSpriteColSprite( const char *szSrcName, const char *szTarName )
{
OnSpriteColSprite(szSrcName, szTarName);
}
void dOnSpriteColWorldLimit( const char *szName, const int iColSide )
{
if(strstr(szName,"feichong"))
{
for(int i=0; i<g_iCount; i++)
{
if(strcmp(szName,FlyBugs[i].szName)==0)
{
Flying(szName);
}
}
}
OnSpriteColWorldLimit(szName, iColSide);
}