C语言实现推箱子游戏,用函数,这不是函数实现,我以前写的,不知道怎么把函数用到里面来做

#include
#include
#include
#define KD 0
#define ZA 1
#define XZ 2
#define ZD 3
#define KU 4
#define W 10
#define H 10
#define S (W * H)
void main()
{
srand(unsigned int(time(0)));
/*bool pos =true;*/
int HeroPos = 13;

int Map[S]={};
int State = 0;//0开始状态,1游戏运行状态,2游戏退出
int pos = 0;//关卡
int map1[S]={
    1,1,1,1,1,1,1,1,1,1,
    1,0,0,0,0,0,0,0,0,1,
    1,0,0,0,0,0,0,0,0,1,
    1,0,0,0,2,3,0,0,0,1,
    1,0,0,0,0,0,0,0,0,1,
    1,0,0,2,0,0,0,0,0,1,
    1,0,0,3,0,0,0,0,0,1,
    1,0,0,0,0,0,0,0,0,1,
    1,0,0,0,0,0,0,0,0,1,
    1,1,1,1,1,1,1,1,1,1
};
int map2[S] =   
{
    1,1,1,1,1,1,1,1,1,1,
    1,0,0,0,0,0,0,0,0,1,
    1,0,0,2,3,0,0,0,0,1,
    1,0,0,0,2,3,0,0,0,1,
    1,0,0,0,0,1,0,0,0,1,
    1,0,0,2,3,1,0,0,0,1,
    1,0,0,0,0,0,0,0,0,1,
    1,0,0,0,0,0,0,0,0,1,
    1,0,0,0,0,0,0,0,0,1,
    1,1,1,1,1,1,1,1,1,1
};
int map3[S] =   
{
    1,1,1,1,1,1,1,1,1,1,
    1,0,0,0,0,0,0,0,0,1,
    1,0,0,2,3,0,0,0,0,1,
    1,0,0,0,2,3,0,0,0,1,
    1,0,0,0,0,0,0,0,0,1,
    1,0,0,2,3,0,0,0,0,1,
    1,0,0,0,0,0,0,0,0,1,
    1,0,0,0,0,0,0,0,0,1,
    1,0,0,0,0,0,0,0,0,1,
    1,1,1,1,1,1,1,1,1,1
};
for(int i = 0;i < S; ++i)
{
    Map[i] = map1[i];
}

while(true)
{
    if( 0 == State)
    {
        for(int i = 0;i < S; ++i)
        {
            if(pos > 2 || pos < 0)
            {
                pos=0;
            }
            if(pos==0)
            {
                Map[i] = map2[i];
            }
            else if(pos==1)
            {
                Map[i] = map3[i];
            }
            else if(pos==2)
            {
                Map[i] = map1[i];
            }
        }
        /*  switch(pos)
        {
        case 0:HeroPos = 11;break;
        case 1:HeroPos = 11;break;
        case 2:HeroPos = 11;break;
        }*/
        do
        {
            HeroPos = rand() % S;
        }while(Map[HeroPos] != KD);
        State = 1;
    }
    else if(1 == State)
    {

        system("cls");
        for(int i = 0;i < S;++i)
        {

            if(i == HeroPos)
            {
                switch(Map[i])
                {
                case KD:std::cout<<"人";break;
                case ZD:std::cout<<"囚";break;
                }
            }
            else
            {
                switch(Map[i])
                {
                case KD:std::cout<<"  ";break;
                case ZA:std::cout<<"■";break;
                case XZ:std::cout<<"木";break;
                case ZD:std::cout<<"□";break;
                case KU:std::cout<<"困";break;
                }
            }

            if(i % W == W - 1)
                std::cout<<'\n';


        }
        bool Win = true;
        for(int i = 0;i < S; ++i)//budong
        {
            if(Map[i] == XZ)
            {
                Win = false;
                break;
            }
        }
        if(Win)
        {

            std::cout<<"Win!"<<std::endl;//budong
            pos++;
            State = 0;

        }
        int input = _getch();
        int x = HeroPos % W;
        int y = HeroPos / W;
        int x1 = W,y1 = H,x2 = W,y2 = H;
        if(input=='w'||input=='W')
        {
            x1 = x2 = x;
            y1 = y - 1;
            y2 = y - 2;
        }
        else if(input=='s'||input=='S')
        {
            x1 = x2 = x;
            y1 = y + 1;
            y2 = y + 2;
        }
        else if(input=='a'||input=='A')
        {
            y1 = y2 = y;
            x1 = x - 1;
            x2 = x - 2;
        }
        else if(input=='d'||input=='D')
        {

            y1 = y2 = y;
            x1 = x + 1;
            x2 = x + 2;
        }
        else if(input=='e'||input=='E')
        {

            break;
        }
        else if(input=='q'||input=='Q')//budongs
        {
            if(pos==1)
            {
                for(int i = 0;i < S; ++i)
                {
                    Map[i] = map1[i];
                    break;
                }
            }
        }
        else if(input=='r'||input=='R')
        {
            State = 0;
            HeroPos = 13;
        }
        if(x1 >= 0 && x1 <= W - 1 && y1 >= 0 && y1 <= H - 1 &&
            x2 >= 0 && x2 <= W - 1 && y2 >= 0 && y2 <= H - 1)
        {
            switch(Map[x1 + y1 * W])
            {
            case KD:HeroPos = x1 + y1 * W;break;
            case ZA:;break;
            case XZ:
                {
                    switch(Map[x2 + y2 * W])
                    {
                    case KD:
                        {
                            HeroPos = x1 + y1 * W;
                            Map[x2 + y2 * W] = XZ;
                            Map[x1 + y1 * W] = KD;
                        }break;
                    case ZA:;break;
                    case XZ:;break;
                    case ZD:    
                        {
                            HeroPos = x1 + y1 * W;
                            Map[x2 + y2 * W] = KU;
                            Map[x1 + y1 * W] = KD;

                        }break;
                    case KU:
                        ;break;
                    }
                }break;
            case ZD:HeroPos = x1 + y1 * W;break;
            case KU:
                {
                    HeroPos = x1 + y1 * W;
                    Map[x2 + y2 * W] = XZ;
                    Map[x1 + y1 * W] = ZD;
                };break;
            }
        }

    }
    else 
    {
        break;
    }
}
system("pause");

}

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