C语言小项目----> 推箱子 二维指针之前知识实现,需要的可以看看。注释已经附上,文章不再赘述代码思路。
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define SPACE 0
#define WALL 1
#define DEST 2
#define BOX 3
#define PLAYER 4
int level = 0;
int map[3][10][10] =
{
{
0,0,0,0,0,0,0,0,0,0,
0,0,0,1,1,1,0,0,0,0,
0,0,0,1,2,1,0,0,0,0,
0,0,0,1,0,1,0,0,0,0,
0,1,1,1,3,1,1,1,1,0,
0,1,2,3,4,0,3,2,1,0,
0,1,1,1,1,3,1,1,1,0,
0,0,0,0,1,2,1,0,0,0,
0,0,0,0,1,1,1,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,1,1,0,0,1,1,0,0,
0,1,0,0,1,1,0,0,1,0,
1,0,2,0,4,0,0,2,0,1,
1,0,0,3,0,0,3,0,0,1,
0,1,0,3,0,0,3,0,1,0,
0,0,1,0,0,0,0,1,0,0,
0,0,0,1,2,2,1,0,0,0,
0,0,0,0,1,1,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,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,0,0,
0,0,0,0,0,0,0,0,0,0
}
};
void draw()
{
for (int i = 0; i < 10; i++)
{
for (int k = 0; k < 10; k++)
{
switch (map[level][i][k])
{
case SPACE:
printf(" ");
break;
case WALL:
printf("▓");
break;
case DEST:
printf("☆");
break;
case BOX:
printf("★");
break;
case PLAYER:
printf("♀");
break;
case BOX+DEST:
printf("卐");
break;
case PLAYER + DEST:
printf("♂");
break;
}
}
printf("\n");
}
}
void move()
{
int i = 0, k = 0;
for (i = 0; i < 10; i++)
{
for (k = 0; k < 10; k++)
{
if (map[level][i][k] == PLAYER || map[level][i][k] == PLAYER+DEST)
{
goto end;
}
}
}
end:;
char key = _getch();
switch (key)
{
case 'W':
case 'w':
case 72:
if (map[level][i - 1][k] == SPACE || map[level][i - 1][k] == DEST)
{
map[level][i - 1][k] += PLAYER;
map[level][i][k] -= PLAYER;
}
else if (map[level][i - 1][k] == BOX || map[level][i - 1][k] == BOX + DEST)
{
if (map[level][i - 2][k] == SPACE || map[level][i - 2][k] == DEST)
{
map[level][i - 2][k] += BOX;
map[level][i - 1][k] -= BOX;
map[level][i - 1][k] += PLAYER;
map[level][i][k] -= PLAYER;
}
}
break;
case 'S':
case 's':
case 80:
if (map[level][i+1][k] == SPACE || map[level][i+1][k] == DEST)
{
map[level][i+1][k] += PLAYER;
map[level][i][k] -= PLAYER;
}
else if (map[level][i+1][k] == BOX || map[level][i+1][k] == BOX + DEST)
{
if (map[level][i+2][k] == SPACE || map[level][i+2][k] == DEST)
{
map[level][i+2][k] += BOX;
map[level][i+1][k] -= BOX;
map[level][i+1][k] += PLAYER;
map[level][i][k] -= PLAYER;
}
}
break;
case 'A':
case 'a':
case 75:
if (map[level][i][k - 1] == SPACE || map[level][i][k - 1] == DEST)
{
map[level][i][k - 1] += PLAYER;
map[level][i][k] -= PLAYER;
}
else if (map[level][i][k - 1] == BOX || map[level][i][k - 1] == BOX + DEST)
{
if (map[level][i][k - 2] == SPACE || map[level][i][k - 2] == DEST)
{
map[level][i][k - 2] += BOX;
map[level][i][k - 1] -= BOX;
map[level][i][k - 1] += PLAYER;
map[level][i][k] -= PLAYER;
}
}
break;
case 'D':
case 'd':
case 77:
if (map[level][i][k + 1] == SPACE || map[level][i][k + 1] == DEST)
{
map[level][i][k + 1] += PLAYER;
map[level][i][k] -= PLAYER;
}
else if(map[level][i][k+1] == BOX || map[level][i][k + 1] == BOX+DEST)
{
if (map[level][i][k + 2] == SPACE || map[level][i][k + 2] == DEST)
{
map[level][i][k + 2] += BOX;
map[level][i][k + 1] -= BOX;
map[level][i][k + 1] += PLAYER;
map[level][i][k] -= PLAYER;
}
}
break;
}
}
int jude()
{
for (int i = 0; i < 10; i++)
{
for (int k = 0; k < 10; k++)
{
if (map[level][i][k] == BOX)
{
return 0;
}
}
}
return 1;
}
int main()
{
while (1)
{
system("cls");
draw();
if (jude())
{
level++;
if (level >= 3)
{
printf("you win~");
return 666;
}
}
move();
}
getchar();
return 0;
}
|