05 C语言课程设计_迷你高尔夫

发布时间 : 星期日 文章05 C语言课程设计_迷你高尔夫更新完毕开始阅读

{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 CGameMain::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 CGameMain::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} };

二维数组中0表示该位置不创建精灵,否则根据不同的值创建不同精灵,RIGID_BLOCK(值为1)表示创建一个方块精灵,BLACK_HOLE(值为2)表示创建一个黑洞精灵,GOLF_EXIT(值为3)表示创建一个出口精灵。

2)在WinMain初始化部分,将球的运动状态初始化为静止,添加下面代码:

g_iMoveState=0;

3、 进入GameInit函数,使用3个循环,分别将上一关卡创建的3种精灵删除掉,添

加如下代码:

int iLoop = 0;

Char *szName = NULL;

for( iLoop = 0; iLoop < g_iRigidBlockCount; iLoop++ ) { szName = dMakeSpriteName( \ dDeleteSprite( szName ); } for( iLoop = 0; iLoop < g_iBlackHoleCount; iLoop++ ) { szName = dMakeSpriteName( \ dDeleteSprite( szName ); } for( iLoop = 0; iLoop < g_iGolfExitCount; iLoop++ ) { szName = dMakeSpriteName( \ dDeleteSprite( szName ); }

// TODO 使用3个循环,分别将上一关卡创建的3种精灵删除掉: 三种精灵数量分别为g_iRigidBlockCount, g_iBlackHoleCount, g_iGolfExitCount 三种精灵的名字前缀分别为:RigidBlock, BlackHole, GolfExit举例,第一种精灵,使用 dMakeSpriteName( \iLoop )获得名字,然后使用dDeleteSprite将其删除之

4、 根据当前关卡,选择关卡的数据,即将代表关卡的二维数组中的数据拷贝到

g_iGridData中,同时设置控制球在每个数组中的起始位置。代码如下:

// 总数置0,重新创建

g_iRigidBlockCount = 0; g_iBlackHoleCount = 0; g_iGolfExitCount = 0;

// 控制球在数组中的开始位置(出生点),该位置不能为0.根据关卡数据自行指定 int iControlStartX = 0, iControlStartY = 0;

5、 选择关卡我们使用了switch-case结构,程序通过判断switch中的参数进入到不

同的case中去,每个case就是一种情况的实现。代码如下: // 根据当前关卡,选择关卡数据 switch( g_iCurLevel ) {

case 2: {

iControlStartX = 5; iControlStartY = 9;

memcpy( g_iGridData, g_iLevelData2, sizeof(int) * GRID_COUNT * GRID_COUNT ); }

break; case 3: {

iControlStartX = 3; iControlStartY = 6;

memcpy( g_iGridData, g_iLevelData3, sizeof(int) * GRID_COUNT * GRID_COUNT ); }

break;

// 如果要新增关卡,在此处增加case即可 // case...

// Level1 或者g_iCurLevel错误 case 1: default: {

iControlStartX = 5; iControlStartY = 6;

memcpy( g_iGridData, g_iLevelData1, sizeof(int) * GRID_COUNT * GRID_COUNT ); }

break; };

6、 memcpy函数作用是从源src所指的内存地址的起始位置开始拷贝n个字节到目标

dest所指的内存地址的起始位置中。因为二维数组在内存中的存放方式是连续的,因此我们将源地址拷贝给m_iGridData的起始地址之后,系统后自动根据m_iGridData的下标来找到正确的值。

至此,本实验结束。

实验三 游戏精灵初始化

【实验内容】 步骤一、创建精灵

步骤二、初始化精灵位置 【实验思路】

TODO 遍历二维数组g_iGridData,根据数组值生成对应的精灵实例: 值为0的时候不用创建,需要创建的精灵名字前缀为(按照宏定义的1,2,3顺序):RigidBlock, BlackHole, GolfExit,每创建一种精灵,请将其总数加1 :g_iRigidBlockCount, g_iBlackHoleCount,g_iGolfExitCount,举例,第一种精灵,使用 dMakeSpriteName( \g_iRigidBlockCount )获得名字,然后使用dCloneSprite创建,并将 g_iRigidBlockCount加1,最后,使用MoveSpriteToBlock函数将精灵摆放到对应位置上 【实验指导】

1、 创建精灵之后需要将精灵移到特定位置,因此我们需要定义一个自定义的函数

MoveSpriteToBlock来实现这个功能。 1) 我们首先在Main.cpp中声明该函数:

void MoveSpriteToBlock( const char *szName, const int iIndexX, const int iIndexY );

2) 然后定义该函数:

void MoveSpriteToBlock( const char *szName, const int iIndexX, const int iIndexY ) { }

3) 传入该函数的是精灵实体以及x,y坐标参数。再通过SetSpritePosition函数

设置精灵位置,在该函数里面添加如下代码:

float fPosX = g_fGridStartX + iIndexX * g_fGridSize; float fPosY = g_fGridStartY + iIndexY * g_fGridSize; dSetSpritePosition( szName, fPosX, fPosY );

4) 变量声明:

int iLoopX = 0, iLoopY = 0; 5) 实现两个for循环:

for( iLoopY = 0; iLoopY < GRID_COUNT; iLoopY++ ) {

for( int iLoopX = 0; iLoopX < GRID_COUNT; iLoopX++ ) {

} }

1) 在里面的循环添加判断代码:

如果是0,则不创建精灵,continue表示跳出本次循环,继续下一个循环。 if( 0 == g_iGridData[iLoopY][iLoopX] ) continue;

如果是方块,则创建方块精灵:

联系合同范文客服:xxxxx#qq.com(#替换为@)