【C语言】推箱子小游戏

推箱子小游戏

目前已实现功能

选择关卡
  便捷地图编辑接口(使用结构体加结构体指针实现)

待实现功能

撤回一步
  重新开始

效果展示

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

源码

头文件
#ifndef __TUIXIANGZI_H__
#define __TUIXIANGZI_H__

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <Windows.h>
#include <conio.h>

#define ROW 12   //地图行数
#define COL 12   //地图列数
#define NUM 5   //地图总数
enum MENU
{
	EXIT,
	PLAY
};

struct MAP
{
	const int row;
	const int col;
	char map[ROW][COL];
};

void menu();  //菜单

void game();  //游戏实现函数

void print_map(char (*map)[COL], const int *row, const int *col);  //打印地图

void find_man(char (*map)[COL], const int *row, const int *col, int *x, int *y);  //找到玩家的坐标

void player_act(char (*map)[COL], int *x, int *y);  //玩家控制小人行动

int is_win(char (*map)[COL], const int *row, const int *col);    //判断是否胜利

#endif  //__TUIXIANGZI_H__
主函数文件
#define _CRT_SECURE_NO_WARNINGS 1
#include "tuixiangzi.h"


int main()
{
	int input = 0;

	do
	{
		menu();
		printf("请选择想要进行的操作:>");
		scanf("%d", &input);

		switch (input)
		{
		case PLAY:  system("cls"), game(), system("cls");
			break;
		case EXIT:  printf("正在退出游戏\n");
			break;
		default:  printf("你选择的选项有误,我看不懂\n");
			break;
		}

	} while (input);
	return 0;
}
功能实现文件
#define _CRT_SECURE_NO_WARNINGS 1
#include "tuixiangzi.h"

struct MAP all_map[NUM] = 
{ { 8, 8, { { 0, 0, 1, 1, 1, 0, 0, 0 },  //地图一
{ 0, 0, 1, 3, 1, 0, 0, 0 },
{ 0, 0, 1, 0, 1, 1, 1, 1 },
{ 1, 1, 1, 2, 0, 2, 3, 1 },
{ 1, 3, 0, 2, 4, 1, 1, 1 },
{ 1, 1, 1, 1, 2, 1, 0, 0 },
{ 0, 0, 0, 1, 3, 1, 0, 0 },
{ 0, 0, 0, 1, 1, 1, 0, 0 } } },
{ 9, 9, { {1,1,1,1,1,0,0,0,0},   //地图二
{1,4,0,0,1,0,0,0,0},
{1,0,2,2,1,0,1,1,1},
{1,0,2,0,1,0,1,3,1},
{1,1,1,0,1,1,1,3,1},
{0,1,1,0,0,0,0,3,1},
{0,1,0,0,0,1,0,0,1},
{0,1,0,0,0,1,1,1,1},
{0,1,1,1,1,1,0,0,0} } },
{ 7, 10, { { 0, 1, 1, 1, 1, 1, 1, 1, 0, 0 },  //地图三
{ 0, 1, 0, 0, 0, 0, 0, 1, 1, 1 },
{ 1, 1, 2, 1, 1, 1, 0, 0, 0, 1 },
{ 1, 0, 4, 0, 2, 0, 0, 2, 0, 1 },
{ 1, 0, 3, 3, 1, 0, 2, 0, 1, 1 },
{ 1, 1, 3, 3, 1, 0, 0, 0, 1, 0 },
{0,1,1,1,1,1,1,1,1,0} } },
{ 8, 6, { { 0, 1, 1, 1, 1, 0 },    //地图四
{ 1, 1, 0, 0, 1, 0 },
{ 1, 4, 2, 0, 1, 0 },
{ 1, 1, 2, 0, 1, 1 },
{ 1, 1, 0, 2, 0, 1 },
{ 1, 3, 2, 0, 0, 1 },
{ 1, 3, 3, 5, 3, 1 },
{ 1, 1, 1, 1, 1, 1 } } },
{ 8, 8, { { 0, 1, 1, 1, 1, 1, 0, 0 },  //地图五
{ 0, 1, 0, 4, 0, 1, 1, 1 },
{ 1, 1, 0, 1, 2, 0, 0, 1 },
{ 1, 0, 5, 3, 0, 3, 0, 1 },
{ 1, 0, 0, 2, 2, 0, 1, 1 },
{ 1, 1, 1, 0, 1, 3, 1, 0 },
{ 0, 0, 1, 0, 0, 0, 1, 0 },
{ 0, 0, 1, 1, 1, 1, 1, 0 } } } };   //在这里增加地图,行数,列数,以及地图元素

void menu()  //菜单
{
	printf("***********************\n");
	printf("***    1.开始游戏   ***\n");
	printf("***    0.退出游戏   ***\n");
	printf("***********************\n");
}

void find_man(char (*map)[COL], const int *row, const int *col, int *x, int *y)  //找到玩家的坐标
{
	int i = 0;
	for (i = 0; i < *row; i++)
	{
		int j = 0;
		for (j = 0; j < *col; j++)
		{
			if ((map[i][j] == 4) || (map[i][j] == 7))
			{
				*x = i;
				*y = j;
				return;
			}
		}
	}
}

void print_map(char (*map)[COL], const int *row, const int *col)  //打印地图
{
	int i = 0;

	printf("\n----------------------\n\n");
	for (i = 0; i < *row; i++)
	{
		int j = 0;
		for (j = 0; j < *col; j++)
		{
			switch ((map[i][j]))
				{
				case 0:printf("  ");
					break;
				case 1:printf("■");
					break;
				case 2:printf("□");
					break;
				case 3:printf("◎");
					break;
				case 4:printf("♀");
					break;
				case 5:printf("★");
					break;
				case 7:printf("@");
					break;
				}
		}
		printf("\n");
	}
	printf("\nW A S D 控制小人的上下左右\n");
	printf("\n■表示墙,□表示箱子,◎表示目的地\n♀表示玩家,★表示箱子在目的地上,@表示人在目的地上\n");
	printf("\n----------------------\n\n");
}
 
