funcode综合教程迷你高尔夫

//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
#include "CommonAPI.h"
#include "LessonX.h"
#include
const int GRID_COUNT=12;	// N * N 的矩阵方块,一个N的大小
const int MAX_LEVEL=3;	// 最大关卡数量。如果要增加关卡,请先修改此值
const int RIGID_BLOCK=1;	// 以下3个分别为方块阻挡物、黑洞、出口的值
const int BLACK_HOLE=2;
const int GOLF_EXIT=3;
int g_iGridData[GRID_COUNT][GRID_COUNT];
int	iControlStartX = 0, iControlStartY = 0;
float g_fGridStartX=-27.5f,g_fGridStartY=-27.5f,g_fGridSize=5.f;
int g_iCurLevel=1,g_iRigidBlockCount=0,g_iBlackHoleCount=0,g_iGolfExitCount=0;

int SpritePosXToIndexX ( const float fPosX )
{
    const  float	fLeftSide	=	g_fGridStartX - g_fGridSize / 2.f;
    const  float	fRightSide  =  fLeftSide + g_fGridSize * GRID_COUNT;
    if( fPosX < fLeftSide || fPosX > fRightSide )
        return -1;
    int	iIndexX	=	(int)( (fPosX - fLeftSide) / g_fGridSize );
    return iIndexX;
}

int SpritePosYToIndexY( const float fPosY )
{
    const	float	fTopSide	=	g_fGridStartY - g_fGridSize / 2.f;
    const	float	fBottomSide	=	fTopSide + g_fGridSize * GRID_COUNT;
    if( fPosY < fTopSide || fPosY > fBottomSide )
        return -1;
    int	iIndexY	=	(int)( (fPosY - fTopSide) / g_fGridSize );
    return iIndexY;
}
void MoveSpriteToBlock( const char *szName, const int iIndexX, const int iIndexY )
{
    float	fPosX	=	g_fGridStartX + iIndexX * g_fGridSize;
    float	fPosY	=	g_fGridStartY + iIndexY * g_fGridSize;
    dSetSpritePosition( szName, fPosX, fPosY );
    int	iLoopX = 0, iLoopY = 0;
    for( iLoopY = 0; iLoopY < GRID_COUNT; iLoopY++ )
    {
        for( int iLoopX = 0; iLoopX < GRID_COUNT; iLoopX++ )
        {
            if( 0 == g_iGridData[iLoopY][iLoopX] )
            {
                continue;
            }
            if( RIGID_BLOCK == g_iGridData[iLoopY][iLoopX] )
            {
                szName	=	dMakeSpriteName( "RigidBlock", g_iRigidBlockCount );
                dCloneSprite( "RigidBlockTemplate", szName );
                MoveSpriteToBlock( szName, iLoopX, iLoopY );
                g_iRigidBlockCount++;
            }
            else if( BLACK_HOLE == g_iGridData[iLoopY][iLoopX] )
            {
                //如果是黑洞就建立黑洞
                szName	=	dMakeSpriteName( "BlackHole", g_iBlackHoleCount );
                dCloneSprite( "BlackHoleTemplate", szName );
                MoveSpriteToBlock( szName, iLoopX, iLoopY );
                g_iBlackHoleCount++;
            }
            else if( GOLF_EXIT == g_iGridData[iLoopY][iLoopX] )
            {
                //如果是出口,就出去
                szName	=	dMakeSpriteName( "GolfExit", g_iGolfExitCount );
                dCloneSprite( "GolfExitTemplate", szName );
                MoveSpriteToBlock( szName, iLoopX, iLoopY );
                g_iGolfExitCount++;
            }
            // 将控制的球和指示箭头摆放到出生点
            dSetSpriteLinearVelocity( "ControlBall", 0.f, 0.f );
            MoveSpriteToBlock( "ControlBall", iControlStartX, iControlStartY );
            MoveSpriteToBlock( "GolfArrow", iControlStartX, iControlStartY );
            dSetSpriteVisible( "GolfArrow", 1 );


        }
    }

}

