推箱子代码c/c++

花半个小时左右写的推箱子简易版代码,里面的代码可能没有大幅度优化,但对于初学者来说应该不难理解。增加关卡只要扩容三维数组就可以了。
推箱子代码c/c++_第1张图片

#include
#include
#include
#include
#include
#define KEY_UP 72
#define KEY_DOWN 80
#define KEY_LEFT 75
#define KEY_RIGHT 77
const int MAX_LEVEL = 2;
using namespace std;
int level = 1; //目前关卡
int step = 0 , cs_pushBox = 0, PosNum = 0;
//分别是总走的步数,推箱子的总次数,现在箱子的数目
char keyCode;
int map[MAX_LEVEL][14][16]
{
    {
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0},
        {0,0,0,1,4,0,0,1,0,0,0,0,0,0,0,0},
        {0,0,0,1,0,3,3,1,0,1,1,1,0,0,0,0},
        {0,0,0,1,0,3,0,1,0,1,2,1,0,0,0,0},
        {0,0,0,1,1,1,0,1,1,1,2,1,0,0,0,0},
        {0,0,0,0,1,1,0,0,0,0,2,1,0,0,0,0},
        {0,0,0,0,1,0,0,0,1,0,0,1,0,0,0,0},
        {0,0,0,0,1,0,0,0,1,1,1,1,0,0,0,0},
        {0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
    },
};
/*------------函数分界线--------------*/
void gotoxy(int x,int y); //设置控制台光标的位置
void color(unsigned int c); //设置控制台输出文本的颜色
void IScursor();   //隐藏控制台光标
void DrawMap();    //描绘游戏主地图
void FindPos();    //找到主角在地图中的位置
void KeyMove(int i,int j,int k,int people);//实现游戏逻辑,主角移动功能
void WinGame()  //游戏胜利操作
/*------------------------------------*/
void gotoxy(int x,int y)
{
    COORD app;
    app.X = x; app.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),app);
}
void color(unsigned int c)
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),c);
}
void IScursor()
{
    CONSOLE_CURSOR_INFO c;
    GetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&c);
    c.bVisible = 0;
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&c);
}

