推箱子(Num014)
目录
代码示例:
代码示例:
#include<stdio.h>
#include<conio.h>
#include<windows.h>
//推箱子
#define MAP_ROW 10
#define MAP_COL 10enum { up='w', down='s', left='a', right='d' };enum { map_null, map_wall, map_person, map_box, map_destination,map_pOnd=6,map_bOnd };
//地图
char map[MAP_ROW][MAP_COL] = { 0 };int level = 1; //关卡//存储所有关卡的地图
//int myMap[3][MAP_ROW][MAP_COL] = {
// {
// 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,4,4,0,0,0,0,
// 0,0,0,0,0,3,0,0,0,0,
// 0,0,0,0,3,0,0,0,0,0,
// 0,0,0,0,0,0,0,0,0,0,
// 0,0,0,0,2,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,4,0,0,0,0,0,0,0,
// 0,0,0,0,0,0,0,0,0,0,
// 0,0,0,0,4,4,0,0,0,0,
// 0,0,0,0,0,3,0,0,0,0,
// 0,0,0,0,3,0,0,0,0,0,
// 0,0,0,0,0,0,0,0,3,0,
// 0,0,0,0,2,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,4,0,0,0,0,3,0,0,
// 0,0,0,0,0,0,0,0,0,0,
// 0,0,0,0,4,4,0,0,0,0,
// 0,0,0,0,0,3,0,0,0,0,
// 0,0,0,0,3,0,0,0,0,0,
// 0,0,0,0,0,0,0,0,3,0,
// 0,3,0,0,2,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 drawMap();
void play();
int isWin();
void initGame();int main()
{initGame();drawMap();while (1){play();drawMap();if (isWin()){initGame();drawMap();}}getchar();getchar();return 0;
}//存放地图
void initGame()
{//for (int i = 0; i < MAP_ROW; i++)//{// for (int j = 0; j < MAP_COL; j++)// {// map[i][j] = mymap[level][i][j];// }//}//level++;char str[20] = { 0 }; //用来保存文件名字sprintf(str, "map/%d.txt", level);FILE* file = fopen(str, "r");if (file == NULL){if (IDYES == MessageBox(NULL, "游戏结束,是否从第一关开始!", "提示", MB_YESNO)){level = 1;sprintf(str, "map/%d.txt", level);file = fopen(str, "r");}else{exit(0);}}//逐字节初始化内存memset(map, 0, sizeof(char) * MAP_ROW * MAP_COL);for (int i = 0; i < MAP_ROW; i++){for (int j = 0; j < MAP_COL; j++){map[i][j] = fgetc(file);}}fclose(file);}//判断游戏胜利
int isWin()
{for (int i = 0; i < MAP_ROW; i++){for (int j = 0; j < MAP_COL; j++){if (map[i][j] == map_box) //找到空地上的箱子游戏结束{return 0; //表示游戏继续}}}level++;return 1; //游戏胜利
}//按键控制
void play()
{int x, y;for (int i = 0; i < MAP_ROW; i++){for (int j = 0; j < MAP_COL; j++){if (map[i][j] == map_person||map[i][j]==map_pOnd){x = i;y = j;}}}switch (_getch()) //获取一个字符,按下就马上获取,不按就阻塞{case up:if (map[x - 1][y] == map_null|| map[x - 1][y] == map_destination){map[x - 1][y] +=2;map[x][y] -= 2;}if (map[x - 1][y] == map_box||map[x-1][y]==map_bOnd){if (map[x - 2][y] == map_null|| map[x - 2][y] == map_destination){map[x - 2][y] += 3;map[x - 1][y] -= 1;map[x][y] -= 2;}}break;case down:if (map[x + 1][y] == map_null || map[x + 1][y] == map_destination){map[x + 1][y] += 2;map[x][y] -= 2;}if (map[x + 1][y] == map_box || map[x + 1][y] == map_bOnd){if (map[x + 2][y] == map_null || map[x + 2][y] == map_destination){map[x + 2][y] += 3;map[x + 1][y] -= 1;map[x][y] -= 2;}}break;case left:if (map[x][y-1] == map_null || map[x][y-1] == map_destination){map[x][y-1] += 2;map[x][y] -= 2;}if (map[x][y-1] == map_box || map[x][y-1] == map_bOnd){if (map[x][y-2] == map_null || map[x][y-2] == map_destination){map[x][y-2] += 3;map[x][y-1] -= 1;map[x][y] -= 2;}}break;case right:if (map[x][y + 1] == map_null || map[x][y + 1] == map_destination){map[x][y + 1] += 2;map[x][y] -= 2;}if (map[x][y + 1] == map_box || map[x][y + 1] == map_bOnd){if (map[x][y + 2] == map_null || map[x][y + 2] == map_destination){map[x][y + 2] += 3;map[x][y + 1] -= 1;map[x][y] -= 2;}}break;case 'r':initGame();break;}}//实时绘制地图
void drawMap()
{system("cls"); //做清屏操作for (int i = 0;i<MAP_ROW;i++) {for (int j = 0;j<MAP_COL;j++){switch(map[i][j]){case map_null:printf(" ");break;case map_wall:printf("墙");break;case map_person:printf("人");break;case map_box:printf("箱");break;case map_destination:printf("地");break;case map_pOnd:printf("人");break;case map_bOnd:printf("箱");break;}}printf("\n");}
}
###图形库的下载地址:https://easyx.cn/