从多个结构实例调用 Draw() 函数

问题描述

我正在开发一个控制台、ASCII 游戏,我需要文本输出显示玩家、敌人和地图。为了让多个敌人可以访问它们,我将更改和读取其值的函数存储在 entity 结构中。

struct entity
{
    int hp,atk,x,y;
    bool isPlayer;
    void Draw();
    void Setup();
    void input();
    void Logic();
};

这里是 Draw() 函数

void entity::Draw()
{
    system("cls");  // Screen clear

    for (int i = 0; i < WIDTH; i++) // Top frame
        cout << "#";

    cout << endl;

    for (int i = 1; i < HEIGHT; i++)
    {
        for (int j = 0; j < WIDTH; j++)
        {
            if (j == 0 || j == WIDTH - 1)   // Middle frame
                cout << "#";

            else if (j == x && i == y)      // Player character display
            {
                if (isPlayer)
                    cout << "@";
                else                        // Enemy display
                    cout << "D";
            }
            else
                cout << " ";                // Empty space
        }
        cout << endl;
    }

    for (int i = 0; i < WIDTH; i++) // Bottom frame
        cout << "#";
}

问题出现在执行中,其中 Draw() 和其他 3 个函数在 while 循环中运行,每个 entity 实例一个

int main()
{
    entity player = entity();
    player.isPlayer = true;
    player.Setup();

    entity dummy = entity();
    dummy.Setup();
    while (!gameOver)   // Main Game Loop
    {
        player.Draw();
        dummy.Draw();
        player.input();
        player.Logic();
        dummy.Logic();
        Sleep(10);
    }
}

由于 dummy Draw() 最后运行,它与前一个重叠,有效地使玩家不可见。由于必须为每个实体重绘一次屏幕,这也大大减慢了游戏速度。

有没有办法只调用一次函数?或者可能有更深层次的缺陷?

是的,我知道 system("cls") 是大罪。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)