网上有很多视频教程,这个游戏编写难度不大,直接上代码,本来和同学讨论想随机生成每一关地图,后来发现技术达不到。。就只手写了两张地图实现功能
代码注释很详细
#include<stdio.h>
#include<windows.h>
#include<stdlib.h>
#include<conio.h>
//地图
//0:空地 1:墙壁 3:箱子目的地 4:箱子
//6:人 7:箱子和目的地重合 9:人站在目的地
#define MAX_LEN 100
#define MAX_WID 100
int length = 10, width = 12;
int g_nCurrentLevel = 0;//当前关卡
int g_map[10][MAX_LEN][MAX_WID] =
{
{
{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},
{0,1,0,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,1,1,1,1,0,0,0,0,0,1},
{0,0,0,0,0,0,1,1,1,1,1,1},
},
{
{0,1,1,1,1,0,0,0,0,0,0,0},
{0,1,1,1,1,1,1,1,1,0,0,0},
{0,1,0,0,0,0,1,1,1,1,1,1},
{1,1,4,1,1,0,1,0,0,0,3,1},
{1,0,6,0,1,0,0,0,0,0,0,1},
{1,0,0,1,1,1,0,0,0,0,0,1},
{1,0,0,0,4,0,0,4,0,0,0,1},
{1,0,3,3,1,0,4,0,0,0,1,1},
{1,1,3,3,1,0,0,0,0,1,1,1},
{0,1,1,1,1,1,1,1,1,1,1,1},
},
};
//设置颜色
void SetColor(int nColor)
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
//msdn
SetConsoleTextAttribute(hConsole, nColor);
}
//打印地图
void DrapMap()
{
for (int i = 0; i < length; i++)
{
for (int j = 0; j < width; 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_GREEN |FOREGROUND_RED | FOREGROUND_INTENSITY);
printf("□");
break;
case 6://人
SetColor(FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
printf("↑");
break;
case 7://箱子和目的地重合
SetColor(FOREGROUND_GREEN | FOREGROUND_BLUE |FOREGROUND_INTENSITY);
printf("●");
break;
case 9://人站在目的地
SetColor(FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
printf("↑");
break;
}
}
printf("\n");
}
}
//获取人物坐标
POINT GetGamePosition()
{
POINT pos = { -1,-1 };
for (int i = 0; i < length; i++)
{
for (int j = 0; j < width; j++)
{
if (g_map[g_nCurrentLevel][i][j] == 6 || g_map[g_nCurrentLevel][i][j] == 9)
{
pos.x = i;
pos.y = j;
return pos;
}
}
}
}
//人物向上移动
void Up()
{
POINT pos = GetGamePosition();
//1.人的前面面是空地
if (g_map[g_nCurrentLevel][pos.x - 1][pos.y] == 0)
{
//空地改为人
g_map[g_nCurrentLevel][pos.x - 1][pos.y] = 6;
//原来人的位置还原
if (g_map[g_nCurrentLevel][pos.x][pos.y] == 9)
g_map[g_nCurrentLevel][pos.x][pos.y] = 3;
else
g_map[g_nCurrentLevel][pos.x][pos.y] = 0;
}
//2.人的前面面是目的地
if (g_map[g_nCurrentLevel][pos.x - 1][pos.y] == 3)
{
g_map[g_nCurrentLevel][pos.x - 1][pos.y] = 9;
//原来人的位置还原
if (g_map[g_nCurrentLevel][pos.x][pos.y] == 9)
g_map[g_nCurrentLevel][pos.x][pos.y] = 3;
else
g_map[g_nCurrentLevel][pos.x][pos.y] = 0;
}
//3.人的前面面是箱子
if (g_map[g_nCurrentLevel][pos.x - 1][pos.y] == 4)
{
//1)箱子前面是空地
if (g_map[g_nCurrentLevel][pos.x - 2][pos.y] == 0)
{
g_map[g_nCurrentLevel][pos.x - 2][pos.y] = 4;
g_map[g_nCurrentLevel][pos.x - 1][pos.y] = 6;
//原来人的位置还原
if (g_map[g_nCurrentLevel][pos.x][pos.y] == 9)
g_map[g_nCurrentLevel][pos.x][pos.y] = 3;
else
g_map[g_nCurrentLevel][pos.x][pos.y] = 0;
}
//2)箱子前面是目的地
if (g_map[g_nCurrentLevel][pos.x - 2][pos.y] == 3)
{
g_map[g_nCurrentLevel][pos.x - 2][pos.y] = 7;
g_map[g_nCurrentLevel][pos.x - 1][pos.y] = 6;
//原来人的位置还原
if (g_map[g_nCurrentLevel][pos.x][pos.y] == 9)
g_map[g_nCurrentLevel][pos.x][pos.y] = 3;
else
g_map[g_nCurrentLevel][pos.x][pos.y] = 0;
}
}
//4.人的前面箱子和目的地重合
if (g_map[g_nCurrentLevel][pos.x - 1][pos.y] == 7)
{
//1)箱子前面是空地
if (g_map[g_nCurrentLevel][pos.x - 1][pos.y] == 0)
{
g_map[g_nCurrentLevel][pos.x - 2][pos.y] == 4;
g_map[g_nCurrentLevel][pos.x - 1][pos.y] == 9;
//原来人的位置还原
if (g_map[g_nCurrentLevel][pos.x][pos.y] == 9)
g_map[g_nCurrentLevel][pos.x][pos.y] = 3;
else
g_map[g_nCurrentLevel][pos.x][pos.y] = 0;
}
//2)箱子前面是下一个目的地
if (g_map[g_nCurrentLevel][pos.x - 2][pos.y] == 3)
{
g_map[g_nCurrentLevel][pos.x - 2][pos.y] = 7;
g_map[g_nCurrentLevel][pos.x - 1][pos.y] = 9;
//原来人的位置还原
if (g_map[g_nCurrentLevel][pos.x][pos.y] == 9)
g_map[g_nCurrentLevel][pos.x][pos.y] = 3;
else
g_map[g_nCurrentLevel][pos.x][pos.y] = 0;
}
}
}
//人物向下移动
void Down()
{
POINT pos = GetGamePosition();
//1.人的前面面是空地
if (g_map[g_nCurrentLevel][pos.x + 1][pos.y] == 0)
{
//空地改为人
g_map[g_nCurrentLevel][pos.x + 1][pos.y] = 6;
//原来人的位置还原
if (g_map[g_nCurrentLevel][pos.x][pos.y] == 9)
g_map[g_nCurrentLevel][pos.x][pos.y] = 3;
else
g_map[g_nCurrentLevel][pos.x][pos.y] = 0;
}
//2.人的前面面是目的地
if (g_map[g_nCurrentLevel][pos.x + 1][pos.y] == 3)
{
g_map[g_nCurrentLevel][pos.x + 1][pos.y] = 9;
//原来人的位置还原
if (g_map[g_nCurrentLevel][pos.x][pos.y] == 9)
g_map[g_nCurrentLevel][pos.x][pos.y] = 3;
else
g_map[g_nCurrentLevel][pos.x][pos.y] = 0;
}
//3.人的前面面是箱子
if (g_map[g_nCurrentLevel][pos.x + 1][pos.y] == 4)
{
//1)箱子前面是空地
if (g_map[g_nCurrentLevel][pos.x + 2][pos.y] == 0)
{
g_map[g_nCurrentLevel][pos.x + 2][pos.y] = 4;
g_map[g_nCurrentLevel][pos.x + 1][pos.y] = 6;
//原来人的位置还原
if (g_map[g_nCurrentLevel][pos.x][pos.y] == 9)
g_map[g_nCurrentLevel][pos.x][pos.y] = 3;
else
g_map[g_nCurrentLevel][pos.x][pos.y] = 0;
}
//2)箱子前面是目的地
if (g_map[g_nCurrentLevel][pos.x + 2][pos.y] == 3)
{
g_map[g_nCurrentLevel][pos.x + 2][pos.y] = 7;
g_map[g_nCurrentLevel][pos.x + 2][pos.y] = 6;
//原来人的位置还原
if (g_map[g_nCurrentLevel][pos.x][pos.y] == 9)
g_map[g_nCurrentLevel][pos.x][pos.y] = 3;
else
g_map[g_nCurrentLevel][pos.x][pos.y] = 0;
}
}
//4.人的前面箱子和目的地重合
if (g_map[g_nCurrentLevel][pos.x - 1][pos.y] == 7)
{
//1)箱子前面是空地
if (g_map[g_nCurrentLevel][pos.x - 1][pos.y] == 0)
{
g_map[g_nCurrentLevel][pos.x - 2][pos.y] == 4;
g_map[g_nCurrentLevel][pos.x - 1][pos.y] == 9;
//原来人的位置还原
if (g_map[g_nCurrentLevel][pos.x][pos.y] == 9)
g_map[g_nCurrentLevel][pos.x][pos.y] = 3;
else
g_map[g_nCurrentLevel][pos.x][pos.y] = 0;
}
//2)箱子前面是下一个目的地
if (g_map[g_nCurrentLevel][pos.x - 2][pos.y] == 3)
{
g_map[g_nCurrentLevel][pos.x - 2][pos.y] = 7;
g_map[g_nCurrentLevel][pos.x - 1][pos.y] = 9;
//原来人的位置还原
if (g_map[g_nCurrentLevel][pos.x][pos.y] == 9)
g_map[g_nCurrentLevel][pos.x][pos.y] = 3;
else
g_map[g_nCurrentLevel][pos.x][pos.y] = 0;
}
}
}
//人物向左移动
void Left()
{
POINT pos = GetGamePosition();
//1.人的前面面是空地
if (g_map[g_nCurrentLevel][pos.x][pos.y - 1] == 0)
{
//空地改为人
g_map[g_nCurrentLevel][pos.x][pos.y - 1] = 6;
//原来人的位置还原
if (g_map[g_nCurrentLevel][pos.x][pos.y] == 9)
g_map[g_nCurrentLevel][pos.x][pos.y] = 3;
else
g_map[g_nCurrentLevel][pos.x][pos.y] = 0;
}
//2.人的前面面是目的地
if (g_map[g_nCurrentLevel][pos.x][pos.y - 1] == 3)
{
g_map[g_nCurrentLevel][pos.x][pos.y - 1] = 9;
//原来人的位置还原
if (g_map[g_nCurrentLevel][pos.x][pos.y] == 9)
g_map[g_nCurrentLevel][pos.x][pos.y] = 3;
else
g_map[g_nCurrentLevel][pos.x][pos.y] = 0;
}
//3.人的前面面是箱子
if (g_map[g_nCurrentLevel][pos.x][pos.y - 1] == 4)
{
//1)箱子前面是空地
if (g_map[g_nCurrentLevel][pos.x][pos.y - 2] == 0)
{
g_map[g_nCurrentLevel][pos.x][pos.y - 2] = 4;
g_map[g_nCurrentLevel][pos.x][pos.y - 1] = 6;
//原来人的位置还原
if (g_map[g_nCurrentLevel][pos.x][pos.y] == 9)
g_map[g_nCurrentLevel][pos.x][pos.y] = 3;
else
g_map[g_nCurrentLevel][pos.x][pos.y] = 0;
}
//2)箱子前面是目的地
if (g_map[g_nCurrentLevel][pos.x][pos.y - 2] == 3)
{
g_map[g_nCurrentLevel][pos.x][pos.y - 2] = 7;
g_map[g_nCurrentLevel][pos.x][pos.y - 1] = 6;
//原来人的位置还原
if (g_map[g_nCurrentLevel][pos.x][pos.y] == 9)
g_map[g_nCurrentLevel][pos.x][pos.y] = 3;
else
g_map[g_nCurrentLevel][pos.x][pos.y] = 0;
}
}
//4.人的前面箱子和目的地重合
if (g_map[g_nCurrentLevel][pos.x][pos.y - 1] == 7)
{
//1)箱子前面是空地
if (g_map[g_nCurrentLevel][pos.x][pos.y - 1] == 0)
{
g_map[g_nCurrentLevel][pos.x][pos.y - 2] == 4;
g_map[g_nCurrentLevel][pos.x][pos.y - 1] == 9;
//原来人的位置还原
if (g_map[g_nCurrentLevel][pos.x][pos.y] == 9)
g_map[g_nCurrentLevel][pos.x][pos.y] = 3;
else
g_map[g_nCurrentLevel][pos.x][pos.y] = 0;
}
//2)箱子前面是下一个目的地
if (g_map[g_nCurrentLevel][pos.x][pos.y - 2] == 3)
{
g_map[g_nCurrentLevel][pos.x][pos.y - 2] = 7;
g_map[g_nCurrentLevel][pos.x][pos.y - 1] = 9;
//原来人的位置还原
if (g_map[g_nCurrentLevel][pos.x][pos.y] == 9)
g_map[g_nCurrentLevel][pos.x][pos.y] = 3;
else
g_map[g_nCurrentLevel][pos.x][pos.y] = 0;
}
}
}
//人物向右移动
void Right()
{
POINT pos = GetGamePosition();
//1.人的前面面是空地
if (g_map[g_nCurrentLevel][pos.x][pos.y + 1] == 0)
{
//空地改为人
g_map[g_nCurrentLevel][pos.x][pos.y + 1] = 6;
//原来人的位置还原
if (g_map[g_nCurrentLevel][pos.x][pos.y] == 9)
g_map[g_nCurrentLevel][pos.x][pos.y] = 3;
else
g_map[g_nCurrentLevel][pos.x][pos.y] = 0;
}
//2.人的前面面是目的地
if (g_map[g_nCurrentLevel][pos.x][pos.y + 1] == 3)
{
g_map[g_nCurrentLevel][pos.x][pos.y + 1] = 9;
//原来人的位置还原
if (g_map[g_nCurrentLevel][pos.x][pos.y] == 9)
g_map[g_nCurrentLevel][pos.x][pos.y] = 3;
else
g_map[g_nCurrentLevel][pos.x][pos.y] = 0;
}
//3.人的前面面是箱子
if (g_map[g_nCurrentLevel][pos.x][pos.y + 1] == 4)
{
//1)箱子前面是空地
if (g_map[g_nCurrentLevel][pos.x][pos.y + 2] == 0)
{
g_map[g_nCurrentLevel][pos.x][pos.y + 2] = 4;
g_map[g_nCurrentLevel][pos.x][pos.y + 1] = 6;
//原来人的位置还原
if (g_map[g_nCurrentLevel][pos.x][pos.y] == 9)
g_map[g_nCurrentLevel][pos.x][pos.y] = 3;
else
g_map[g_nCurrentLevel][pos.x][pos.y] = 0;
}
//2)箱子前面是目的地
if (g_map[g_nCurrentLevel][pos.x][pos.y + 2] == 3)
{
g_map[g_nCurrentLevel][pos.x][pos.y + 2] = 7;
g_map[g_nCurrentLevel][pos.x][pos.y + 1] = 6;
//原来人的位置还原
if (g_map[g_nCurrentLevel][pos.x][pos.y] == 9)
g_map[g_nCurrentLevel][pos.x][pos.y] = 3;
else
g_map[g_nCurrentLevel][pos.x][pos.y] = 0;
}
}
//4.人的前面箱子和目的地重合
if (g_map[g_nCurrentLevel][pos.x][pos.y - 1] == 7)
{
//1)箱子前面是空地
if (g_map[g_nCurrentLevel][pos.x][pos.y - 1] == 0)
{
g_map[g_nCurrentLevel][pos.x][pos.y - 2] == 4;
g_map[g_nCurrentLevel][pos.x][pos.y - 1] == 9;
//原来人的位置还原
if (g_map[g_nCurrentLevel][pos.x][pos.y] == 9)
g_map[g_nCurrentLevel][pos.x][pos.y] = 3;
else
g_map[g_nCurrentLevel][pos.x][pos.y] = 0;
}
//2)箱子前面是下一个目的地
if (g_map[g_nCurrentLevel][pos.x][pos.y - 2] == 3)
{
g_map[g_nCurrentLevel][pos.x][pos.y - 2] = 7;
g_map[g_nCurrentLevel][pos.x][pos.y - 1] = 9;
//原来人的位置还原
if (g_map[g_nCurrentLevel][pos.x][pos.y] == 9)
g_map[g_nCurrentLevel][pos.x][pos.y] = 3;
else
g_map[g_nCurrentLevel][pos.x][pos.y] = 0;
}
}
}
//计算空余箱子个数
int GetSpareBox()
{
int nCount = 0;
for (int i = 0; i < length; i++)
{
for (int j = 0; j < width; j++)
{
if (g_map[g_nCurrentLevel][i][j] == 4)
nCount++;
}
}
return nCount;
}
//主函数
int main()
{
//设置标题
SetConsoleTitle("推箱子");
//设置窗口大小
system("mode con cols=26 line width");
while (1)
{
//获取剩余箱子个数
if (GetSpareBox() == 0)
{
g_nCurrentLevel++;
}
//清屏
system("cls");
//打印地图
DrapMap();
char ch = _getch();
switch (ch)
{
case 'w':case 72://往上
Up();
break;
case 's':case 80://往下
Down();
break;
case 'a':case 75://往左
Left();
break;
case 'd':case 77://往右
Right();
break;
}
}
return 0;
}