int m_iLevelData1[GRID_COUNT][GRID_COUNT] =
{
    {0, 0, 0, 0,			0,				0,				0,				0,				0, 0, 0, 0},
    {0, 0, 0, 0,			0,				0,				0,				0,				0, 0, 0, 0},
    {0, 0, 0, 0,			0,				0,				0,				0,				0, 0, 0, 0},
    {0, 0, 0, RIGID_BLOCK,	RIGID_BLOCK,	RIGID_BLOCK,	RIGID_BLOCK,	RIGID_BLOCK,	RIGID_BLOCK, 0, 0, 0},
    {0, 0, 0, RIGID_BLOCK,	0,				0,				0,				0,				RIGID_BLOCK, 0, 0, 0},
    {0, 0, 0, RIGID_BLOCK,	0,				0,				0,				0,				RIGID_BLOCK, 0, 0, 0},
    {0, 0, 0, RIGID_BLOCK,	0,				0,				0,				0,				BLACK_HOLE, 0, 0, 0},
    {0, 0, 0, 0,			0,				0,				0,				GOLF_EXIT,		RIGID_BLOCK, 0, 0, 0},
    {0, 0, 0, 0,			0,				0,				0,				0,				0, 0, 0, 0},
    {0, 0, 0, 0,			0,				0,				0,				0,				0, 0, 0, 0},
    {0, 0, 0, 0,			0,				0,				0,				0,				0, 0, 0, 0},
    {0, 0, 0, 0,			0,				0,				0,				0,				0, 0, 0, 0}
};
int m_iLevelData2[GRID_COUNT][GRID_COUNT]=
{
    {0, RIGID_BLOCK, RIGID_BLOCK, RIGID_BLOCK, 0, RIGID_BLOCK, RIGID_BLOCK, RIGID_BLOCK, RIGID_BLOCK, RIGID_BLOCK, RIGID_BLOCK, 0},
    {0, RIGID_BLOCK, 0, 0, 0, 0, 0, 0, 0, 0, RIGID_BLOCK, 0},
    {0, RIGID_BLOCK, 0, 0, 0, 0, 0, 0, 0, 0, RIGID_BLOCK, 0},
    {0, RIGID_BLOCK, 0, 0, 0, 0, 0, 0, 0, 0, RIGID_BLOCK, 0},
    {0, RIGID_BLOCK, 0, 0, 0, 0, 0, 0, 0, 0, RIGID_BLOCK, 0},
    {0, RIGID_BLOCK, 0, 0, 0, 0, 0, 0, 0, 0, RIGID_BLOCK, 0},
    {0, RIGID_BLOCK, 0, 0, 0, RIGID_BLOCK, RIGID_BLOCK, RIGID_BLOCK, 0, 0, RIGID_BLOCK, 0},
    {0, RIGID_BLOCK, 0, 0, 0, 0, 0, RIGID_BLOCK, 0, 0, RIGID_BLOCK, 0},
    {0, RIGID_BLOCK, 0, 0, 0, 0, 0, 0, GOLF_EXIT, RIGID_BLOCK, RIGID_BLOCK, 0},
    {0, RIGID_BLOCK, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, RIGID_BLOCK, 0, 0, 0, 0, 0, 0, 0, 0, RIGID_BLOCK, 0},
    {0, RIGID_BLOCK, 0, RIGID_BLOCK, RIGID_BLOCK, RIGID_BLOCK, RIGID_BLOCK, RIGID_BLOCK, RIGID_BLOCK, 0, RIGID_BLOCK, 0}
};
int m_iLevelData3[GRID_COUNT][GRID_COUNT]=
{
    {0, 0, 0, 0, 0, 0, 0, 0, RIGID_BLOCK, RIGID_BLOCK, 0, 0},
    {0, 0, RIGID_BLOCK, RIGID_BLOCK, RIGID_BLOCK, 0, 0, 0, 0, 0, 0, RIGID_BLOCK},
    {RIGID_BLOCK, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, RIGID_BLOCK},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, RIGID_BLOCK},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, GOLF_EXIT, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, RIGID_BLOCK, RIGID_BLOCK, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, RIGID_BLOCK, 0},
    {0, 0, 0, RIGID_BLOCK, 0, 0, 0, 0, 0, 0, RIGID_BLOCK, 0},
    {0, 0, 0, 0, BLACK_HOLE, RIGID_BLOCK, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
};
int g_iPlayState=1;
int g_iMoveState=0;
void Init()
{
    int	iLoop = 0;
    char *szName = NULL;
    for( iLoop = 0; iLoop < g_iRigidBlockCount; iLoop++ )
    {
        szName	=	dMakeSpriteName( "RigidBlock", iLoop );
        dDeleteSprite( szName );
    }
    for( iLoop = 0; iLoop < g_iBlackHoleCount; iLoop++ )
    {
        szName	=	dMakeSpriteName( "BlackHole", iLoop );
        dDeleteSprite( szName );
    }
    for( iLoop = 0; iLoop < g_iGolfExitCount; iLoop++ )
    {
        szName	=	dMakeSpriteName( "GolfExit", iLoop );
        dDeleteSprite( szName );
    }
    g_iRigidBlockCount	=	0;
    g_iBlackHoleCount	=	0;
    g_iGolfExitCount	=	0;
    g_iMoveState=0;
    switch( g_iCurLevel )
    {
    case 2:
    {
        iControlStartX	=	5;
        iControlStartY	=	9;
        memcpy( g_iGridData, m_iLevelData2, sizeof(int) * GRID_COUNT * 				GRID_COUNT );
    }
    break;
    case 3:
    {
        iControlStartX	=	3;
        iControlStartY	=	6;
        memcpy( g_iGridData, m_iLevelData3, sizeof(int) * GRID_COUNT * 				GRID_COUNT );
    }
    break;
    // 如果要新增关卡,在此处增加case即可
    // case...
    // Level1 或者g_iCurLevel错误
    case 1:
    default:
    {
        iControlStartX	=	5;
        iControlStartY	=	6;
        memcpy( g_iGridData, m_iLevelData1, sizeof(int) * GRID_COUNT * 				GRID_COUNT );
    }
    break;
    };
}

