当前位置: 移动技术网 > IT编程>开发语言>C/C++ > 用C语言、C++编写一个迷宫游戏(代码实例)

用C语言、C++编写一个迷宫游戏(代码实例)

2018年09月14日  | 移动技术网IT编程  | 我要评论

无中生有是什么计,huangpianyiji,flash player 11.4

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

程序是在vc++6.0中写的,新建一个win32 application即可。

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

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

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

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

//全局变量

#ifndef _global_var_

#define _global_var_

#include "mazeclass.h"

class cmaze *game = null; //迷宫类对象指针

class croom **map; //游戏map指针

hwnd hwndmain; //主窗口句柄

hbitmap h_membm = null; //内存位图

hdc h_memdc = null; //内存dc

rect rcliret; //游戏窗口大小

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

#endif

//////////////////linklist.h///////////////////////

#ifndef _link_list_h

#define _link_list_h

#include

template

class node

{

private:

node *nextptr;

public:

t data;

node();

~node();

node *next() const;

node(const t &item, node *ptrnext=null);

void setnext(node *ptrnext=null);

};

template

class linklist

{

private:

node *head;

node *tail;

node *curr;

node *getnode(const t &item, node *ptrnext=null);

void freenode(node *p);

int size;

public:

linklist();

~linklist();

node *next();

node *current();

node *first();

node *last();

node *findnode(int index);

node *findnode(const t &item); //此功能需要模板类重载等于运算符

void reset();

int getsize();

int insertfront(const t &item);

int inserttail(const t &item);

int insertafter(const t &item, int index);

int deletefront(t *item);

int deletetail(t *item);

int deletenode(t *item, int index);

void clear();

bool isempty();

};

#endif

//////////////mazeclass.h///////////////////////////

#ifndef _maze_class_h

#define _maze_class_h

#include

#include

#include

#include "linklist.h"

//引入外部变量

extern hwnd hwndmain; // 主窗口句柄

extern hbitmap h_membm; // 内存位图

extern hdc h_memdc; // 内存dc

extern rect rcliret; // 窗口大小

extern class cmaze *game; // 迷宫类指针

extern class croom **map; //游戏map指针

extern hbrush hbrushes[10]; //绘制路径画刷

#define right 0 //向右

#define down 1 //向下

#define left 2 //向左

#define up 3 //向后

#define nodir 4

#define left_wall 0 //左边墙壁

#define up_wall 1 //上面墙壁

#define is_wall 0 //有墙壁

#define no_wall 1 //没有墙壁

#define has_deal 0 //map处理状态,已经处理

#define in_deal 1 //正在处理

#define not_deal 2 //未处理

#define step 12 //搜索步长

#define auto_mode 0 //玩家类型电脑自动

#define play_mode 1 //手动

#define init 0 //玩家状态初始化

#define going 1 //运行

#define stop 2 //暂停

#define over 3 //结束

#define idt_timer1 1 // 定时器编号

#define idt_timer2 2

//坐标点类

class point

{

public:

int x;

int y;

point();

~point();

point(int x, int y);

point(const point &p);

point &operator =(const point &p);

bool operator ==(const point &p);

};

//room类,生成迷宫用到

class croom

{

friend class cmaze;

private:

point pos;

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

int upstatus; //0:可以通过 1:不可以通过

int leftstatus; //0:可以通过 1:不可以通过

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

};

//迷宫墙壁类

class cwall

{

friend class cmaze;

private:

point pos; //墙壁位置

int dir; //方向 1:横 0:竖

public:

cwall();

cwall(const point &pos, const int &dir);

cwall(const cwall &p);

~cwall();

cwall &operator =(const cwall &p);

bool operator ==(const cwall &p);

};

class cboard

{

protected:

hbrush hbrush; //绘制背景画刷

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

point m_pos, m_entry, m_exit;

int m_size, m_cell, m_nx, m_ny;

int m_mode, m_speed, m_status;

void fillcell(point pos, int color, int dir);

void displaytext(point p, char *text);

public:

cboard();

~cboard();

int getcell();

int getmode();

int getspeed();

int getstatus();

int getsize();

point getpos();

point getentry();

point getexit();

void messagebox(char *errmsg, char *title, int style);

void settimer(int id, int time);

void killtimer(int id);

};

//路径节点类

class cpathnode

