推箱子

#include <iostream>
#include <graphics.h>
#include <conio.h>
#include <stdlib.h>

using namespace std;

#define SCREEN_WIDTH 960    //初始化屏幕宽度
#define SCREEN_HEIGHT 768   //初始化屏幕高度

#define MAP_LINE 9        
#define MAP_COLUMN 12

#define START_X 100         //地图起始x坐标
#define START_Y 150
#define RATIO 61            //加载游戏道具图片的像素比例

//控制键 上下左右退出
#define KEY_UP 'w'          
#define KEY_DOWN 's'
#define KEY_LEFT 'a'
#define KEY_RIGHT 'd'
#define KEY_QUIT 'q'

//判断位置是否有效
#define isValid(coordinate) coordinate.x>=0 && coordinate.x<MAP_LINE && coordinate.y>=0 && coordinate.y<MAP_COLUMN

int map[MAP_LINE][MAP_COLUMN] = {
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
    { 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0 },
    { 0, 1, 4, 1, 0, 2, 1, 0, 2, 1, 0, 0 },
    { 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0 },
    { 0, 1, 0, 2, 0, 1, 1, 4, 1, 1, 1, 0 },
    { 0, 1, 1, 1, 0, 3, 1, 1, 1, 4, 1, 0 },
    { 0, 1, 2, 1, 1, 4, 1, 1, 1, 1, 1, 0 },
    { 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0 },
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
};

enum _PROPS   //道具
{
    WALL,     //0 墙
    FLOOR,    //1 地板
    BOX_DES,  //2 箱子目的地
    MAN,      //3 小人
    BOX,      //4 箱子
    HIT,      //5 箱子命中目的地
    ALL
};

enum _DIRECTION  //方向
{
    UP,
    DOWN,
    LEFT,
    RIGHT,
    QUIT
};

IMAGE images[ALL];    //用来加载道具资源

struct _COORDINATE    //地图中的坐标
{
    int x;
    int y;
};
struct _COORDINATE man;

//修改地图  参数:coordinate需要修改的位置  prop修改后的道具
void chageMap(struct _COORDINATE* coordinate, enum _PROPS prop)
{
    map[coordinate->x][coordinate->y] = prop;
    putimage(START_X + coordinate->y * RATIO, START_Y + coordinate->x * RATIO, &images[prop]);
}
 
//实现游戏控制
void gameControl(enum _DIRECTION direction)
{
    struct _COORDINATE next = man;
    struct _COORDINATE nextNext = man;
    switch (direction)
    {
    case UP:
        next.x--;
        nextNext.x -= 2;
        break;
    case DOWN:
        next.x++;
        nextNext.x += 2;
        break;
    case LEFT:
        next.y--;
        nextNext.y -= 2;
        break;
    case RIGHT:
        next.y++;
        nextNext.y += 2;
        break;
    }

    //人的前方是地板
    if (isValid(next) && map[next.x][next.y] == FLOOR)
    {
        chageMap(&next, MAN);
        chageMap(&man, FLOOR);
        man = next;
    }
    //人的前方是箱子
    else if (isValid(nextNext) && map[next.x][next.y] == BOX)
    {
        //箱子的前方是地板
        if (map[nextNext.x][nextNext.y] == FLOOR)
        {
            chageMap(&nextNext, BOX);
            chageMap(&next, MAN);
            chageMap(&man, FLOOR);
            man = next;
        }
        //箱子的前方是箱子目的地
        else if (map[nextNext.x][nextNext.y] == BOX_DES)
        {
            chageMap(&nextNext, HIT);
            chageMap(&next, MAN);
            chageMap(&man, FLOOR);
            man = next;
        }
    }
}

//判断游戏是否结束
bool isGameOver()
{
    for (int i = 0; i < MAP_LINE; i++)
    {
        for (int j = 0; j < MAP_COLUMN; j++)
        {
            if (map[i][j] == BOX_DES)
            {
                return false;
            }
        }
    }
    return true;
}
//游戏结束场景
void gameOverScene(IMAGE* bj)
{
    putimage(0, 0, bj);
    settextcolor(WHITE);
    RECT rect = { 0,0,SCREEN_WIDTH ,SCREEN_HEIGHT };
    settextstyle(20, 0, _T("宋体")); 
    drawtext(_T("恭喜您~ \n 您终于成为了一个合格的搬箱子老司机!"), &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
}
int main()
{
    initgraph(SCREEN_WIDTH, SCREEN_HEIGHT);
    //加载背景图
    IMAGE imageBj;
    loadimage(&imageBj,"blackground.bmp", 0, 0, false);
    putimage(0, 0, &imageBj);
    //加载道具图标
    loadimage(&images[0], "wall_right.bmp", RATIO, RATIO, true);
    loadimage(&images[1], "floor.bmp", RATIO, RATIO, true);
    loadimage(&images[2], "des.bmp", RATIO, RATIO, true);
    loadimage(&images[3], "man.bmp", RATIO, RATIO, true);
    loadimage(&images[4], "box.bmp", RATIO, RATIO, true);
    loadimage(&images[5], "box.bmp", RATIO, RATIO, true);
    for (int i = 0; i < MAP_LINE; i++)
    {
        for (int j = 0; j < MAP_COLUMN; j++)
        {
            putimage(START_X + j * RATIO, START_Y + i * RATIO, &images[map[i][j]]);
            if (map[i][j] == MAN)
            {
                man.x = i;
                man.y = j;
            }
        }
    }

    bool quit = false;    //游戏是否退出
    while (!quit)
    {
        if (_kbhit())     //玩家按键
        {
            char ch = _getch();
            if (ch == KEY_UP)
            {
                gameControl(UP);
            }
            else if (ch == KEY_DOWN)
            {
                gameControl(DOWN);
            }
            else if (ch == KEY_LEFT)
            {
                gameControl(LEFT);
            }
            else if (ch == KEY_RIGHT)
            {
                gameControl(RIGHT);
            }
            else if (ch == KEY_QUIT)
            {
                quit = true;
            }
            if (isGameOver())
            {
                gameOverScene(&imageBj);
                quit = true;
            }
        }
    }
    system("pause");
    closegraph();
    return 0;
}

运行结果:
image
推箱子图片素材:链接:https://pan.baidu.com/s/1EqVeXiCZbDsOowCEh_JE9w 提取码:sxf0

posted @ 2022-04-28 17:42  荒年、  阅读(81)  评论(0编辑  收藏  举报