待定(tomorrow)

待定(tomorrow)今天只是备份一下这个推箱子代码,太累了,明天加注释#include”box.h”intmap[LINE][COLUMN]={{0,0,0,0,0,0,0,0,0,0,0,0},{0,1,0,1,1,1,1,1,1,1,

待定(tomorrow)"

今天只是备份一下这个推箱子代码,太累了,明天加注释

#include "box.h"

int map[LINE][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 },
};

bool isGameOver() { 
   
        for (int i = 0; i < LINE; i++) { 
   
            for (int j = 0; j < COLUMN; j++) { 
   
                if (map[i][j] == BOX_DES) return false;
            }
        }
        return true;
 }

POS man;

    void changeMap(POS* pos, PROPS prop) { 
   
        map[pos->x][pos->y] = prop;
        putimage(START_X + (pos->y * RATIO), START_Y + (pos->x * RATIO), &images[prop]);
    }
    void gameControl(DIRECTION direct) { 
   
        int x = man.x;
        int y = man.y;
        POS next_pos = man;
        POS next_next_pos = man;
        switch (direct)
        { 
   
        case UP:
            next_pos.x--;
            next_next_pos.x -= 2;
            break;
        case DOWN:
            next_pos.x++;
            next_next_pos.x += 2;
            break;
        case LEFT:
            next_pos.y--;
            next_next_pos.y -= 2;
            break;
        case RIGHT:
            next_pos.y++;
            next_next_pos.y += 2;
            break;
        default:
            break;
        }
        if (isVald(next_pos) && map[next_pos.x][next_pos.y] == FLOOR) { 
   
            changeMap(&next_pos, MAN);
            changeMap(&man, FLOOR);
            man = next_pos;
        }
        else if (isVald(next_pos) && map[next_pos.x][next_pos.y] == BOX) { 
   
            if (isVald(next_next_pos) && map[next_next_pos.x][next_next_pos.y] == FLOOR) { 
   
                changeMap(&next_next_pos, BOX);
                changeMap(&next_pos, MAN);
                changeMap(&man, FLOOR);
                man = next_pos;
            }
            if (isVald(next_next_pos) && map[next_next_pos.x][next_next_pos.y] == BOX_DES) { 
   
                changeMap(&next_next_pos, HIT);
                changeMap(&next_pos, MAN);
                changeMap(&man, FLOOR);
                man = next_pos;
            }
        }
    }

int main() { 
   
    IMAGE bg_img;
    //开始搭台
    initgraph(SCREEN_WIDTH, SCREEN_HEIGHT);
    loadimage(&bg_img, _T("blackground.bmp"), SCREEN_WIDTH, SCREEN_HEIGHT, true);
    putimage(0, 0, &bg_img);
    //预加载图片
    loadimage(&images[WALL], _T("wall.bmp"), RATIO, RATIO, true); 
    loadimage(&images[FLOOR], _T("floor.bmp"), RATIO, RATIO, true); 
    loadimage(&images[BOX_DES], _T("des.bmp"), RATIO, RATIO, true); 
    loadimage(&images[MAN], _T("man.bmp"), RATIO, RATIO, true); 
    loadimage(&images[BOX], _T("box.bmp"), RATIO, RATIO, true);
    loadimage(&images[HIT], _T("box.bmp"), RATIO, RATIO, true);
    //开始布置
    for (int i = 0; i < LINE; i++) { 
   
        for (int j = 0; j < COLUMN; j++) { 
   
            if (map[i][j] == MAN) { 
   
                man.x = i;
                man.y = j;
            }
            putimage(START_X + j*RATIO, START_Y + i * RATIO, &images[map[i][j]]);
        }
    }

    //游戏环节
    bool quit = false;
    do { 
   
        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()) { 
   
                showTheEnd(&bg_img);
                quit = true;
            }
            Sleep(100);
        }
    } while (quit == false);
    system("pause");
    closegraph();
    return 0;
}

以及头文件box.h

#pragma once
#include<iostream>
#include<Windows.h>
#include<string>
#include<stdlib.h>
#include<graphics.h>
#include <conio.h>

#define RATIO 61 

#define SCREEN_WIDTH 1190 
#define SCREEN_HEIGHT 800

#define LINE 9 
#define COLUMN 12

#define START_X 235
#define START_Y 130
#define KEY_UP 'w' //char 'a'
#define KEY_LEFT 'a' 
#define KEY_RIGHT 'd' 
#define KEY_DOWN 's'
#define KEY_QUIT 'q' 

#define isVald(pos) pos.x <LINE-1 && pos.y < COLUMN-1&&pos.x>0&&pos.y>0

using namespace std;

typedef     enum _PROPS            PROPS;
typedef     enum _DIRECTION     DIRECTION;
typedef     struct _POS                POS;
typedef     int                              int32;



enum _DIRECTION { 
   
        UP,
        DOWN,
        LEFT,
        RIGHT
 };
enum _PROPS { 
   
        WALL,
        FLOOR,
        BOX_DES,
        MAN,
        BOX,
        HIT,
        ALL
};

IMAGE images[ALL];

   
void showTheEnd(IMAGE* bg) { 
   
        putimage(0, 0, bg);
        settextcolor(YELLOW);
        RECT rec = { 
    0, 0, SCREEN_WIDTH, SCREEN_HEIGHT };
        settextstyle(30, 0, _T("宋体"));
        drawtext(_T("Congratulations!Thank you for playing!\n"), &rec, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
}

struct _POS { 
   
        int x;
        int y;
};
    

今天的文章待定(tomorrow)分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/68417.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注