⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 room.cpp

📁 vc++经典的仿3D的推箱子游戏
💻 CPP
字号:
#include "stdafx.h"

#include "room.h"


CRoom::CRoom()
{
	number=0;
	width=height=0;
	box=aim=NULL;
	data=NULL;
	people.x=0;people.y=0;
}
CRoom::~CRoom()
{
	if(number!=0)
	{
		DELETE_ARRAY(box);
		DELETE_ARRAY(aim);
	}
	if(width*height!=0)
	{
		DELETE_ARRAY(data);
	}
}
void CRoom::SetRoomRange(int w,int h)
{
	int i,j;
	if(width*height!=0) DELETE_ARRAY(data);
	width=w;height=h;
	data=new int*[h];
	for(i=0;i<h;i++)
	{
		data[i]=new int[w];
		for(j=0;j<w;j++)
			data[i][j]=SPACE;
	}
}
void CRoom::SetPointNum(int n)
{
	int i;
	if(number!=0) 
	{
		DELETE_ARRAY(box);
		DELETE_ARRAY(aim);
	}
	number=n;
	box=new SPoint[n];
	aim=new SPoint[n];
	for(i=0;i<n;i++)
	{
		box[i].x=-1;box[i].y=-1;
		aim[i].x=-1;aim[i].y=-1;
	}
}
void CRoom::SetRoomData(int **d)
{
	int i,j;
	for(i=0;i<height;i++)
		for(j=0;j<width;j++)
			data[i][j]=d[i][j];
}
void CRoom::SetRoom(int x,int y,int type)
{
	data[y][x]=type;
}
void CRoom::SetBox(int n,int x,int y)
{
	box[n].x=x;box[n].y=y;
}
void CRoom::SetPeople(int x,int y)
{
	people.x=x;people.y=y;
}
void CRoom::MovePeople(enum BDirect d)
{
	int i,j;
	bool IsTouch;
	switch(d)
	{
	case B_EAST:
		IsTouch=false;
		if(people.x>0&&data[people.x-1][people.y]!=BLOCK)
		{
			for(i=0;i<number;i++)
			{
				if(people.x==box[i].x+1&&people.y==box[i].y)
				{
					for(j=0;j<number;j++)
					{
						if(box[i].x-box[j].x==1&&box[i].y==box[j].y) IsTouch=true;
					}
					if(people.x>1&&data[people.x-2][people.y]!=BLOCK&&!IsTouch)
					{
						--box[i].x;
					}
					else
					{
						++people.x;
					}
				}
			}

			 --people.x;
		}
		break;
	case B_WEST:
		IsTouch=false;
		if(people.x<width-1&&data[people.x+1][people.y]!=BLOCK)
		{
			for(i=0;i<number;i++)
			{
				if(people.x==box[i].x-1&&people.y==box[i].y)
				{
					for(j=0;j<number;j++)
					{
						if(box[j].x-box[i].x==1&&box[i].y==box[j].y) IsTouch=true;
					}
					if(people.x+2<width&&data[people.x+2][people.y]!=BLOCK&&!IsTouch)
					{
						++box[i].x;
					}
					else
					{
						--people.x;
					}
				}
			}
			++people.x;
		}
		break;
	case B_NORTH:
		IsTouch=false;
		if(people.y>0&&data[people.x][people.y-1]!=BLOCK)
		{
			for(i=0;i<number;i++)
			{
				if(people.x==box[i].x&&people.y==box[i].y+1)
				{
					for(j=0;j<number;j++)
					{
						if(box[i].y-box[j].y==1&&box[i].x==box[j].x) IsTouch=true;
					}
					if(people.y>1&&data[people.x][people.y-2]!=BLOCK&&!IsTouch)
					{
						--box[i].y;
					}
					else
					{
						++people.y;
					}
				}
			}
			--people.y;
		}
		break;
	case B_SOURTH:
		IsTouch=false;
		if(people.y<height-1&&data[people.x][people.y+1]!=BLOCK)
		{
			for(i=0;i<number;i++)
			{
				if(people.x==box[i].x&&people.y==box[i].y-1)
				{
					for(j=0;j<number;j++)
					{
						if(box[j].y-box[i].y==1&&box[i].x==box[j].x) IsTouch=true;
					}
					if(people.y+2<height&&data[people.x][people.y+2]!=BLOCK&&!IsTouch)
					{
						++box[i].y;
					}
					else
					{
						--people.y;
					}
				}
			}
			++people.y;
		}
		break;
	}
}
bool CRoom::IsDone(void)
{
	int i;
	for(i=0;i<number;i++)
		if(data[box[i].x][box[i].y]!=AIM) return false;
	return true;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -