C语言实现推箱子

 底层逻辑才是重点

我只做了10关

废话不多说,上代码

#pragma warning (disable:4996) //消除警告

//头文件引用
#include <stdio.h>
#include <Windows.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include<string.h>

//
#define ROW 10
#define COL 10
#define WALL 1
#define KONG 2
#define PLAYER 3
#define BOX 4
#define BOXLOCATION 5

//键值宏定义
#define UP 72//方向键:上
#define DOWN 80//方向键:下
#define LEFT 75//方向键:左
#define RIGHT 77//方向键:右
#define ESC 27//退出
#define N 224//无效键值

//玩家信息
struct Player
{
	int x;//横坐标
	int y;//纵坐标
}player;

//箱子信息
struct Box
{
	int x;//横坐标
	int y;//纵坐标
}box[6];

//箱子最终位信息
struct BoxLocation
{
	int x;//横坐标
	int y;//纵坐标
}boxloc[6];

char face[ROW][COL];//记录游戏区各位置状态

//步数
int Step;


//函数
//主函数
int main();
//隐藏光标
void HideCursor()
{
	CONSOLE_CURSOR_INFO curInfo; //定义光标信息的结构体变量
	curInfo.dwSize = 1; //如果没赋值的话,光标隐藏无效
	curInfo.bVisible = FALSE; //将光标设置为不可见
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); //获取控制台句柄
	SetConsoleCursorInfo(handle, &curInfo); //设置光标信息
}
//光标跳转
void CursorJump(int x, int y)
{
	COORD pos; //定义光标位置的结构体变量
	pos.X = x; //横坐标
	pos.Y = y; //纵坐标
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); //获取控制台句柄
	SetConsoleCursorPosition(handle, pos); //设置光标位置
}
//颜色设置
void color(int c)
{
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c); //颜色设置
	//注:SetConsoleTextAttribute是一个API(应用程序编程接口)
}

//清空上一关缓存
void clean()
{
	for (int n = 1; n <= 5; n++)
	{
		boxloc[n].x = 0;
		boxloc[n].y = 0;
		box[n].x = 0;
		box[n].y = 0;
	}
	for (int i = 0; i < 10; i++)
	{
		for (int j = 0; j < 10; j++)
		{
			face[i][j] = KONG;
		}
	}
	return;
}

//初始化界面
void InitInterface(int n)
{
	Step = 0;
	clean();//清除上一关缓存
	system("cls");
	system("mode con cols=20 lines=11"); //设置cmd窗口的大小
	color(6);//土黄色
	for (int i = 0; i < ROW; i++)
	{
		for (int j = 0; j < COL; j++)
		{
			if (j == 0 || j == COL - 1)
			{
				face[i][j] = WALL;
				CursorJump(2 * j, i);
				printf("■");
			}
			else if (i == 0 || i == ROW - 1)
			{
				face[i][j] = WALL;
				CursorJump(2 * j, i);
				printf("■");
			}
			else
			{
				face[i][j] = KONG;
				printf("  ");
			}
		}
	}
	color(7);
	CursorJump(1, 11);
	printf("步数:%d", Step);

}
//打印界面
void my_printf()
{
	CursorJump(0, 0);
	for (int i = 0; i < ROW; i++)
	{
		for (int j = 0; j < COL; j++)
		{
			if (face[i][j] == KONG)
			{
				for (int n = 1; n <=5; n++)
				{
					if (i == boxloc[n].y && j == boxloc[n].x)
					{
						color(8);
						printf("●");
						face[i][j] = BOXLOCATION;
						break;
					}
				}  
				if (face[i][j] == KONG)
				{
					printf("  ");
				}
			}
			else if (face[i][j] == WALL)
			{
				color(6);//土黄色
				printf("■");
			}
			else if (face[i][j] == PLAYER)
			{
					color(3);
					printf("■");
			}
			else if (face[i][j] == BOX)
			{
				color(5);
				printf("■");
			}
			else if (face[i][j] == BOXLOCATION)
			{
				color(8);
				printf("●");
			}
		}
	}
	color(7);
	CursorJump(1, 11);
	printf("步数:%d", Step);
}
//设置箱子最终位
 void SetBoxloc(int x, int y,int n)
{
		CursorJump(2 * x, y);
		color(8);
		printf("●");
		face[y][x] = BOXLOCATION;
		boxloc[n].x = x;
		boxloc[n].y = y;
}

//设置箱子位
void SetBox(int x, int y,int n)
{
	CursorJump(2 * x, y);
	c
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值