我如何更新基于文本的游戏的帧

时间:2017-12-22 21:14:59

标签: c++ text-based

现在,对于程序员和我试图制作基于文本的游戏而言,我现在只是一半。我今天开始,我做了一些世界地图,后来将更新,我将使它变得庞大!但对于我的生活,我无法正确更新框架。我使用普通窗口,我把我的代码放在下面。请帮我干净地更新我的画面!

映射类和标题

#include<string>
#include <Windows.h>



class map {
protected:
    char mep[200][50];

public:
    map();
    ~map();
    void bs();
    void buildTree(int X, int Y);
    void addTree();
    void buildMap();
    void printMap(int X, int Y);
};

#include"map.h"


map::map() {
    buildMap();
    printMap(0, 0);
}
map::~map() {

}


void map::bs() {
    //this builds a square world later cool things like trees get added
    for (int x = 0; x<200; x++) {
        for (int y = 0; y<500; y++) {
            mep[x][y] = '_';

        }

    }
}


void map::buildTree(int X, int Y) {

    //¨    /\¨ [x][y-4] 
    //    /  \"[x][y-3] 
    //¨  /-o  \¨ [x][y-2]  
    //¨ /-o-o- \¨[x][y-1]  
    //¨ / o0-o \¨ [x][y]   
    //¨/o-ooo-  \¨[x][y+1] 
    //¨   ||||¨ [x][y+2] 
    //¨   ||||¨ [x][y+3] 

    mep[X][Y - 4] = '/'; mep[X + 1][Y - 4] = '\\';
    mep[X - 1][Y - 3] = '/'; mep[X][Y - 3] = ' '; mep[X + 1][Y - 3] = ' '; mep[X + 2][Y - 3] = '\\';
    mep[X - 2][Y - 2] = '/'; mep[X - 1][Y - 2] = '-'; mep[X][Y - 2] = 'o'; mep[X + 1][Y - 2] = ' '; mep[X + 2][Y - 2] = ' '; mep[X + 3][Y - 2] = '\\';
    mep[X - 3][Y - 1] = '/'; mep[X - 2][Y - 1] = '-'; mep[X - 1][Y - 1] = 'o'; mep[X][Y - 1] = '-'; mep[X + 1][Y - 1] = 'o'; mep[X + 2][Y - 1] = '-'; mep[X + 3][Y - 1] = ' '; mep[X + 4][Y - 1] = '\\';
    mep[X - 3][Y] = '/'; mep[X - 2][Y] = ' '; mep[X - 1][Y] = 'o'; mep[X][Y] = '0'; mep[X + 1][Y] = '-'; mep[X + 2][Y] = 'o'; mep[X + 3][Y] = ' '; mep[X + 4][Y] = '\\';
    mep[X - 4][Y + 1] = '/'; mep[X - 3][Y + 1] = 'o'; mep[X - 2][Y + 1] = '-'; mep[X - 1][Y + 1] = 'o'; mep[X][Y + 1] = 'o'; mep[X + 1][Y + 1] = 'o'; mep[X + 2][Y + 1] = '-'; mep[X + 3][Y + 1] = ' '; mep[X + 4][Y + 1] = ' '; mep[X + 5][Y + 1] = '\\';
    mep[X - 1][Y + 2] = '|'; mep[X][Y + 2] = '|'; mep[X + 1][Y + 2] = '|'; mep[X + 2][Y + 2] = '|';
    mep[X - 1][Y + 3] = '|'; mep[X][Y + 3] = '|'; mep[X + 1][Y + 3] = '|'; mep[X + 2][Y + 3] = '|';

}
void map::addTree() {
    buildTree(74, 69);
    buildTree(5, 6);
    //add 18 more
}
void map::buildMap() {
    bs();
    addTree();
}
void map::printMap(int X, int Y) {
    //this is what the players will see
    std::string viewMap[15];

    for (int x = 0; x<15; x++) {
        for (int y = 0; y <= 70; y++) {
            viewMap[x] += mep[Y + y][X + x];

        }
    }
    //this prints the visible map for the players
    std::cout << viewMap[1] + "\n" + viewMap[2] + "\n" + viewMap[3]
        + "\n" + viewMap[4] + "\n" + viewMap[5] + "\n" + viewMap[6]
        + "\n" + viewMap[7] + "\n" + viewMap[8] + "\n" + viewMap[9]
        + "\n" + viewMap[10] + "\n" + viewMap[11] + "\n" + viewMap[12]
        + "\n" + viewMap[13] + "\n" + viewMap[14] << std::endl;

}
int main()
{
    //this is my main im using it as an update frames right now
    while (1) {
//also i will have the world render and build it self as a whole first and only loop the frame update ik its given but ill say it anway
        map mp;
        mp.~map();
    }
}

1 个答案:

答案 0 :(得分:0)

我希望我理解你。

如果您的程序仅使用终端,则可以打印某个ascii字符以返回到行的开头。然后你可以重写整个屏幕。

\033[f

这是我用于基于终端的ascii游戏的内容。 只需打印此内容即可覆盖您想要覆盖的行数。

std::cout << "\033[f";

希望这是您的解决方案