{

friend class cplayer;

private:

int index; //节点在链表中编号

int indir; //最初进去方向

int outdir; //最后一次出去方向

class point pos; //节点位置

public:

cpathnode();

cpathnode(point pos, int index, int indir, int outdir);

~cpathnode();

cpathnode(const cpathnode &p);

cpathnode &operator =(const cpathnode &p); //重载赋值运算符

bool operator ==(const cpathnode &p); //重载等于号

};

//游戏玩家类

class cplayer: public cboard

{

private:

int curdir; //当前的移动方向

int steps; //当前步数

time_t usedtime, stoptime; //所有时间用时

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

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

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

int canmove();

int dooper();

int autogo();

void errout(char *errmsg);

void showinfo();

void drawpath();

public:

cplayer(cmaze *maze);

~cplayer();

void onkeydown(unsigned short key);

void ontimer(int id);

};

class cmaze: public cboard

{

private:

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

class cplayer *m_player;

void drawwall(class cwall wall);

int createwalllist(linklist *list);

int chgroomstatus(point pos, linklist *list, int dir, int depth);

int createmaze();

void drawmaze();

void showtime();

void showhelpinfo();

void initbkgnd();

int newgame();

void gameover();

public:

cmaze();

~cmaze();

void dialogbox(hinstance hinst, lpctstr idd, dlgproc myfunc);

int oninit();

void initdlg(hwnd hdlg);

void ondlgok(hwnd hdlg);

void ontimer(int id); //游戏主控函数

void onkeydown(unsigned short key); //游戏主控函数

void onpaint(hdc hdc);

linklist *getwalllist();

};

#endif

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

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

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

#include

#include

#include

#include

#include

#include

#define m 50000 /* 迷宫最大墙壁数 */

#define max_node_number 50000 /* 最大分配内存节点 */

#define maxgrade 150 /* 游戏最大难度 */

#define idt_timer1 1 /* 定时器编号 */

#define idt_timer2 2

#define step 16 /* 搜索步长 */

#define right 0

#define down 1

#define left 2

#define up 3

#define auto_mode 0

#define play_mode 1

#define up_wall 0

#define left_wall 1

#define init 0

#define going 1

#define stop 3

#define over 4

struct position

{

int x;

int y;

};

struct gameinfo

{

struct position pos; /* 背景位置坐标 */

int size; /* 大小 */

int timer;

int nx;

int ny;

int cell;

int mode;

int mazemode;

int speed;

int times;

int status;

}game;

struct mazewall

{

struct position pos;

int diretcion; /* 方向 0:横 1:竖 */

};

struct mazecfg

{

struct mazewall data[m]; /* 墙壁坐标数据 */

struct position entry, exit; /* 入口和出口位置 */

int num; /* 墙壁个数 */

}maze;

struct mapcfg

{

struct position pos;

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

int upstatus; /* 0:可以通过 1:不可以通过 */

int leftstatus; /* 0:可以通过 1:不可以通过 */

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

};

struct mapcfg **map;

struct travinfo

{

struct position pos;

struct travinfo *next;

};

struct roadinfo

{

int index; /* 编号 */

int indir; /* 进去方向 */

int outdir; /* 出去方向 */

struct position pos;

struct roadinfo *next;

};

struct playercfg

{

int direction;

int steps;

time_t usedtime, endtime, stoptime;

struct roadinfo *roadptr, *errptr;

}play;

hwnd hwndmain; /* 主窗口句柄 */

hbitmap h_membm = null; /* 内存位图 */

hdc h_memdc = null; /* 内存dc */

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

hbrush hbrushbkgnd; /* 背景色画刷 */

hpen hpenborder; /* 绘制边框线的画笔 */

int initgame(hwnd hwnd);

int markpath(int x, int y, int dir, int color);

int drawwall(int x, int y, int dir);

int drawmaze();

int readmazedata(int *num, int id);

char *getfldstr(char *sfldstr, char *sstr, char sfld);

void initmalloc();

void addmallocnode(char *);

void freemallocnode();

void *gccalloc(size_t , size_t);

int initbkgnd();

int createmaze();

int getwallcfg();

int setdirstatus(struct position pos, struct travinfo *head, int dir, int depth);

int createplayer(int mode);

int destroyplayer();

int canmove();

int dooperation();

int havepathed(struct roadinfo node);

int newgame(int type, int mode);

void drawpath();

void showtime();

int gameover();

int autogo(int num);

void displaytext(int posx, int posy, char *text);

int initgameset(hwnd hdlg);

int chggamestatus();

void showusedtime();

void showplayinfo();

void errout(char *errmsg)

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网