所在的位置: c++ >> 基本简介 >> C语言C编写迷宫游戏

C语言C编写迷宫游戏

      C语言C++编写——迷宫游戏    

最初用C语言写的,写了一天多写好。后来又改用C++,由于对C++不是很熟悉,改成C++用了三四天才改好。

程序是在VC++6.0中写的,新建一个Win32Application即可。

最初觉得程序的关键应该是求解迷宫,后来发现求解迷宫很简单,难点是如何生成迷宫。这里采用接合图的深度优先遍历,生成迷宫的算法,定义了搜索步长以便控制迷宫分支多少和分支深度。由于程序中多处用到链表,所以对于迷宫求解部分也是用链表,没有用栈。代码比较多,就不出了,只列举了C和C++主要全局变量和函数,时间有限本人就不去一一编辑代码了;想要全部代码私聊我      

///////////////////////////////////////////////////

//C++主要变量和函数定义

//////////////////maze.h///////////////////////////

//全局变量

#ifndef_GLOBAL_VAR_

#define_GLOBAL_VAR_

#includeMazeClass.h

classCMaze*game=NULL;//迷宫类对象指针

classCRoom**map;//游戏map指针

HWNDhWndMain;//主窗口句柄

HBITMAPh_MemBm=NULL;//内存位图

HDCh_MemDC=NULL;//内存DC

RECTrCliRet;//游戏窗口大小

HBRUSHhBrushes[10];//绘制方块的画刷

#endif

//////////////////LinkList.h///////////////////////

#ifndef_LINK_LIST_H

#define_LINK_LIST_H

#includeiostream.h

templateclassT

classNode

{

private:

NodeT*nextptr;

public:

Tdata;

Node();

~Node();

NodeT*next()const;

Node(constTitem,NodeT*ptrnext=NULL);

voidsetNext(NodeT*ptrnext=NULL);

};

templateclassT

classLinkList

{

private:

NodeT*head;

NodeT*tail;

NodeT*curr;

NodeT*getNode(constTitem,NodeT*ptrnext=NULL);

voidfreeNode(NodeT*p);

intsize;

public:

LinkList();

~LinkList();

NodeT*next();

NodeT*current();

NodeT*first();

NodeT*last();

NodeT*findNode(intindex);

NodeT*findNode(constTitem);//此功能需要模板类重载等于运算符

voidreset();

intgetSize();

intinsertFront(constTitem);

intinsertTail(constTitem);

intinsertAfter(constTitem,intindex);

intdeleteFront(T*item);

intdeleteTail(T*item);

intdeleteNode(T*item,intindex);

voidclear();

boolisEmpty();

};

#endif

//////////////MazeClass.h///////////////////////////

#ifndef_MAZE_CLASS_H

#define_MAZE_CLASS_H

#includeiostream.h

#includestring.h

#includetime.h

#includeLinkList.h

//引入外部变量

externHWNDhWndMain;//主窗口句柄

externHBITMAPh_MemBm;//内存位图

externHDCh_MemDC;//内存DC

externRECTrCliRet;//窗口大小

externclassCMaze*game;//迷宫类指针

externclassCRoom**map;//游戏map指针

externHBRUSHhBrushes[10];//绘制路径画刷

#defineRIGHT0//向右

#defineDOWN1//向下

#defineLEFT2//向左

#defineUP3//向后

#defineNODIR4

#defineLEFT_WALL0//左边墙壁

#defineUP_WALL1//上面墙壁

#defineIS_WALL0//有墙壁

#defineNO_WALL1//没有墙壁

#defineHAS_DEAL0//map处理状态,已经处理

#defineIN_DEAL1//正在处理

#defineNOT_DEAL2//未处理

#defineSTEP12//搜索步长

#defineAUTO_MODE0//玩家类型电脑自动

#definePLAY_MODE1//手动

#defineINIT0//玩家状态初始化

#defineGOING1//运行

#defineSTOP2//暂停

#defineOVER3//结束

#defineIDT_TIMER11//定时器编号

#defineIDT_TIMER22

//坐标点类

classpoint

{

public:

intx;

inty;

point();

~point();

point(intx,inty);

point(constpointp);

pointoperator=(constpointp);

booloperator==(constpointp);

};

//Room类,生成迷宫用到

classCRoom

{

friendclassCMaze;

private:

pointpos;

intstatus;//0:未访问,1:正在访问,2:已访问

intupstatus;//0:可以通过1:不可以通过

intleftstatus;//0:可以通过1:不可以通过

//每个节点只需判断upleft是否可以通过即可

};

//迷宫墙壁类

classCWall

{

friendclassCMaze;

private:

pointpos;//墙壁位置

intdir;//方向1:横0:竖

public:

CWall();

CWall(constpointpos,constintdir);

CWall(constCWallp);

~CWall();

CWalloperator=(constCWallp);

booloperator==(constCWallp);

};

classCBoard

{

protected:

HBRUSHhBrush;//绘制背景画刷

HPENhPen;//绘制边框线的画笔

pointm_pos,m_entry,m_exit;

intm_size,m_cell,m_nx,m_ny;

intm_mode,m_speed,m_status;

voidfillCell(pointpos,intcolor,intdir);

voiddisplayText(pointp,char*text);

public:

CBoard();

~CBoard();

intgetCell();

intgetMode();

intgetSpeed();

intgetStatus();

intgetSize();

pointgetPos();

pointgetEntry();

pointgetExit();

voidmessageBox(char*errMsg,char*title,intstyle);

voidsetTimer(intid,inttime);

voidkillTimer(intid);

};

//路径节点类

classCPathNode