void DrawMap()
{
    for(int i=0;i<MAX_LEVEL;++i)
    {
        if(i == level - 1)
        {
            for(int j=0;j<14;++j)
            {
                for(int k = 0;k<16;++k)
                {
                    if(map[i][j][k]==1)
                    {
                        color(14);
                        gotoxy(k*2,j);
                        printf("■");
                        continue;
                    }
                    if(map[i][j][k]==0)
                    {
                        color(14);
                        gotoxy(k*2,j);
                        printf("  ");
                        continue;
                    }
                    if(map[i][j][k]==2)
                    {
                        color(7);
                        gotoxy(k*2,j);
                        printf("☆");
                        continue;
                    }
                    if(map[i][j][k]==3)
                    {
                        color(10);
                        gotoxy(k*2,j);
                        printf("□");
                        ++PosNum;
                        continue;
                    }
                    if(map[i][j][k]==4)
                    {
                        color(4);
                        gotoxy(k*2,j);
                        printf("♀");
                        continue;
                    }
                    if(map[i][j][k]==5)
                    {
                        color(3);
                        gotoxy(k*2,j);
                        printf("★");
                        continue;
                    }
                    if(map[i][j][k]==6)
                    {
                        color(3);
                        gotoxy(k*2,j);
                        printf("* ");
                        continue;
                    }
                }
            }
        }
    }
}
void KeyMove(int i,int j,int k,int people)
{
    switch(keyCode)
    {
        case  KEY_LEFT://方向左键
            if(map[i][j][k-1] == 0)//人左边是空地
            {
                map[i][j][k] = (people==4)?0:2;
                map[i][j][k-1] = 4;
            }
            else if(map[i][j][k-1] == 3 )//人左边是空箱子
            {
                if(map[i][j][k-2] == 0)//空箱子左边是空地
                {
                    map[i][j][k] = (people==4)?0:2;
                    map[i][j][k-1] = 4;
                    map[i][j][k-2] = 3;
                }
                else if(map[i][j][k-2] == 2)//空箱子左边是目标
                {
                    map[i][j][k] = (people==4)?0:2;
                    map[i][j][k-1] = 4;
                    map[i][j][k-2] = 5;
                }
            }
            else if(map[i][j][k-1] == 5 )//人左边是到达目标的箱子
            {
                if(map[i][j][k-2] == 0)//到达目标的箱子的坐标是空地
                {
                    map[i][j][k] = (people==4)?0:2;
                    map[i][j][k-1] = 6;
                    map[i][j][k-2] = 3;
                }
                else if(map[i][j][k-2] == 2)//到达目标的箱子的左边是空目标
                {
                    map[i][j][k] = (people==4)?0:2;
                    map[i][j][k-1] = 6;
                    map[i][j][k-2] = 5;
                }
            }
            else if(map[i][j][k-1] == 2)//人左边是空目标
            {
                map[i][j][k] = (people==4)?0:2;
                map[i][j][k-1] = 6;
            }
            break;
        case  KEY_RIGHT://方向右键
            if(map[i][j][k+1] == 0)
            {
                map[i][j][k] = (people==4)?0:2;
                map[i][j][k+1] = 4;
            }
            else if(map[i][j][k+1] == 3 )
            {
                if(map[i][j][k+2] == 0)
                {
                    map[i][j][k] = (people==4)?0:2;
                    map[i][j][k+1] = 4;
                    map[i][j][k+2] = 3;
                }
                else if(map[i][j][k+2] == 2)
                {
                    map[i][j][k] = (people==4)?0:2;
                    map[i][j][k+1] = 4;
                    map[i][j][k+2] = 5;
                }
            }
            else if(map[i][j][k+1] == 5 )
            {
                if(map[i][j][k+2] == 0)
                {
                    map[i][j][k] = (people==4)?0:2;
                    map[i][j][k+1] = 6;
                    map[i][j][k+2] = 3;
                }
                else if(map[i][j][k+2] == 2)
                {
                    map[i][j][k] = (people==4)?0:2;
                    map[i][j][k+1] = 6;
                    map[i][j][k+2] = 5;
                }
            }
            else if(map[i][j][k+1] == 2)
            {
                map[i][j][k] = (people==4)?0:2;
                map[i][j][k+1] = 6;
            }
            break;
        case  KEY_DOWN: //方向下键
            if(map[i][j+1][k]==0)
            {
                map[i][j][k] = (people==4)?0:2;
                map[i][j+1][k]=4;
            }
            else if(map[i][j+1][k] == 3)
            {
                if(map[i][j+2][k] == 0)
                {
                    map[i][j][k] = (people==4)?0:2;
                    map[i][j+1][k] = 4;
                    map[i][j+2][k] = 3;
                }
                else if(map[i][j+2][k] == 2)
                {
                    map[i][j][k] = (people==4)?0:2;
                    map[i][j+1][k] = 4;
                    map[i][j+2][k] = 5;
                }
            }
            else if(map[i][j+1][k] == 5 )
            {
                if(map[i][j+2][k] == 0)
                {
                    map[i][j][k] = (people==4)?0:2;
                    map[i][j+1][k] = 6;
                    map[i][j+2][k] = 3;
                }
                else if(map[i][j+2][k] == 2)
                {
                    map[i][j][k] = (people==4)?0:2;
                    map[i][j+1][k] = 6;
                    map[i][j+2][k] = 5;
                }
            }
            else if(map[i][j+1][k] == 2)
            {
                map[i][j][k] = (people==4)?0:2;
                map[i][j+1][k] = 6;
            }
            break;
        case  KEY_UP://方向上键
            if(map[i][j-1][k] == 0)
            {
                map[i][j][k] = (people==4)?0:2;
                map[i][j-1][k] = 4;
            }
            else if(map[i][j-1][k] == 3)
            {
                cout<<"1"<<endl;
                if(map[i][j-2][k] == 0)
                {
                    map[i][j][k] = (people==4)?0:2;
                    map[i][j-1][k] = 4;
                    map[i][j-2][k] = 3;
                }
                else if(map[i][j-2][k] == 2)
                {
                    map[i][j][k] = (people==4)?0:2;
                    map[i][j-1][k] = 4;
                    map[i][j-2][k] = 5;
                }
            }
            else if(map[i][j-1][k] == 5 )
            {
                if(map[i][j-2][k] == 0)
                {
                    map[i][j][k] = (people==4)?0:2;
                    map[i][j-1][k] = 6;
                    map[i][j-2][k] = 3;
                }
                else if(map[i][j-2][k] == 2)
                {
                    map[i][j][k]=(people==4)?0:2;
                    map[i][j-1][k] = 6;
                    map[i][j-2][k] = 5;
                }
            }
            else if(map[i][j-1][k] == 2)
            {
                map[i][j][k] = (people==4)?0:2;
                map[i][j-1][k] = 6;
            }
            break;
    }
}
void FindPos()
{
    for(int i=0;i<MAX_LEVEL;++i)
    {
        if(i == level - 1)
        {
            for(int j=0;j<14;++j)
            {
                for(int k = 0;k<16;++k)
                {
                    if(map[i][j][k] == 4 || map[i][j][k] == 6)
                    {
                        KeyMove(i,j,k,map[i][j][k]);
                        return;
                    }
                }
            }
        }
    }
}
void WinGame()
{
    system("cls");
    gotoxy(100,100);
    printf("你胜利了!");
    system("pause");
}
int main(void)
{
    system("mode con cols=60 lines=20");//设置控制台窗口宽cols高lines
    IScursor();
    while(1)
    {
        DrawMap();
        if(PosNum == 0)
        {
            WinGame();
            break;
        }
        keyCode = _getch();
        FindPos();
        PosNum = 0;  //每次判断完后对PosNum初始化
    }
}

有问题欢迎一起讨论_

你可能感兴趣的:(游戏开发,c++)