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

📄 manager.cpp

📁 推箱子游戏源码
💻 CPP
字号:
#include "StdAfx.h"
#include "Manager.h"

#include <mmsystem.h>
#pragma comment(lib,"winmm.lib")

Manager::Manager()
{
	target=NULL;
	index=1;
	gamestate=0;
}

Manager::~Manager()
{
	if(target!=NULL)
	{
		delete target;
	}
}

bool Manager::CheckState() //是否完成任务
{
	bool finish=true;
	for(int i=0;i<this->num;i++)
	{
		if(map[this->target[i].y][this->target[i].x]==2)
		{
			this->target[i].status=1;
		}
		else
		{
			this->target[i].status=0;
			finish=false;
		}
	}
	return finish;
}

bool Manager::Empty(int a,int b) //是否为背景
{
	return map[a][b]==0?true:false;
}

void Manager::GameStart()
{
	this->GetMap();
	this->Init();
	playSnd();
	gamestate=1;
}

void Manager:: playSnd() 
{
	char path[200];
	::GetModuleFileName(NULL,path,200);
	(::_tcsrchr(path,'\\'))[1]='\0';
	strcat(path,"\\sound\\1.wav");
	::PlaySound(path,NULL,SND_LOOP|SND_ASYNC|SND_NODEFAULT);
}

bool Manager::MoveBox(int x,int y,int action) //是否可以搬运箱子,并且更新数据
{
	int i=x,j=y;
	if(action==1)
	{
		j--;
	}
	else if(action==2)
	{
		i--;
	}
	else if(action==3)
	{
		j++;
	}
	else 
	{
		i++;
	}
	if(Empty(i,j))
	{
		map[i][j]=2;
		map[x][y]=0;
		return true;
	}
	else
	{
		return false;
	}
}

bool Manager::DoMsg(int action) //处理键盘事件
{
	if(action==37)
	{
		if(this->Empty(man.y,man.x-1))
		{
			man.x-=1;
			return true;
		}
		else if(this->IsBox(man.y,man.x-1))
		{
			if(this->MoveBox(man.y,man.x-1,1))
			{
				man.x-=1;
				return true;
			}
		}
	}
	else if(action==38)
	{
		if(this->Empty(man.y-1,man.x))
		{
			man.y-=1;
			return true;
		}
		else if(this->IsBox(man.y-1,man.x))
		{
			if(this->MoveBox(man.y-1,man.x,2))
			{
				man.y-=1;
				return true;
			}
		}
	}
	else if(action==39)
	{
		if(this->Empty(man.y,man.x+1))
		{
			man.x+=1;
			return true;
		}
		else if(this->IsBox(man.y,man.x+1))
		{
			if(this->MoveBox(man.y,man.x+1,3))
			{
				man.x+=1;
				return true;
			}
		}
	}
	else if(action==40)
	{
		if(this->Empty(man.y+1,man.x))
		{
			man.y+=1;
			return true;
		}
		else if(this->IsBox(man.y+1,man.x))
		{
			if(this->MoveBox(man.y+1,man.x,4))
			{
				man.y+=1;
				return true;
			}
		}
	}
	return false;
}

bool Manager::IsBox(int a,int b) //是否为箱子
{
	return map[a][b]==2?true:false;
}

void Manager::Init() //根据从文件读取的地图初始化数据
{
	int number=0;
	for(int i=0;i<CY;i++)
	{
		for(int j=0;j<CX;j++)
		{
			if(map[i][j]==3||map[i][j]==5) //3是目标,5是完成的箱子
			{
				number++;	
			}
		}
	}
	num=number;
	target=new Target[num];
	number=0;
	for(i=0;i<CY;i++)
	{
		for(int j=0;j<CX;j++)
		{
			if(map[i][j]==3) 
			{
				target[number].x=j;
				target[number].y=i;
				target[number].status=0;
				number++;
				map[i][j]=0;
			}
			else if(map[i][j]==5)
			{
				target[number].x=j;
				target[number].y=i;
				target[number].status=1;
				number++;
				map[i][j]=2;
			}
			else if(map[i][j]==4) //人物
			{
				man.x=j;
				man.y=i;
				map[i][j]=0;
			}
		}
	}
}

void Manager::GetMap() //从文件读取的地图,并初始化其它数据
{
	mm.GetMap(map,index);
}

⌨️ 快捷键说明

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