{

friendclassCPlayer;

private:

intindex;//节点在链表中编号

intindir;//最初进去方向

intoutdir;//最后一次出去方向

classpointpos;//节点位置

public:

CPathNode();

CPathNode(pointpos,intindex,intindir,intoutdir);

~CPathNode();

CPathNode(constCPathNodep);

CPathNodeoperator=(constCPathNodep);//重载赋值运算符

booloperator==(constCPathNodep);//重载等于号

};

//游戏玩家类

classCPlayer:publicCBoard

{

private:

intcurdir;//当前的移动方向

intsteps;//当前步数

time_tusedTime,stopTime;//所有时间用时

LinkListclassCPathNodepath;//保存走过的正确路径链表

LinkListclassCPathNodeerror;//保存走过的错误路劲链表

LinkListclassCWall*mazeptr;//指向当前迷宫的指针

intcanMove();

intdoOper();

intautoGo();

voiderrOut(char*errMsg);

voidshowInfo();

voiddrawPath();

public:

CPlayer(CMaze*maze);

~CPlayer();

voidonKeyDown(unsignedshortkey);

voidonTimer(intid);

};

classCMaze:publicCBoard

{

private:

LinkListclassCWallwlist;//存放迷宫墙壁数据链表

classCPlayer*m_player;

voiddrawWall(classCWallwall);

intcreateWallList(LinkListclasspoint*list);

intchgRoomStatus(pointpos,LinkListclasspoint*list,intdir,intdepth);

intcreateMaze();

voiddrawMaze();

voidshowTime();

voidshowHelpInfo();

voidinitBkGnd();

intnewGame();

voidgameOver();

public:

CMaze();

~CMaze();

voiddialogBox(HINSTANCEhInst,LPCTSTRIDD,DLGPROCmyFunc);

intonInit();

voidinitDlg(HWNDhDlg);

voidonDlgOk(HWNDhDlg);

voidonTimer(intid);//游戏主控函数

voidonKeyDown(unsignedshortkey);//游戏主控函数

voidonPaint(HDChdc);

LinkListclassCWall*getWallList();

};

#endif

////////////////////////////////////////////

//C语言主要变量和函数定义

////////////////maze.h//////////////////////

#includestdio.h

#includestring.h

#includemalloc.h

#includestdlib.h

#includemath.h

#includetime.h

#defineM/*迷宫最大墙壁数*/

#defineMAX_NODE_NUMBER/*最大分配内存节点*/

#defineMAXGRADE/*游戏最大难度*/

#defineIDT_TIMER11/*定时器编号*/

#defineIDT_TIMER22

#defineSTEP16/*搜索步长*/

#defineRIGHT0

#defineDOWN1

#defineLEFT2

#defineUP3

#defineAUTO_MODE0

#definePLAY_MODE1

#defineUP_WALL0

#defineLEFT_WALL1

#defineINIT0

#defineGOING1

#defineSTOP3

#defineOVER4

structposition

{

intx;

inty;

};

structGameInfo

{

structpositionpos;/*背景位置坐标*/

intsize;/*大小*/

inttimer;

intnx;

intny;

intcell;

intmode;

intmazemode;

intspeed;

inttimes;

intstatus;

}game;

structMazeWall

{

structpositionpos;

intdiretcion;/*方向0:横1:竖*/

};

structMazeCfg

{

structMazeWalldata[M];/*墙壁坐标数据*/

structpositionentry,exit;/*入口和出口位置*/

intnum;/*墙壁个数*/

}maze;

structMapCfg

{

structpositionpos;

intstatus;/*0:未访问,1:正在访问,2:已访问*/

intupstatus;/*0:可以通过1:不可以通过*/

intleftstatus;/*0:可以通过1:不可以通过*/

/*每个节点只需判断upleft是否可以通过即可*/

};

structMapCfg**map;

structtravInfo

{

structpositionpos;

structtravInfo*next;

};

structRoadInfo

{

intindex;/*编号*/

intindir;/*进去方向*/

intoutdir;/*出去方向*/

structpositionpos;

structRoadInfo*next;

};

structplayercfg

{

intdirection;

intsteps;

time_tusedTime,endTime,stopTime;

structRoadInfo*RoadPtr,*errPtr;

}play;

HWNDhWndMain;/*主窗口句柄*/

HBITMAPh_MemBm=NULL;/*内存位图*/

HDCh_MemDC=NULL;/*内存DC*/

HBRUSHhBrushes[10];/*绘制方块的画刷*/

HBRUSHhBrushBkGnd;/*背景色画刷*/

HPENhPenBorder;/*绘制边框线的画笔*/

intinitgame(HWNDhWnd);

intmarkpath(intx,inty,intdir,intcolor);

intdrawWall(intx,inty,intdir);

intdrawMaze();

intreadMazedata(int*num,intid);

char*GetFldStr(char*sFldStr,char*sStr,charsFld);

voidinitMalloc();

voidaddMallocNode(char*);

voidfreeMallocNode();

void*gcCalloc(size_t,size_t);

intinitBkGnd();

intcreateMaze();

intgetWallCfg();

intsetDirStatus(structpositionpos,structtravInfo*head,intdir,intdepth);

intcreatePlayer(intmode);

intdestroyPlayer();

intcanMove();

intdoOperation();

inthavePathed(structRoadInfonode);

intnewGame(inttype,intmode);

voiddrawPath();

voidshowTime();

intgameOver();

intautoGo(intnum);

voidDisplayText(intposx,intposy,char*text);

intinitGameSet(HWNDhDlg);

intchgGameStatus();

voidshowUsedTime();

voidshowPlayInfo();

voiderrOut(char*errMsg)

代码可能看着有点晕~~~c/c++问题群里问QQ群







































白癜风怎样治疗好
治白癜风石家庄哪家医院好



转载请注明:http://www.nydjfy.com/jbjj/1084.html