Funcode设计
前言
基于Funcode设计的C语言游戏。
提示:以下是本篇文章正文内容,下面案例可供参考
一、Funcode下载地址
Win10版本 提取码:9761
二、C语言课程设计–迷你高尔夫
1.效果图
2.部分代码
代码如下(示例):
#include "CommonAPI.h"
#include "LessonX.h"
int g_MiGameState = 0;
void dDeleteSpriteGameInit();
void dCreatGameInit();
void MoveSpriteToBlock( const char *szName, const int iIndexX, const int iIndexY );
void dCreatInitSpirt();
int SpritePosXToIndexX ( const float fPosX );
int SpritePosYToIndexY( const float fPosY );
#define GRID_COUNT 12
#define MAX_LEVEL 3
#define RIGID_BLOCK 1
#define BLACK_HOLE 2
#define GOLF_EXIT 3
int g_iMoveState = 0;
int g_iCurLevel = 1;
const float g_fGridStartX = -27.5f;
const float g_fGridStartY = -27.5f;
const float g_fGridSize = 5.f;
int g_iRigidBlockCount = 0;
int g_iBlackHoleCount = 0;
int g_iGolfExitCount = 0;
int g_iGridData[GRID_COUNT][GRID_COUNT];
const 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}
};
const 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}
};
const 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 PASCAL WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
if( !dInitGameEngine( hInstance, lpCmdLine ) )
return 0;
g_MiGameState=1;
g_iMoveState=0;
dDeleteSpriteGameInit();
dCreatGameInit();
dSetSpriteLinearVelocity( "ControlBall", 0.f, 0.f );
dSetSpriteVisible( "GolfArrow", 1 );
dCreatInitSpirt();
dSetWindowTitle("Lesson");
while( dEngineMainLoop() )
{
float fTimeDelta = dGetTimeDelta();
g_MiGameState=2;
GameMainLoop( fTimeDelta );
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;
}
int iNextIndexX = SpritePosXToIndexX( fNextPosX );
int iNextIndexY = SpritePosYToIndexY( fNextPosY );
if( iNextIndexX < 0 || iNextIndexX >= GRID_COUNT || iNextIndexY < 0 || iNextIndexY >= GRID_COUNT )
return 0;
if( RIGID_BLOCK == g_iGridData[iNextIndexY][iNextIndexX] )
{
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[iNextIndexY][iNextIndexX] )
{
g_MiGameState = 1;
}
else if( GOLF_EXIT == g_iGridData[iNextIndexY][iNextIndexX] )
{
g_MiGameState = 1;
g_iCurLevel++;
if( g_iCurLevel > MAX_LEVEL )
g_iCurLevel = 1;
}
}
};
dShutdownGameEngine();
return 0;
}
总结
难点是通过二维数组,进行存储当前关卡N*N的矩阵方块信息,其他的地图加载,以及方向移动,通过相应函数编写即可。 完整代码及游戏资源
|