//g_iCurLevel=1,g_iRigidBlockCount=0,g_iBlackHoleCount=0,g_iGolfExitCount=0;

//float g_fGridStartX=-27.5f,g_fGridStartY=-27.5f,g_fGridSize=5.f;
///////////////////////////////////////////////////////////////////////////////////////////
//
// 主函数入口
//
//////////////////////////////////////////////////////////////////////////////////////////
int PASCAL WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR     lpCmdLine,
                   int       nCmdShow)
{
    // 初始化游戏引擎
    if( !dInitGameEngine( hInstance, lpCmdLine ) )
        return 0;
        g_iMoveState=0;
    // To do : 在此使用API更改窗口标题
    dSetWindowTitle("Lesson");
    while( dEngineMainLoop() )
    {
        // 获取两次调用之间的时间差,传递给游戏逻辑处理
        float	fTimeDelta	=	dGetTimeDelta();
        Init();
        if( 0 != g_iMoveState )
        {
            // 先将控制球精灵坐标转换到二维格子数组索引
            float	fPosX	=	dGetSpritePositionX( "ControlBall" );
            float	fPosY	=	dGetSpritePositionY( "ControlBall" );
            int		iIndexX	=	SpritePosXToIndexX( fPosX );
            int		iIndexY	=	SpritePosYToIndexY( fPosY );
            // 控制球已经出了边界,所以不需要再判断
            if( iIndexX < 0 || iIndexX >= GRID_COUNT || iIndexY < 0 || iIndexY >= GRID_COUNT )
            {
                return 0;
            }
            float	fNextPosX	=	fPosX;
            float	fNextPosY	=	fPosY;
            if( 1 == g_iMoveState )
            {
                fNextPosY -= g_fGridSize * 0.5f;
            }
            else if( 2 == g_iMoveState )
            {
                fNextPosY += g_fGridSize * 0.5f;
            }
            else if( 3 == g_iMoveState )
            {
                fNextPosX	-= g_fGridSize * 0.5f;
            }
            else if( 4 == g_iMoveState )
            {
                fNextPosX	+= g_fGridSize * 0.5f;
            }
            if( RIGID_BLOCK == g_iGridData[iIndexY][iIndexX] )
            {
                // 清零移动状态
                g_iMoveState = 0;
                // 速度清零,显示指示箭头
                dSetSpriteLinearVelocity( "ControlBall", 0.f, 0.f );
                dSetSpriteVisible( "GolfArrow", 1 );
                // 把球和指示箭头设置在本方块的中心
                MoveSpriteToBlock( "ControlBall", iIndexX, iIndexY );
                MoveSpriteToBlock( "GolfArrow", iIndexX, iIndexY );
            }
            else if( BLACK_HOLE == g_iGridData[iIndexY][iIndexX] )
            {
                // 将游戏状态设置为1,重新开始关卡
                g_iPlayState	=	1;
            }
            else if( GOLF_EXIT == g_iGridData[iIndexY][iIndexX] )
            {
// 将游戏状态设置为1,开始新关卡
                g_iPlayState	=	1;
// 往下一关卡,如果已经是最大值,则返回第一关
                g_iCurLevel++;
                if( g_iCurLevel > MAX_LEVEL )
                    g_iCurLevel = 1;
            }

        }

        // 执行游戏主循环
        GameMainLoop( fTimeDelta );
    };

    // 关闭游戏引擎
    dShutdownGameEngine();
    return 0;
}

