推箱子(C语言版)

  推箱子这个小游戏,当初自己也写了很久

这个游戏:就是人将所有箱子推到目的地,便算是成功

主要思路:

1.用二维数组和枚举绘制地图(如果要增加关卡,就用三维数组(里面存放的元素是多个二维数组),这里我写了三关)

2.用数组的位置来写人和箱子的移动,再配合switch语句,就可以实现用键盘操控人的移动。

3.游戏结束的标志:空地上没有箱子

源码在这,如有需要,请自行领取。

#include                                       // 推箱子的几个元素;
#include                                     //  0 空地   1 墙  2 目的地   3 箱子 4 玩 5 箱子在目的地  6 玩家在目的地 
#include//_getch()函数的头文件   //用_getch函数,如果用getchar(),每次移动要回车键  // 上 72  下 80  左75   右77

enum Mine
{
	space,
	wall,
	destination,
	box,
	player,
};
int level = 0;
int map[3][10][10] =
{
	{
		{0, 0, 0, 1, 1, 1 ,1 ,1, 0, 0},
		{0, 0, 0, 1, 0, 0 ,2 ,1, 0, 0},
		{0, 0, 0, 1, 0, 3 ,0 ,1, 0, 0},
		{0, 1, 1, 1, 0, 0 ,0 ,1, 1, 1},
		{0, 1, 0, 2, 3 ,4 ,3, 0, 2, 1},
		{0, 1, 1, 1, 0, 3 ,0 ,1, 1, 1},
		{0, 0, 0, 1, 0, 2 ,0 ,1, 0, 0},
		{0, 0, 0, 1, 0, 0 ,0 ,1, 0, 0},
		{0, 0, 0, 1, 1, 1 ,1 ,1, 0, 0},
		{0, 0, 0, 0, 0, 0 ,0 ,0, 0, 0},
	},

	{
		{1, 1, 1, 1, 1, 1 ,1 ,1, 1, 1},
		{1, 2, 0, 0, 0, 0 ,0 ,0, 0, 1},
		{1, 0, 0, 0, 0, 0 ,3 ,0, 1, 0},
		{1, 0, 0, 3, 0, 0 ,0 ,0, 0, 1},
		{1, 0, 0, 3, 0 ,4 ,0, 0, 0, 1},
		{1, 0, 0, 0, 0, 0 ,1 ,0, 0, 1},
		{1, 2, 1, 1, 0, 0 ,1 ,0, 2, 1},
		{1, 1, 1, 0, 1, 0 ,1 ,0, 1, 0},
		{0, 0, 0, 0, 1, 0 ,1 ,0, 1, 0},
		{0, 0, 0, 0, 1, 1 ,1 ,1, 0, 0},
	},

	{
		{0, 0, 0, 1, 1, 1 ,1 ,0, 0, 0},
		{0, 0, 1, 0, 0, 0 ,0 ,1, 0, 0},
		{0, 1, 0, 3, 0, 3 ,0 ,0, 1, 0},
		{1, 2, 0, 0, 0, 0 ,1 ,0, 0, 1},
		{1, 1, 1, 1, 2 ,0 ,1, 0, 0, 1},
		{1, 0, 0, 1, 1, 1 ,1 ,0, 0, 1},
		{1, 2, 0, 0, 0, 4 ,0 ,0, 0, 1},
		{0, 1, 0, 0, 3, 0 ,3 ,0, 1, 0},
		{0, 0, 1, 0, 0, 0 ,2 ,1, 0, 0},
		{0, 0, 0, 1, 1, 1 ,1 ,0, 0, 0},
	}

};

