时间:2022-11-30 10:58:52 | 栏目:C代码 | 点击:次
本文实例为大家分享了C语言系列之推箱子游戏的具体代码,供大家参考,具体内容如下
输入WSAD控制行走,只需要把一个方向的代码写好了,剩下的是三个方向就是复制粘贴和简单的修改就可以了。
#include <stdio.h> #include <conio.h> //键盘输入函数库 //0:空地 1:墙 2:箱子要放的位置 5:人与星号重叠5:箱子与星号重叠 3:人+-3 4:箱子:+-4 int map[10][10] = {1,1,1,1,1,1,1,1,1,1, 1,0,0,0,0,0,2,0,0,1, 1,0,0,3,4,0,0,0,0,1, 1,0,0,1,1,0,0,0,0,1, 1,0,1,0,0,0,1,0,0,1, 1,0,0,4,0,0,1,0,0,1, 1,0,0,0,0,0,0,0,0,1, 1,0,2,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,0,0,1, 1,1,1,1,1,1,1,1,1,1, }; int flag=0; void PrintMap() { int i, j; for (i = 0; i < 10; i++) { for (j = 0; j < 10; j++) { switch (map[i][j]) { //0:空地 1:墙 2:箱子要放的位置 3:人 4:箱子 5:箱子到达目的地 case 0: printf(" "); break; case 1: printf("■"); break; case 2: printf("☆"); break; case 3: printf("♀"); break; case 4: printf("□"); break; case 5: printf("★"); break; default: break; } } printf("\n"); } } void PlayGame() { int num = 0; char temp; temp = getch();//键盘输入无回显函数 static int x=2,y=3; switch (temp) { case 'W': if(map[x-1][y] == 0)//如果下一个的地方为空地 { if(flag == 0) { map[x-1][y] = 3; map[x][y] = 0; x--; } else { map[x-1][y] = 3; map[x][y] = 2; x--; flag = 0; } } else if (map[x-1][y] == 2)//如果下一个的地方为箱子存放地 { map[x-1][y] = 3; map[x][y] = 0; x--; if(map[x-1][y] == 0) { flag = 1; } } else if(map[x-1][y] == 4)//下一个去的地方有箱子 { if(map[x-2][y] == 0 )//x-2是空地时 { if(flag == 0) { map[x-2][y] = 4; map[x-1][y] = 3; map[x][y] = 0; x--; } else { map[x-2][y] = 4; map[x-1][y] = 3; map[x][y] = 2; x--; flag = 0; } } else if(map[x-2][y] == 2)//x-2是箱子的存放地 { map[x-2][y] = 5; map[x-1][y] = 3; map[x][y] = 0; x--; } } else if (map[x-1][y] == 5)//下一个去的地方是已经在存放点的箱子 { if(map[x-2][y] != 1) { map[x-2][y] = 4; map[x-1][y] = 3; map[x][y] = 0; x--; if(map[x-2][y] != 1) { flag = 1; } } } break; case 'S': if(map[x+1][y] == 0)//如果下一个的地方为空地 { if(flag == 0) { map[x+1][y] = 3; map[x][y] = 0; x++; } else { map[x+1][y] = 3; map[x][y] = 2; x++; flag = 0; } } else if (map[x+1][y] == 2)//如果下一个的地方为箱子存放地 { map[x+1][y] = 3; map[x][y] = 0; x++; if(map[x+1][y] == 0) { flag = 1; } } else if(map[x+1][y] == 4)//下一个去的地方有箱子 { if(map[x+2][y] == 0 )//x-2是空地时 { if(flag == 0) { map[x+2][y] = 4; map[x+1][y] = 3; map[x][y] = 0; x++; } else { map[x+2][y] = 4; map[x+1][y] = 3; map[x][y] = 2; x++; flag = 0; } } else if(map[x+2][y] == 2)//x-2是箱子的存放地 { map[x+2][y] = 5; map[x+1][y] = 3; map[x][y] = 0; x++; } } else if (map[x+1][y] == 5)//下一个去的地方是已经在存放点的箱子 { if(map[x+2][y] != 1) { map[x+2][y] = 4; map[x+1][y] = 3; map[x][y] = 0; x++; if(map[x+2][y] != 1) { flag = 1; } } } break; case 'A': if(map[x][y-1] == 0)//如果下一个的地方为空地 { if(flag == 0) { map[x][y-1] = 3; map[x][y] = 0; y--; } else { map[x][y-1] = 3; map[x][y] = 2; y--; flag = 0; } } else if (map[x][y-1] == 2)//如果下一个的地方为箱子存放地 { map[x][y-1] = 3; map[x][y] = 0; y--; if(map[x][y-1] == 0) { flag = 1; } } else if(map[x][y-1] == 4)//下一个去的地方有箱子 { if(map[x][y-2] == 0 )//x-2是空地时 { if(flag == 0) { map[x][y-2] = 4; map[x][y-1] = 3; map[x][y] = 0; y--; } else { map[x][y-2] = 4; map[x][y-1] = 3; map[x][y] = 2; y--; flag = 0; } } else if(map[x][y-2] == 2)//x-2是箱子的存放地 { map[x][y-2] = 5; map[x][y-1] = 3; map[x][y] = 0; y--; } } else if (map[x][y-1] == 5)//下一个去的地方是已经在存放点的箱子 { if(map[x][y-2] != 1) { map[x][y-2] = 4; map[x][y-1] = 3; map[x][y] = 0; y--; if(map[x][y-2] != 1) { flag = 1; } } } break; case 'D': if(map[x][y+1] == 0)//如果下一个的地方为空地 { if(flag == 0) { map[x][y+1] = 3; map[x][y] = 0; y++; } else { map[x][y+1] = 3; map[x][y] = 2; y++; flag = 0; } } else if (map[x][y+1] == 2)//如果下一个的地方为箱子存放地 { map[x][y+1] = 3; map[x][y] = 0; y++; if(map[x][y+1] == 0) { flag = 1; } } else if(map[x][y+1] == 4)//下一个去的地方有箱子 { if(map[x][y+2] == 0 )//x-2是空地时 { if(flag == 0) { map[x][y+2] = 4; map[x][y+1] = 3; map[x][y] = 0; y++; } else { map[x][y+2] = 4; map[x][y+1] = 3; map[x][y] = 2; y++; flag = 0; } } else if(map[x][y+2] == 2)//x-2是箱子的存放地 { map[x][y+2] = 5; map[x][y+1] = 3; map[x][y] = 0; y++; } } else if (map[x][y+1] == 5)//下一个去的地方是已经在存放点的箱子 { if(map[x][y+2] != 1) { map[x][y+2] = 4; map[x][y+1] = 3; map[x][y] = 0; y++; if(map[x][y+2] != 1) { flag = 1; } } } break; default: break; } } int main() { printf(" \r\n请输入WASD控制行走\r\n"); sleep(2); while ((map[1][6] != 5) || (map[7][2] != 5)) { PrintMap(); PlayGame(); system("cls"); } printf(" \n恭喜您成功通关\n\n\n"); return 0; }
结果显示: