前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >C语言实现推箱子

C语言实现推箱子

作者头像
紫禁玄科
发布2022-03-24 16:58:49
发布2022-03-24 16:58:49
7790
举报
文章被收录于专栏:紫禁玄科紫禁玄科

#include<conio.h>

#include<stdio.h>

#include<string.h>

#include<Windows.h>

int map1[13][14] = { //创建一个地图

{ 0,0,0,0,2,2,2,2,2,2,0,0,0,0 }, // 0: 空地

{ 0,0,0,0,2,0,0,0,0,2,0,0,0,0 }, // 1: 目的地 ☆

{ 0,2,2,2,2,0,0,0,0,2,2,2,2,2 }, // 2: 墙壁 ■

{ 0,2,0,0,0,2,0,0,0,0,0,0,0,2 }, // 4: 箱子 □

{ 0,2,0,4,0,0,0,4,0,0,0,4,0,2 }, // 8: 玩家 ♀

{ 0,2,0,0,0,2,2,0,2,2,2,2,2,2 }, // 16:箱子已在目的地 ★

{ 2,2,2,2,0,0,0,0,0,0,0,0,0,2 }, // 32:人在目的地上 ♂

{ 2,0,0,0,0,0,2,0,0,0,8,0,0,2 },

{ 2,0,4,0,0,0,2,0,2,2,2,2,2,2 },

{ 2,2,0,0,0,0,2,0,2,0,1,1,2,0 },

{ 0,2,0,2,0,0,0,0,0,0,1,1,2,0 },

{ 0,2,0,0,0,0,2,2,0,0,0,0,2,0 },

{ 0,2,2,2,2,2,2,2,2,2,2,2,2,0 }

};

void init_map1() //初始化该地图

{

int row;

int col;

for (row = 0; row < sizeof(map1) / sizeof(map1[0]); row++)

{

printf(" ");

for (col = 0; col < sizeof(map1[0]) / sizeof(map1[0][0]); col++)

{

switch (map1[row][col])

{

case 0:

printf("%s", " ");

break;

case 1:

printf("%s", "☆");

break;

case 2:

printf("%s", "■");

break;

case 4:

printf("%s", "□");

break;

case 8:

printf("%s", "♀");

break;

case 16:

printf("%s", "★");

break;

case 32:

printf("%s", "♂");

break;

default:

break;

}

}

printf("\n");

}

}

int is_win_map1(void)

{

int row;

int col;

int num = 0;

for (row = 0; row < sizeof(map1) / sizeof(map1[0]); row++)

{

for (col = 0; col < sizeof(map1[0]) / sizeof(map1[0][0]); col++)

{

if (map1[row][col] == 4)

num++;

}

}

if (num == 0)

{

printf("游戏结束,恭喜你过关!\n");

return 0;

}

return 1;

}

void player_game_map1(int row, int col)

{

int perrow, percol;

int i;

int j;

for (i = 0; i < sizeof(map1) / sizeof(map1[0]); i++)

{

for (j = 0; j < sizeof(map1[0]) / sizeof(map1[0][0]); j++)

{

if (map1[i][j] == 8 || map1[i][j] == 32)

{

perrow = i;

percol = j;

}

}

}

if (map1[perrow + row][percol + col] == 0)

{

if (map1[perrow][percol] == 32)

{

map1[perrow + row][percol + col] = 8;

map1[perrow][percol] = 1;

}

else

{

map1[perrow + row][percol + col] = 8;

map1[perrow][percol] = 0;

}

return;

}

if (map1[perrow + row][percol + col] == 1)

{

if (map1[perrow][percol] == 32)

{

map1[perrow + row][percol + col] = 32;

map1[perrow][percol] = 1;

}

else

{

map1[perrow + row][percol + col] = 32;

map1[perrow][percol] = 0;

}

return;

}

if (map1[perrow + row][percol + col] == 4)

{

if (map1[perrow + row + row][percol + col + col] == 0)

{

map1[perrow + row + row][percol + col + col] = 4;

map1[perrow + row][percol + col] = 8;

map1[perrow][percol] = 0;

}

if (map1[perrow + row + row][percol + col + col] == 1)

{

map1[perrow + row + row][percol + col + col] = 16;

map1[perrow + row][percol + col] = 8;

map1[perrow][percol] = 0;

}

return;

}

if (map1[perrow + row][percol + col] == 16)

{

if (map1[perrow][percol] == 32

&& map1[perrow + row + row][percol + col + col] == 1)

{

map1[perrow + row + row][percol + col + col] = 16;

map1[perrow + row][percol + col] = 32;

map1[perrow][percol] = 1;

}

if (map1[perrow][percol] == 32

&& map1[perrow + row + row][percol + col + col] == 0)

{

map1[perrow + row + row][percol + col + col] = 4;

map1[perrow + row][percol + col] = 32;

map1[perrow][percol] = 1;

return;

}

if (map1[perrow + row + row][percol + col + col] != 16 && map1[perrow + row + row][percol + col + col] != 2)

{

map1[perrow + row + row][percol + col + col] = 16;

map1[perrow + row][percol + col] = 32;

map1[perrow][percol] = 0;

}

return;

}

}

int main()

{

int a;

system("mode con cols=40 lines=15");

system("color 5e");

init_map1();

do

{

switch (getch())

{

case 'w':

player_game_map1(-1, 0);

break;

case 's':

player_game_map1(1, 0);

break;

case 'a':

player_game_map1(0, -1);

break;

case 'd':

player_game_map1(0, 1);

break;

default:

printf("输入错误\n");

break;

}

system("cls");

init_map1();

} while (is_win_map1());

system("pause");

return 0;

}

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-11-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 紫禁玄科 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档