如何解决从多个结构实例调用 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 举报,一经查实,本站将立刻删除。