我正在编写一个小型控制台冒险游戏,但遇到了一些问题。
1. 输入有点滞后,我正在使用 while 循环( while(getch() == 'w') )。第一次按下某个键后,什么也没有发生(您必须按两次),如果您切换方向(按 A/D/S 键),第一次也没有反应。如果你拿着钥匙,它工作正常。如何解决这个问题?
2.这是我用来刷新游戏的功能(按下按键时渲染游戏场景):
void refresh(char map[Y][X])
{
system("cls");
for (int i = 0; i<UP; i++)
{
cout<<endl;
}
for (int i = 0; i<Y; i++)
{
for (int k = 0; k<LEFT; k++)
{
cout<<" ";
}
for (int j = 0; j<X; j++)
{
cout<<map[i][j];
}
cout<<endl;
}
}
当我使用此功能一次时,没关系,但是当他们多次按下或按住键时 - 游戏区域开始闪烁。我想我只需要重绘该字段的一部分(进行更改/完成移动的地方),而不是整个字段。你能提供任何想法如何做到这一点吗?
输入示例代码:
while(getch() == 'w')
{
if (map[y-1][x]!= WALL)
{
map[y-1][x] = CHARACTER;
map [y][x] = ' ';
y--;
refresh(map);
Sleep(SPEED); // this is unnecessary, SPEED is 0, I just kept it for tests
}
}
基本上,主函数如下所示:
int main()
{
(...) Variables (...)
generateMap(FROM FILE);
refresh(); // First initialization of the field
while (getch() != 'q') // While not quitting
{
while(getch() == 'w')
{
if (THE FIELD ABOVE IS NOT OCCUPIED)
{
setSomeVariables();
refresh(THE GAMEFIELD);
}
}
}
while(getch() == 's')
{
if (THE FIELD BELOW IS NOT OCCUPIED)
{
setSomeVariables();
refresh(THE GAMEFIELD);
}
}
}
while(getch() == 'a')
{
if (THE FIELD ON THE LEFT IS NOT OCCUPIED)
{
setSomeVariables();
refresh(THE GAMEFIELD);
}
}
}
while(getch() == 'd')
{
if (THE FIELD ON THE RIGHT IS NOT OCCUPIED)
{
setSomeVariables();
refresh(THE GAMEFIELD);
}
}
}
return 0;
}