int is_win(char (*map)[COL], const int *row, const int *col)    //判断是否胜利
{
	int dest = 0;
	int i = 0;
	for (i = 0; i < *row; i++)
	{
		int j = 0;
		for (j = 0; j < *col; j++)
		{
			if (map[i][j] == 2 || map[i][j] == 7)
				return 0;
		}
	}
	return 1;
}
void player_act(char (*map)[COL], int *x, int *y)  //玩家控制小人行动
{
	char input = 0;
	switch (input = _getch())
	{
	case 'W':
	case 'w':  
	case 38://向上走
		{
			if (map[(*x) - 1][*y] == 0 && map[*x][*y] == 4)
			{
				map[*x][*y] = 0;
				map[(*x) - 1][*y] = 4;
				--*x;
			}
			else if (map[(*x) - 1][*y] == 0 && map[*x][*y] == 7)
			{
				map[*x][*y] = 3;
				map[(*x) - 1][*y] = 4;
				--*x;
			}
			else if (map[(*x) - 1][*y] == 3 && map[*x][*y] == 7)
			{
				map[*x][*y] = 3;
				map[(*x) - 1][*y] = 7;
				--*x;
			}
			else if (map[(*x) - 1][*y] == 3 && map[*x][*y] == 4)
			{
				map[*x][*y] = 0;
				map[(*x) - 1][*y] = 7;
				--*x;
			}
			else if (map[(*x) - 1][*y] == 0 && map[*x][*y] == 7)
			{
				map[*x][*y] = 3;
				map[(*x) - 1][*y] = 4;
				--*x;
			}
			else if (map[(*x) - 1][*y] == 2 && map[*x][*y] == 4 && map[(*x) - 2][*y] == 0)
			{
				map[*x][*y] = 0;
				map[(*x) - 1][*y] = 4;
				map[(*x) - 2][*y] = 2;
				--*x;
			}
			else if (map[(*x) - 1][*y] == 2 && map[*x][*y] == 7 && map[(*x) - 2][*y] == 0)
			{
				map[*x][*y] = 3;
				map[(*x) - 1][*y] = 4;
				map[(*x) - 2][*y] = 2;
				--*x;
			}
			else if (map[(*x) - 1][*y] == 2 && map[*x][*y] == 7 && map[(*x) - 2][*y] == 3)
			{
				map[*x][*y] = 3;
				map[(*x) - 1][*y] = 4;
				map[(*x) - 2][*y] = 5;
				--*x;
			}
			else if (map[(*x) - 1][*y] == 2 && map[*x][*y] == 4 && map[(*x) - 2][*y] == 3)
			{
				map[*x][*y] = 0;
				map[(*x) - 1][*y] = 4;
				map[(*x) - 2][*y] = 5;
				--*x;
			}
			else if (map[(*x) - 1][*y] == 5 && map[*x][*y] == 7 && map[(*x) - 2][*y] == 3)
			{
				map[*x][*y] = 3;
				map[(*x) - 1][*y] = 7;
				map[(*x) - 2][*y] = 5;
				--*x;
			}
			else if (map[(*x) - 1][*y] == 5 && map[*x][*y] == 4 && map[(*x) - 2][*y] == 0)
			{
				map[*x][*y] = 0;
				map[(*x) - 1][*y] = 7;
				map[(*x) - 2][*y] = 2;
				--*x;
			}
			else if (map[(*x) - 1][*y] == 5 && map[*x][*y] == 4 && map[(*x) - 2][*y] == 3)
			{
				map[*x][*y] = 0;
				map[(*x) - 1][*y] = 7;
				map[(*x) - 2][*y] = 5;
				--*x;
			}
		}
		break;
	case 'S':
	case 's':   
	case 40://向下走
		{
			if (map[(*x) + 1][*y] == 0 && map[*x][*y] == 4)
			{
				map[*x][*y] = 0;
				map[(*x) + 1][*y] = 4;
				++*x;
			}
			else if (map[(*x) + 1][*y] == 0 && map[*x][*y] == 7)
			{
				map[*x][*y] = 3;
				map[(*x) + 1][*y] = 4;
				++*x;
			}
			else if (map[(*x) + 1][*y] == 3 && map[*x][*y] == 7)
			{
				map[*x][*y] = 3;
				map[(*x) + 1][*y] = 7;
				++*x;
			}
			else if (map[(*x) + 1][*y] == 3 && map[*x][*y] == 4)
			{
				map[*x][*y] = 0;
				map[(*x) + 1][*y] = 7;
				++*x;
			}
			else if (map[(*x) + 1][*y] == 0 && map[*x][*y] == 7)
			{
				map[*x][*y] = 3;
				map[(*x) + 1][*y] = 4;
				++*x;
			}
			else if (map[(*x) + 1][*y] == 2 && map[*x][*y] == 4 && map[(*x) + 2][*y] == 0)
			{
				map[*x][*y] = 0;
				map[(*x) + 1][*y] = 4;
				map[(*x) + 2][*y] = 2;
				++*x;
			}
			else if (map[(*x) + 1][*y] == 2 && map[*x][*y] == 7 && map[(*x) + 2][*y] == 0)
			{
				map[*x][*y] = 3;
				map[(*x) + 1][*y] = 4;
				map[(*x) + 2][*y] = 2;
				++*x;
			}
			else if (map[(*x) + 1][*y] == 2 && map[*x][*y] == 7 && map[(*x) + 2][*y] == 3)
			{
				map[*x][*y] = 3;
				map[(*x) + 1][*y] = 4;
				map[(*x) + 2][*y] = 5;
				++*x;
			}
			else if (map[(*x) + 1][*y] == 2 && map[*x][*y] == 4 && map[(*x) + 2][*y] == 3)
			{
				map[*x][*y] = 0;
				map[(*x) + 1][*y] = 4;
				map[(*x) + 2][*y] = 5;
				++*x;
			}
			else if (map[(*x) + 1][*y] == 5 && map[*x][*y] == 7 && map[(*x) + 2][*y] == 3)
			{
				map[*x][*y] = 3;
				map[(*x) + 1][*y] = 7;
				map[(*x) + 2][*y] = 5;
				++*x;
			}
			else if (map[(*x) + 1][*y] == 5 && map[*x][*y] == 4 && map[(*x) + 2][*y] == 0)
			{
				map[*x][*y] = 0;
				map[(*x) + 1][*y] = 7;
				map[(*x) + 2][*y] = 2;
				++*x;
			}
			else if (map[(*x) + 1][*y] == 5 && map[*x][*y] == 4 && map[(*x) + 2][*y] == 3)
			{
				map[*x][*y] = 0;
				map[(*x) + 1][*y] = 7;
				map[(*x) + 2][*y] = 5;
				++*x;
			}
		}
		break;
	case 'A':
	case 'a':  
	case 37://向左走
		{
			if (map[*x][(*y) - 1] == 0 && map[*x][*y] == 4)
			{
				map[*x][*y] = 0;
				map[*x][(*y) - 1] = 4;
				--*y;
			}
			else if (map[*x][(*y) - 1] == 0 && map[*x][*y] == 7)
			{
				map[*x][*y] = 3;
				map[*x][(*y) - 1] = 4;
				--*y;
			}
			else if (map[*x][(*y) - 1] == 3 && map[*x][*y] == 7)
			{
				map[*x][*y] = 3;
				map[*x][(*y) - 1] = 7;
				--*y;
			}
			else if (map[*x][(*y) - 1] == 3 && map[*x][*y] == 4)
			{
				map[*x][*y] = 0;
				map[*x][(*y) - 1] = 7;
				--*y;
			}
			else if (map[*x][(*y) - 1] == 0 && map[*x][*y] == 7)
			{
				map[*x][*y] = 3;
				map[*x][(*y) - 1] = 4;
				--*y;
			}
			else if (map[*x][(*y) - 1] == 2 && map[*x][*y] == 4 && map[*x][(*y) - 2] == 0)
			{
				map[*x][*y] = 0;
				map[*x][(*y) - 1] = 4;
				map[*x][(*y) - 2] = 2;
				--*y;
			}
			else if (map[*x][(*y) - 1] == 2 && map[*x][*y] == 7 && map[*x][(*y) - 2] == 0)
			{
				map[*x][*y] = 3;
				map[*x][(*y) - 1] = 4;
				map[*x][(*y) - 2] = 2;
				--*y;
			}
			else if (map[*x][(*y) - 1] == 2 && map[*x][*y] == 7 && map[*x][(*y) - 2] == 3)
			{
				map[*x][*y] = 3;
				map[*x][(*y) - 1] = 4;
				map[*x][(*y) - 2] = 5;
				--*y;
			}
			else if (map[*x][(*y) - 1] == 2 && map[*x][*y] == 4 && map[*x][(*y) - 2] == 3)
			{
				map[*x][*y] = 0;
				map[*x][(*y) - 1] = 4;
				map[*x][(*y) - 2] = 5;
				--*y;
			}
			else if (map[*x][(*y) - 1] == 5 && map[*x][*y] == 7 && map[*x][(*y) - 2] == 3)
			{
				map[*x][*y] = 3;
				map[*x][(*y) - 1] = 7;
				map[*x][(*y) - 2] = 5;
				--*y;
			}
			else if (map[*x][(*y) - 1] == 5 && map[*x][*y] == 4 && map[*x][(*y) - 2] == 0)
			{
				map[*x][*y] = 0;
				map[*x][(*y) - 1] = 7;
				map[*x][(*y) - 2] = 2;
				--*y;
			}
			else if (map[*x][(*y) - 1] == 5 && map[*x][*y] == 4 && map[*x][(*y) - 2] == 3)
			{
				map[*x][*y] = 0;
				map[*x][(*y) - 1] = 7;
				map[*x][(*y) - 2] = 5;
				--*y;
			}
		}
		break;
	case 'D':
	case 'd':  
	case 39://向右走
	{
		if (map[*x][(*y) + 1] == 0 && map[*x][*y] == 4)
		{
			map[*x][*y] = 0;
			map[*x][(*y) + 1] = 4;
			++*y;
		}
		else if (map[*x][(*y) + 1] == 0 && map[*x][*y] == 7)
		{
			map[*x][*y] = 3;
			map[*x][(*y) + 1] = 4;
			++*y;
		}
		else if (map[*x][(*y) + 1] == 3 && map[*x][*y] == 7)
		{
			map[*x][*y] = 3;
			map[*x][(*y) + 1] = 7;
			++*y;
		}
		else if (map[*x][(*y) + 1] == 3 && map[*x][*y] == 4)
		{
			map[*x][*y] = 0;
			map[*x][(*y) + 1] = 7;
			++*y;
		}
		else if (map[*x][(*y) + 1] == 0 && map[*x][*y] == 7)
		{
			map[*x][*y] = 3;
			map[*x][(*y) + 1] = 4;
			++*y;
		}
		else if (map[*x][(*y) + 1] == 2 && map[*x][*y] == 4 && map[*x][(*y) + 2] == 0)
		{
			map[*x][*y] = 0;
			map[*x][(*y) + 1] = 4;
			map[*x][(*y) + 2] = 2;
			++*y;
		}
		else if (map[*x][(*y) + 1] == 2 && map[*x][*y] == 7 && map[*x][(*y) + 2] == 0)
		{
			map[*x][*y] = 3;
			map[*x][(*y) + 1] = 4;
			map[*x][(*y) + 2] = 2;
			++*y;
		}
		else if (map[*x][(*y) + 1] == 2 && map[*x][*y] == 7 && map[*x][(*y) + 2] == 3)
		{
			map[*x][*y] = 3;
			map[*x][(*y) + 1] = 4;
			map[*x][(*y) + 2] = 5;
			++*y;
		}
		else if (map[*x][(*y) + 1] == 2 && map[*x][*y] == 4 && map[*x][(*y) + 2] == 3)
		{
			map[*x][*y] = 0;
			map[*x][(*y) + 1] = 4;
			map[*x][(*y) + 2] = 5;
			++*y;
		}
		else if (map[*x][(*y) + 1] == 5 && map[*x][*y] == 7 && map[*x][(*y) + 2] == 3)
		{
			map[*x][*y] = 3;
			map[*x][(*y) + 1] = 7;
			map[*x][(*y) + 2] = 5;
			++*y;
		}
		else if (map[*x][(*y) + 1] == 5 && map[*x][*y] == 4 && map[*x][(*y) + 2] == 0)
		{
			map[*x][*y] = 0;
			map[*x][(*y) + 1] = 7;
			map[*x][(*y) + 2] = 2;
			++*y;
		}
		else if (map[*x][(*y) + 1] == 5 && map[*x][*y] == 4 && map[*x][(*y) + 2] == 3)
		{
			map[*x][*y] = 0;
			map[*x][(*y) + 1] = 7;
			map[*x][(*y) + 2] = 5;
			++*y;
		}
	}
		break;
	}
}

void game()  //游戏实现函数
{
	int i = 0;
	int x = 0;
	int y = 0;
	struct MAP *pmap = NULL;
	printf("\n\n目前总共有%d关,你要从哪一关开始玩?:>", NUM);
	scanf("%d", &i);
	pmap = all_map + i - 1;
	find_man(pmap->map, &(pmap->row), &(pmap->col), &x, &y);
	while (1)
	{
		system("cls");
		print_map(pmap->map, &(pmap->row), &(pmap->col));
		player_act(pmap->map, &x, &y);
		if (is_win(pmap->map, &(pmap->row), &(pmap->col)) && i < NUM)
		{
			system("cls");
			print_map(pmap->map, &(pmap->row), &(pmap->col));
			printf("你真棒,小伙子\n");
			system("pause");
			pmap++;
			x = 0;
			y = 0;
			i++;
			find_man(pmap->map, &(pmap->row), &(pmap->col), &x, &y);
		}
		else if (i == NUM)
		{
			printf("\n通关了啊!你真厉害!\n");
			return;
		}
	}
}

项目文件下载

全文完,感谢浏览

  • 21
    点赞
  • 93
    收藏
    觉得还不错? 一键收藏
  • 14
    评论
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值