Funcode设计
文章目录
- Funcode设计
- 前言
- 一、Funcode下载地址
- 二、C语言课程设计--迷你高尔夫
- 1.效果图
- 2.部分代码
- 总结
前言
基于Funcode设计的C语言游戏。
提示:以下是本篇文章正文内容,下面案例可供参考
一、Funcode下载地址
Win10版本 提取码:9761
二、C语言课程设计–迷你高尔夫
1.效果图
2.部分代码
代码如下(示例):
#include "CommonAPI.h"
#include "LessonX.h"
int g_MiGameState = 0; // 游戏状态,0 -- 游戏结束等待开始状态;1 -- 按下空格键开始,初始化游戏;2 -- 游戏进行中
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 // N * N 的矩阵方块,一个N的大小
#define MAX_LEVEL 3 // 最大关卡数量。如果要增加关卡,请先修改此值
#define RIGID_BLOCK 1 // 方块阻挡物、
#define BLACK_HOLE 2 //黑洞
#define GOLF_EXIT 3 //出口的值
// 控制球的移动状态:0当前静止,可以移动,1、2、3、4:代表上下左右4个 // 方向移动中,按键无响应
int g_iMoveState = 0;
int g_iCurLevel = 1; // 当前关卡
// 第一块的起始坐标 =-(GRID_COUNT * g_fGridSize * 0.5 - g_fGridSize / 2)
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; // 本关卡创建的出口的数量
// 二维数组,存储当前关卡N*N的矩阵方块信息
int g_iGridData[GRID_COUNT][GRID_COUNT];
//二维数组中0表示该位置不创建精灵,否则根据不同的值创建不同精灵,
//RIGID_BLOCK(值为1)表示创建一个方块精灵,
//BLACK_HOLE(值为2)表示创建一个黑洞精灵,
//GOLF_EXIT(值为3)表示创建一个出口精灵。
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();
// To do : 在此使用API更改窗口标题
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] )
{
// 将游戏状态设置为1,重新开始关卡
g_MiGameState = 1;
}
//是出口
else if( GOLF_EXIT == g_iGridData[iNextIndexY][iNextIndexX] )
{
// 将游戏状态设置为1,开始新关卡
g_MiGameState = 1;
// 往下一关卡,如果已经是最大值,则返回第一关
g_iCurLevel++;
if( g_iCurLevel > MAX_LEVEL )
g_iCurLevel = 1;
}
}
};
// 关闭游戏引擎
dShutdownGameEngine();
return 0;
}
总结
难点是通过二维数组,进行存储当前关卡N*N的矩阵方块信息,其他的地图加载,以及方向移动,通过相应函数编写即可。完整代码及游戏资源