代码
#include "CommonAPI.h"
#include "LessonX.h"
#include<bits/stdc++.h>
int g_iPlayState
=0;
int g_iHookRotToLeft
=0;
float g_fHookRotation
=0.f,g_fVhook
=120.f;
int g_iGoldCount
=0;
int iInitedHookPos
= 0,g_iGetGoldState
=0;
float g_fHookStartPosX
,g_fHookStartPosY
,g_iGoldBornMinX
,g_iGoldBornMaxX
,g_iGoldBornMinY
,g_iGoldBornMaxY
;
void DrawHookLine();
float g_fEmptyHookSpeed
=50.0f;
char g_szCurGetGold
[128];
void Init()
{
g_iGoldCount
=20;
g_iHookRotToLeft
=0;
g_fHookRotation
=dGetSpriteRotation("goldhook");
if( 0 == iInitedHookPos
)
{
iInitedHookPos
= 1;
g_fHookStartPosX
= dGetSpritePositionX( "goldHook" );
g_fHookStartPosY
= dGetSpritePositionY( "goldHook" );
g_iGoldBornMinX
= dGetScreenLeft() + 5;
g_iGoldBornMaxX
= dGetScreenRight() - 5;
g_iGoldBornMinY
= dGetScreenTop() + 20;
g_iGoldBornMaxY
= dGetScreenBottom() - 5;
}
for(int i
=0; i
<g_iGoldCount
; i
++)
{
char szName
[128];
sprintf(szName
,"goldBlock%d",i
);
dCloneSprite("goldTemplate",szName
);
float iSize
;
if(i
>=0&&i
<=9)
{
iSize
=4.0f;
}
else if(i
>=10&&i
<=15)
{
iSize
=6.0f;
}
else
{
iSize
=8.0f;
}
dSetSpriteWidth(szName
,iSize
);
dSetSpriteHeight(szName
,iSize
);
float fPosX
=dRandomRange(g_iGoldBornMinX
,g_iGoldBornMaxX
);
float fPosY
=dRandomRange(g_iGoldBornMinY
,g_iGoldBornMaxY
);
dSetSpritePosition(szName
,fPosX
,fPosY
);
}
dAnimateSpritePlayAnimation( "goldMan", "GolderManAnimation2", 0 );
}
void Run(float time
)
{
if(g_iHookRotToLeft
==1 && g_iGetGoldState
==0)
{
g_fHookRotation
+=time
*g_fVhook
;
if(g_fHookRotation
>=180)
{
g_iHookRotToLeft
=0;
g_fHookRotation
=180;
}
}
else if(g_iHookRotToLeft
==0 && g_iGetGoldState
==0)
{
g_fHookRotation
-=time
*g_fVhook
;
if(g_fHookRotation
<=0)
{
g_iHookRotToLeft
=1;
g_fHookRotation
=0;
}
}
dSetSpriteRotation("goldhook",g_fHookRotation
);
if( 2 == g_iGetGoldState
|| 3 == g_iGetGoldState
)
{
float fSpeedX
= dGetSpriteLinearVelocityX( "goldhook" );
float fSpeedY
= dGetSpriteLinearVelocityY( "goldhook" );
if( fSpeedX
> 0.00001f || fSpeedX
< -0.00001f || fSpeedY
> 0.00001f || fSpeedY
< -0.00001f )
return;
if( 3 == g_iGetGoldState
)
{
g_iGoldCount
--;
dSpriteDismount( g_szCurGetGold
);
dDeleteSprite( g_szCurGetGold
);
}
g_iGetGoldState
= 0;
dAnimateSpritePlayAnimation( "goldMan", "GolderManAnimation2", 0 );
}
}
int PASCAL
WinMain(HINSTANCE hInstance
,
HINSTANCE hPrevInstance
,
LPSTR lpCmdLine
,
int nCmdShow
)
{
if( !dInitGameEngine( hInstance
, lpCmdLine
) )
return 0;
dSetWindowTitle("Lesson");
while( dEngineMainLoop() )
{
float fTimeDelta
= dGetTimeDelta();
DrawHookLine();
dSetTextValue("text",g_iGoldCount
);
switch( g_iPlayState
)
{
case 1:
{
Init();
g_iPlayState
= 2;
}
break;
case 2:
{
if( g_iGoldCount
> 0 )
{
Run( fTimeDelta
);
}
else
{
g_iPlayState
= 0;
dSetSpriteVisible("GameBegin",1);
}
}
break;
case 0:
default:
break;
};
GameMainLoop( fTimeDelta
);
};
dShutdownGameEngine();
return 0;
}
void dOnMouseMove( const float fMouseX
, const float fMouseY
)
{
OnMouseMove(fMouseX
, fMouseY
);
}
void dOnMouseClick( const int iMouseType
, const float fMouseX
, const float fMouseY
)
{
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_iPlayState
==0)
{
g_iPlayState
=1;
dSetSpriteVisible("GameBegin",0);
}
else if( KEY_DOWN
== iKey
&& 2 == g_iPlayState
&& 0 == g_iGetGoldState
)
{
g_iGetGoldState
= 1;
dSetSpriteLinearVelocityPolar("goldHook",g_fEmptyHookSpeed
, g_fHookRotation
);
dAnimateSpritePlayAnimation( "goldMan", "GolderManAnimation1", 0 );
}
OnKeyDown(iKey
, iAltPress
, iShiftPress
, iCtrlPress
);
}
void dOnKeyUp( const int iKey
)
{
OnKeyUp(iKey
);
}
void dOnSpriteColSprite( const char *szSrcName
, const char *szTarName
)
{
if( 2 != g_iPlayState
|| 1 != g_iGetGoldState
){
printf("return\n");
return ;
}
if( stricmp("goldHook",szSrcName
)!=0&&stricmp("goldHook",szTarName
)!=0)
return;;
const char *szGoldName
= NULL;
if( strstr( szSrcName
, "goldBlock" ) )
szGoldName
= szSrcName
;
else if( strstr( szTarName
, "goldBlock" ) )
szGoldName
= szTarName
;
else
return;
dSpriteMountToSpriteLinkPoint( szGoldName
, "goldHook", 2 );
g_iGetGoldState
= 3;
memset(g_szCurGetGold
,'\0',sizeof g_szCurGetGold
);
strcpy( g_szCurGetGold
, szGoldName
);
float fWidth
= dGetSpriteWidth( szGoldName
);
float fSpeed
= ((10.f - fWidth
) / 10.f) * g_fEmptyHookSpeed
;
dSpriteMoveTo( "goldHook", g_fHookStartPosX
, g_fHookStartPosY
, fSpeed
, 1);
dAnimateSpritePlayAnimation( "goldMan", "GolderManAnimation3", 0 );
OnSpriteColSprite(szSrcName
, szTarName
);
}
void dOnSpriteColWorldLimit( const char *szName
, const int iColSide
)
{
if(strcmp("goldhook",szName
)==0){
g_iGetGoldState
=2;
dSpriteMoveTo(szName
,g_fHookStartPosX
,g_fHookStartPosY
,g_fEmptyHookSpeed
,true);
dAnimateSpritePlayAnimation( "goldMan", "GolderManAnimation3", 0 );
}
OnSpriteColWorldLimit(szName
, iColSide
);
}
void DrawHookLine()
{
float fStartX
= dGetSpriteLinkPointPosX( "goldMan", 1 );
float fStartY
= dGetSpriteLinkPointPosY( "goldMan", 1 );
float fEndX
= dGetSpriteLinkPointPosX( "goldHook", 1 );
float fEndY
= dGetSpriteLinkPointPosY( "goldHook", 1 );
dDrawLine( fStartX
, fStartY
, fEndX
, fEndY
, 2.f, 0, 50, 50, 50, 255 );
}
注
文章以提高篇内的教程为基础,仅实现了提高篇教程中所要求的内容,修改完善了其中的bug和内容,并无任何创新。
转载请注明原文地址:https://ipadbbs.8miu.com/read-62258.html