//==========================================================================
//
// 引擎捕捉鼠标移动消息后,将调用到本函数
// 参数 fMouseX, fMouseY:为鼠标当前坐标
//
void dOnMouseMove( const float fMouseX, const float fMouseY )
{
    // 可以在此添加游戏需要的响应函数
    OnMouseMove(fMouseX, fMouseY );
}
//==========================================================================
//
// 引擎捕捉鼠标点击消息后,将调用到本函数
// 参数 iMouseType:鼠标按键值,见 enum MouseTypes 定义
// 参数 fMouseX, fMouseY:为鼠标当前坐标
//
void dOnMouseClick( const int iMouseType, const float fMouseX, const float fMouseY )
{
    // 可以在此添加游戏需要的响应函数
    OnMouseClick(iMouseType, fMouseX, fMouseY);

}
//==========================================================================
//
// 引擎捕捉鼠标弹起消息后,将调用到本函数
// 参数 iMouseType:鼠标按键值,见 enum MouseTypes 定义
// 参数 fMouseX, fMouseY:为鼠标当前坐标
//
void dOnMouseUp( const int iMouseType, const float fMouseX, const float fMouseY )
{
    // 可以在此添加游戏需要的响应函数
    OnMouseUp(iMouseType, fMouseX, fMouseY);

}
//==========================================================================
//
// 引擎捕捉键盘按下消息后,将调用到本函数
// 参数 iKey:被按下的键,值见 enum KeyCodes 宏定义
// 参数 iAltPress, iShiftPress,iCtrlPress:键盘上的功能键Alt,Ctrl,Shift当前是否也处于按下状态(0未按下,1按下)
//
void dOnKeyDown(const int iKey, const int iAltPress, const int iShiftPress, const int iCtrlPress)
{
    // 可以在此添加游戏需要的响应函数
    if( 1 != g_iPlayState || 0 != g_iMoveState ){
        printf("%d %d\n",g_iPlayState,g_iMoveState);
        return ;
    }
    printf(":%d %d\n",g_iPlayState,g_iMoveState);
    float	fPosX	=	dGetSpritePositionX( "ControlBall" );
    float	fPosY	=	dGetSpritePositionY( "ControlBall" );
    int		iIndexX	=	SpritePosXToIndexX( fPosX );
    int		iIndexY	=	SpritePosYToIndexY( fPosY );
    if( iIndexX < 0 || iIndexX >= GRID_COUNT || iIndexY < 0 || iIndexY >= GRID_COUNT )
        return;
    if( KEY_UP == iKey )
    {
        if( iIndexY > 0 && RIGID_BLOCK == g_iGridData[iIndexY - 					1][iIndexX] )
            return;
        // 给予控制球一个方向速度,并设置移动状态、隐藏指示箭头
        g_iMoveState	=	1;
        dSetSpriteLinearVelocityY( "ControlBall", -30.f );
        dSetSpriteVisible( "GolfArrow", 0 );
    }
// TODO 按下方向键,控制球移动:
// 参考上面的if代码,完成下、左、右三个方向的控制代码
    else if( KEY_DOWN == iKey )
    {
        if( iIndexY < GRID_COUNT - 1 && RIGID_BLOCK == g_iGridData[iIndexY + 			1][iIndexX] )
            return;
        // 给予控制球一个方向速度,并设置移动状态、隐藏指示箭头
        g_iMoveState	=	2;
        dSetSpriteLinearVelocityY( "ControlBall", 30.f );
        dSetSpriteVisible( "GolfArrow", 0 );
    }
    else if( KEY_LEFT == iKey )
    {
        if( iIndexX > 0&&RIGID_BLOCK == g_iGridData[iIndexY][iIndexX -1])
            return;
        // 给予控制球一个方向速度,并设置移动状态、隐藏指示箭头
        g_iMoveState	=	3;
        dSetSpriteLinearVelocityX( "ControlBall", -30.f );
        dSetSpriteVisible( "GolfArrow", 0 );
    }
    else if( KEY_RIGHT == iKey )
    {
        if(iIndexX<GRID_COUNT-1&&RIGID_BLOCK == g_iGridData[iIndexY][iIndexX+ 1])
            return;
        // 给予控制球一个方向速度,并设置移动状态、隐藏指示箭头
        g_iMoveState	=	4;
        dSetSpriteLinearVelocityX( "ControlBall", 30.f );
        dSetSpriteVisible( "GolfArrow", 0 );
    }
    OnKeyDown(iKey, iAltPress, iShiftPress, iCtrlPress);
}
//==========================================================================
//
// 引擎捕捉键盘弹起消息后,将调用到本函数
// 参数 iKey:弹起的键,值见 enum KeyCodes 宏定义
//
void dOnKeyUp( const int iKey )
{
    // 可以在此添加游戏需要的响应函数
    OnKeyUp(iKey);
}

//===========================================================================
//
// 引擎捕捉到精灵与精灵碰撞之后,调用此函数
// 精灵之间要产生碰撞,必须在编辑器或者代码里设置精灵发送及接受碰撞
// 参数 szSrcName:发起碰撞的精灵名字
// 参数 szTarName:被碰撞的精灵名字
//
void dOnSpriteColSprite( const char *szSrcName, const char *szTarName )
{
    // 可以在此添加游戏需要的响应函数
    OnSpriteColSprite(szSrcName, szTarName);
}

//===========================================================================
//
// 引擎捕捉到精灵与世界边界碰撞之后,调用此函数.
// 精灵之间要产生碰撞,必须在编辑器或者代码里设置精灵的世界边界限制
// 参数 szName:碰撞到边界的精灵名字
// 参数 iColSide:碰撞到的边界 0 左边,1 右边,2 上边,3 下边
//
void dOnSpriteColWorldLimit( const char *szName, const int iColSide )
{
    // 可以在此添加游戏需要的响应函数
    OnSpriteColWorldLimit(szName, iColSide);
}

你可能感兴趣的:(funcode)