void drawmap()
{
	for (int i = 0; i < 10; ++i)
	{
		for (int j = 0; j < 10; ++j)
		{
			switch (map[level][i][j])
			{
			case space:
				printf("  ");
				break;
			case wall:
				printf("▓ ");
				break;
			case destination:
				printf("☆");
				break;
			case box:
				printf("□");
				break;
			case player:
				printf("♀");
				break;
			case box + destination:
				printf("★");
				break;
			case player + destination:
				printf("♂");
				break;
			}
		}
		printf("\n");
	}
}
void playgame()
{
	int i = 0, j = 0;
	for (i = 0; i < 10; i++)
	{
		for (j = 0; j < 10; j++)
		{
			if (map[level][i][j] == player || map[level][i][j] == player + destination)
			{
				goto end;       //break跳出一层循环,用  goto end; end:; 跳出所有的循环
			}
		}
	}
end:;
	int  key = _getch();
	switch (key)
	{
		case 'w':      // 上
		case 'W':
		case  72:
			if (map[level][i - 1][j] ==  space || map[level][i - 1][j] ==  destination)
			{
				map[level][i - 1][j] += player;
				map[level][i][j] -= player;
			}
			else
			{
				if (map[level][i - 1][j] == box || map[level][i - 1][j] == box + destination)
				{
					if (map[level][i - 2][j] == space || map[level][i - 2][j] == destination)
					{
						map[level][i - 2][j] += box;
						map[level][i - 1][j] = map[level][i - 1][j] - box + player;
						map[level][i - 1][j] -=  player;
					}
				}
			}
			break;

		case 's':     //  下
		case 'S':
		case 80:
			if (map[level][i + 1][j] == space || map[level][i + 1][j] == destination)
			{
				map[level][i + 1][j] = map[level][i + 1][j] + player;
				map[level][i][j] = map[level][i][j] - player;
			}
			else
			{
				if (map[level][i + 1][j] == box || map[level][i + 1][j] == box + destination)
				{
					if (map[level][i + 2][j] == space || map[level][i + 2][j] == destination)
					{
						map[level][i + 2][j] = map[level][i + 2][j] + box;
						map[level][i + 1][j] = map[level][i + 1][j] - box + player;
						map[level][i + 1][j] = map[level][i + 1][j] - player;
					}
				}
			}
			break;
		case 'a':     //  左
		case 'A':
		case 75:
			if (map[level][i][j - 1] == space || map[level][i][j - 1] == destination)
			{
				map[level][i][j - 1] += player;
				map[level][i][j] -= player;
			}
			else
			{
				if (map[level][i][j - 1] == box || map[level][i][j - 1] == box + destination)
				{
					if (map[level][i][j - 2] == space || map[level][i][j - 2 ] == destination)
					{
						map[level][i][j - 2] += box;
						map[level][i][j - 1] = map[level][i][j - 1] - box + player;
						map[level][i][j - 1] -= player;
					}
				}
			}			
			break;

		case 'd':      // 右
		case 'D':
		case 77:
			if (map[level][i][j + 1] == space || map[level][i][j + 1] == destination)
			{
				map[level][i][j + 1] += player;
				map[level][i][j] -= player;
			}
			else
			{
				if (map[level][i][j + 1] == box || map[level][i][j + 1] == box + destination)
				{
					if (map[level][i][j + 2] == space || map[level][i][j + 2] == destination)
					{
						map[level][i][j + 2] += box;
						map[level][i][j + 1] = map[level][i][j + 1] - box + player;
						map[level][i][j + 1] -= player;
					}
				}
			}
			break;
	}
}
bool deduce_success()
{
	for (int a = 0; a < 10; a++)
	{
		for (int b = 0; b < 10; b++)
		{
			if (map[level][a][b] == box)
			{
				return false;
			}
		}
	}
	return true;
}

int main()
{
	//cols 长  lines宽
	system("mode con cols=25 lines=15");

	while (1)
	{
		system("cls");
		drawmap();

		if (deduce_success())
		{
			level++;
			if (level > 2)
			{
				printf("你赢了");
				printf("不愧是地表最强的人!!!!!!");
				printf("恭喜通关!!!!!!");
				break;
			}
		}

		playgame();
	}

	

	getchar();

	return 0;
} 

你可能感兴趣的:(C语言项目,c语言,c++,算法)