| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 520 人关注过本帖
标题:我的迷宫游戏程序,大家看看有没有简化的地方
只看楼主 加入收藏
navy_jia
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2014-4-25
结帖率:100%
收藏
 问题点数:0 回复次数:1 
我的迷宫游戏程序,大家看看有没有简化的地方
};[local]1[/local]
搜索更多相关主题的帖子: 游戏程序 
2014-05-04 17:55
navy_jia
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2014-4-25
收藏
得分:0 
#include <iostream>
#include <windows.h>
#include <conio.h>
#include <string.h>
#include <fstream>
using namespace std;
class ConstNum{//常量定义,用以标注各项属性
    public:
        enum Constants
        {

         AREA=0x0,
         WALL=0x1,
         MOUSE=0x2,
         INWAY=0x13,
         OUTWAY=0x14,
         UP=0x48,
         DOWN=0x50,
         LEFT=0x4B,
         RIGHT=0x4D
        };
};
class Map{//描述地图的各项状态
    private :
        int map[800][800];
    public  :
        int n;int m;
        int getMapValue(int x,int y)//获取地图标志
        {
            return map[x][y];
        }
        void setMapValue(int x,int y,int value)//设置地图标志
        {
            map[x][y]=value;
        }
        bool can(int x,int y)//判断是否可行
        {
            if (x<1||x>n) return false;
            if (y<1||y>m) return false;
            if (map[x][y]==ConstNum::WALL) return false;
            return true;
        }
        void clearMap()//清空地图
        {
            int i,j;
            for (i=0;i<=n;i++)
                for (j=0;j<=m;j++)
                map[i][j]=ConstNum::WALL;
        }
};
class Maze//描述迷宫
{
    private:
          int dx[4];
          int dy[4];
         int myX;int myY;
         int outX;int outY;
         Map map;
public:
    Maze()
    {
        dx[0]=-1;dy[0]=0;
        dx[1]=0;dy[1]=-1;
        dx[2]=1;dy[2]=0;
        dx[3]=0;dy[3]=1;
        myX=-1;myY=-1;
        outX=-2;outY=-2;
    }
        void drawMap()//绘制地图
        {
            
            int i,j,x;
            int n=map.n;
            int m=map.m;
            system("cls");//清空界面
            for (i=1;i<=n;i++)
            {
                for (j=1;j<=m;j++)
                {
                    x=map.getMapValue(i,j);
                    switch (x)
                    {
                        case ConstNum::AREA:
                            cout<<"□";
                            break;
                           
                        case ConstNum::WALL:{
                            cout<<"■";
                            break;
                        }
                        case ConstNum::MOUSE:{
                            cout<<"☆";
                            break;
                        }
                        case ConstNum::INWAY:{
                            cout<<"";
                            break;
                        }
                        case ConstNum::OUTWAY:{
                            cout<<"×";
                            break;
                        }
                    }
                }
                cout<<endl;
        
            }            
        }
        void loadMapFromFile(string file)//从文件读取地图
        {
            int i,j,x;
            ifstream fin;
            fin.open("map.txt");
            fin>>map.n>>map.m;
            for (i=1;i<=map.n;i++)
                for (j=1;j<=map.m;j++)
                {
                    fin>>x;
                    map.setMapValue(i,j,x);
                }
            fin.close();
        }
        void move(int x, int y)   
        {
             COORD pos = {y, x};
             SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
        }   
        void drict(int key)//判断按键
        {
                int tx,ty;
                switch (key)
                {
                case (ConstNum::UP):{//处理上下左右不同按键的结果
                    tx=myX+dx[0];
                    ty=myY+dy[0];
                    break;
                }
                case ConstNum::LEFT:{
                    tx=myX+dx[1];
                    ty=myY+dy[1];
                    break;
                }
                case ConstNum::DOWN:{
                    tx=myX+dx[2];
                    ty=myY+dy[2];
                    break;
                }

                case ConstNum::RIGHT:{
                    tx=myX+dx[3];
                    ty=myY+dy[3];
                    break;
                }
                }
                if (map.can(tx,ty))
                {
                    map.setMapValue(myX,myY,ConstNum::AREA);
                    myX=tx;myY=ty;
                    map.setMapValue(tx,ty,ConstNum::MOUSE);
                }
        }
        bool isOut()//判断是否到达终点
        {
            if (myX==outX&& myY==outY) return true;
            return false;
        }
        void begin()
        {
        
            while (1)
            {
                drawMap();//绘制地图
                if (isOut())
                {
                    //printf("恭喜你已走出迷宫!");
                    cout<<"恭喜你已走出迷宫!"<<endl;
                    break;
                }
                drict(_getch());
               
            }
        }
    void setMouse(int x,int y)//设置老鼠初始位置
    {
        myX=x;myY=y;
        map.setMapValue(x,y,ConstNum::MOUSE);
    }
    void setOutWay(int x,int y)//设置出口
    {
        outX=x;outY=y;
        map.setMapValue(x,y,ConstNum::OUTWAY);
    }
};
2014-05-04 18:02
快速回复:我的迷宫游戏程序,大家看看有没有简化的地方
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021444 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved