推箱子(C语言小游戏代码)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include
#include
#include
#include
//打印地图
void DrawMap();
//设置颜色
void SetColor(int nColor);
//获取玩家坐标
POINT GetGamerPosition();
//上
void Up();
//下
void Down();
//左
void Left();
//右
void Right();
//获取空箱子个数
int GetSpareBox();
//地图
//0:空地;3箱子的目的地 4 箱子// 6 人7 箱子与目的地的集合//1:墙壁;9人在箱子的目的地
int g_map[2][10][12]=
{
{
{0,0,0,0,1,1,1,0,0,0,0,0 },
{0,0,0,0,1,3,1,0,0,0,0,0 },
{0,0,0,0,1,0,1,1,1,1,1,1 },
{1,1,1,1,1,4,0,4,0,0,3,1 },
{1,3,0,0,0,4,6,1,1,1,1,1 },
{1,1,1,1,1,1,4,1,0,0,0,0 },
{0,0,0,0,0,1,0,1,0,0,0,0 },
{0,0,0,0,0,1,0,1,0,0,0,0 },
{0,0,0,0,0,1,3,1,0,0,0,0 },
{0,0,0,0,0,1,1,1,0,0,0,0 },
},
{
{1,1,1,1,1,0,0,0,0,0,0,0 },
{1,0,0,0,1,0,1,1,1,0,0,0 },
{1,0,4,0,1,0,1,1,1,1,1,1 },
{1,0,4,6,1,0,1,0,0,0,3,1 },
{1,1,1,4,1,1,1,0,0,0,3,1 },
{1,1,1,0,0,0,0,0,0,0,3,1 },
{0,1,0,0,0,1,0,0,0,0,0,1 },
{0,1,0,0,0,1,0,0,0,0,0,1 },
{0,1,0,0,0,1,1,1,1,1,1,1 },
{1,1,1,1,1,1,0,0,0,0,0,0 },
}
};
int g_nCurrentLevel = 0;//当前管卡
int main()
{
//设置窗口大小
system("mode con cols=26 lines=11");
//设置标题
//SetConsoLeTitle("推箱子");
char nInput = 0;//输入字符
while (1)
{
//如果箱子推完了过关
if (0==GetSpareBox())
g_nCurrentLevel++;
//清屏
system("cls");
//如果没有关卡,那就恭喜你成功通关。
if(g_nCurrentLevel>=sizeof(g_map)/sizeof(g_map[0])) {
printf("恭喜你已经通关!");
break;
}
//打印地图
DrawMap();
nInput = _getch();
switch (nInput)
{
//往上
case 'w':case 72:
Up();
break;
case 's':case 80:
Down();
break;
case 'a':case 75:
Left();
break;
case 'd':case 77:
Right();
break;
}
}
system("pause");
return 0;
}
//打印地图
void DrawMap()
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 12; j++)
{
switch (g_map[g_nCurrentLevel][i][j])
{
case 0://道路,空地
printf(" ");
break;
case 1:
SetColor(FOREGROUND_RED | FOREGROUND_INTENSITY);
printf("■");
break;
case 3:
SetColor(FOREGROUND_GREEN | FOREGROUND_INTENSITY);
printf("☆");
break;
case 4:
SetColor(FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
printf("□");
break;
case 6:
SetColor(FOREGROUND_GREEN | FOREGROUND_INTENSITY);
printf("♂");
break;
case 7:
SetColor(FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
printf("★");
break;
case 9:
SetColor(FOREGROUND_GREEN | FOREGROUND_INTENSITY);
printf("♂");
break;
}
}
printf("\n");
}
}
//设置颜色
void SetColor(int nColor)
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
//MSD
SetConsoleTextAttribute(hConsole, nColor);
}
//获取玩家的坐标
POINT GetGamerPosition()
{