C++实现推箱子游戏
一、项目简介
用两天闲余时间回顾了推箱子这款经典的小游戏,目前设置了5关,只能实现基本的人物移动。判断胜利条件,其他功能还未实现(例:撤回到上一步,自由选择关卡等),也顺便复习了C++的相关知识。
二、代码区
ClassMap(地图类)
Map.h:
#pragmaonce
#defineN10
#defineM10
//地图类
classMap
{
public:
Map();
~Map();
voidInit();
voidReadMapFile(intmap[M][N],intsize,constchar*filename);
voidWriteMapFile(intmap[M][N],intsize,constchar*filename);
private:
};
Map.cpp:
#include"Map.h" #include#include usingnamespacestd; Map::Map() { } //地图初始化方法 voidMap::Init() { intMap[10][10]= { {1,1,1,1,1,1,1,1,1,1}, {1,1,1,1,1,1,1,1,1,1}, {1,1,1,1,1,1,1,1,1,1}, {1,0,0,4,3,0,1,1,1,1}, {1,0,4,3,4,3,0,0,1,1}, {1,7,3,4,3,4,2,0,1,1}, {1,0,4,3,4,3,0,1,1,1}, {1,0,0,4,3,0,0,1,1,1}, {1,1,1,1,1,1,1,1,1,1}, {1,1,1,1,1,1,1,1,1,1}, }; WriteMapFile(Map,10,"map/map_05.txt"); } //读取地图文件 voidMap::ReadMapFile(intmap[M][N],intsize,constchar*filename) { FILE*pfile=nullptr; fopen_s(&pfile,filename,"rb"); fread(map,10*size*4,1,pfile); fclose(pfile); } //写入地图文件 voidMap::WriteMapFile(intmap[M][N],intsize,constchar*filename) { FILE*pfile=nullptr; fopen_s(&pfile,filename,"wb"); fwrite(map,10*size*4,1,pfile); fclose(pfile); } Map::~Map() { }
ClassGame(游戏类)
Game.h:
#define_GAEM_H__ #ifdef_GAEM_H__ #includeusingnamespacestd; #include #include #pragmawarning(disable:4996) #defineN10 #defineM10 /***************************建立一个推箱子相关操作的类***********************/ /*--------------------------Game类编写-----------------------------------*/ /****************************************************************************/ classGame { public: intMove(intmap[M][N],charch); voidDrop(intmap[M][N],intc); intjuide(intmap[M][N]); private: intpush(intmap[M][N],intoffsetX,intoffsetY); voidPostion(intmap[M][N]); intposX; intposY; }; #endif/*_GAME_H__*/
Game.cpp:
#include"Game.h"
//按键控制人物移动
intGame::Move(intmap[M][N],charch)
{
staticintstep=0;
intoffsetx=0;
intoffsety=0;
switch(ch)
{
//向上移动
case'w':case'W':
offsetx=-1;
offsety=0;
if(push(map,offsetx,offsety)==1)
step++;
break;
//向下移动
case's':case'S':
offsetx=1;
offsety=0;
if(push(map,offsetx,offsety)==1)
step++;
break;
//向左移动
case'a':case'A':
offsetx=0;
offsety=-1;
if(push(map,offsetx,offsety)==1)
step++;
break;
//向右移动
case'd':case'D':
offsetx=0;
offsety=1;
if(push(map,offsetx,offsety)==1)
step++;
break;
default:
break;
}
returnstep;
}
//界面打印
voidGame::Drop(intmap[M][N],intc)
{
cout<<"\t\t"<<"**********************第"<
Main:
#include
#include
usingnamespacestd;
#pragmawarning(disable:4996)
#defineM10
#defineN10
//定义一个10*10地图,1表示墙,0表示空地,2表示人
//3表示箱子,4表示成功点
//1.人物可以站到成功点中,显示人
//2.箱子推入成功点后,可以推出来
//3.记录步数,显示在控制台上
//4.界面:提示(■代表墙....)/游戏开始界面
//5.最终提示全部推入,提示成功
//周围都是墙,中间都是空地
#include"Map.h"
#include"Game.h"
intmain()
{
Map_map;
//_map.Init();
intmap[M][N];
charfilename[]="map/map_0";
intcustom=2;
while(custom<=5)
{
charbuffer[80];
sprintf(buffer,"%s%d",filename,custom);//连接filename和custom,以字符串保存到buffer中
strcat(buffer,".txt");//字符串连接
_map.ReadMapFile(map,N,buffer);
Gamegame;
intstep=0;
while(game.juide(map))//游戏胜利,跳出循环
{
system("cls");
game.Drop(map,custom);
charch=_getch();//按键输入
step=game.Move(map,ch);
system("cls");
}
custom++;//关卡+1
cout<<"你赢了!"<
三、实现效果
项目目录图片
地图文件图片
实现效果
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。