int cas = 0;
int map[3][8][8] =
{
1,1,1,1,1,1,1,1,
1,3,4,0,0,4,3,1,
1,0,1,3,0,1,0,1,
1,0,1,4,0,1,0,1,
1,0,0,5,0,0,0,1,
1,0,1,0,0,1,0,1,
1,3,4,0,0,4,3,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
1,3,4,0,0,4,3,1,
1,0,1,3,0,1,0,1,
1,0,1,4,0,1,0,1,
1,3,4,5,0,0,0,1,
1,0,1,0,0,1,0,1,
1,3,4,0,0,4,3,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
1,3,4,0,0,4,3,1,
1,0,1,3,0,1,0,1,
1,0,1,4,0,1,0,1,
1,3,4,5,0,4,3,1,
1,0,1,0,0,1,0,1,
1,3,4,0,0,4,3,1,
1,1,1,1,1,1,1,1
};
void drawGraph()
{
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
//算贴图的坐标
int x = 50 * j;
int y = 50 * i;
switch (map[cas][i][j])
{
case 0:
//一个汉字符号占用两个位置
//printf(" ");
putimage(x, y, img + 0);
break;
case 1:
putimage(x, y, img + 1);
//printf("■");
break;
case 3:
putimage(x, y, img + 2);
//printf("☆");
break;
case 4:
putimage(x, y, img + 3);
//printf("★");
break;
case 5:
case 8:
putimage(x, y, img + 4);
//printf("人");
break;
case 7:
putimage(x, y, img + 5);
//printf("●");
break;
}
}
//printf("\n");
}
}
void keyDown()
{
int userKey = _getch(); //不可见输入
//定位:找到人的位置
int i = 0;
int j = 0;
for (i = 1; i < 8; i++)
{
for (j = 1; j < 8; j++)
{
if (map[cas][i][j] == 5 || map[cas][i][j] == 8)
{
goto NEXT;
}
}
}
NEXT:
//我们这个游戏用什么按键去玩
switch (userKey)
{
case 'W':
case 'w':
case 72:
if (map[cas][i - 1][j] == 0 || map[cas][i - 1][j] == 3)
{
map[cas][i][j] -= 5;
map[cas][i - 1][j] += 5;
}
if (map[cas][i - 1][j] == 4 || map[cas][i - 1][j] == 7)
{
if (map[cas][i - 2][j] == 0 || map[cas][i - 2][j] == 3)
{
map[cas][i][j] -= 5;
map[cas][i - 1][j] += 1;
map[cas][i - 2][j] += 4;
}
}
break;
case 's':
case 'S':
case 80:
if (map[cas][i + 1][j] == 0 || map[cas][i + 1][j] == 3)
{
map[cas][i][j] -= 5;
map[cas][i + 1][j] += 5;
}
if (map[cas][i + 1][j] == 4 || map[cas][i + 1][j] == 7)
{
if (map[cas][i + 2][j] == 0 || map[cas][i +2][j] == 3)
{
map[cas][i][j] -= 5;
map[cas][i + 1][j] += 1;
map[cas][i + 2][j] += 4;
}
}
break;
case 'a':
case 'A':
case 75:
if (map[cas][i][j - 1] == 0 || map[cas][i][j - 1] == 3)
{
//a+=1 a=a+1 复合赋值运算符
map[cas][i][j] -= 5;
map[cas][i][j - 1] += 5;
}
if (map[cas][i][j - 1] == 4 || map[cas][i][j - 1] == 7)
{
if (map[cas][i][j - 2] == 0 || map[cas][i][j - 2] == 3)
{
map[cas][i][j] -= 5;
map[cas][i][j - 1] += 1;
map[cas][i][j - 2] += 4;
}
}
break;
case 'd':
case 'D':
case 77:
if (map[cas][i][j + 1] == 0 || map[cas][i][j + 1] == 3)
{
map[cas][i][j] -= 5;
map[cas][i][j + 1] += 5;
}
if (map[cas][i][j + 1] == 4 || map[cas][i][j + 1] == 7)
{
if (map[cas][i][j + 2] == 0 || map[cas][i][j + 2] == 3)
{
map[cas][i][j] -= 5;
map[cas][i][j + 1] += 1;
map[cas][i][j + 2] += 4;
}
}
break;
}
}
//胜负的判断:
int gameOver()
{
//地图上没有箱子就可以结束
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
if (map[cas][i][j] == 4)
{
return 0;
}
}
}
return 1;
}
int main()
{
loadResource();
mciSendString("open 1.mp3", 0, 0, 0);
mciSendString("play 1.mp3 repeat", 0, 0, 0);
initgraph(50 * 8, 50 * 8);
while (1)
{
drawGraph();
if (gameOver())
{
cas++; //变换关卡
if(cas==3)
break;
}
keyDown();
//system("cls");
}
closegraph();
//printf("GameOver!\n");
return 0;
}