SDL:延迟直到键盘输入
假设我们有这样的(伪)代码:
GetInput()
{
//insert keyboard stuff here
}
Update()
{
//insert rendering stuff here
}
void game::loop()
{
game.Update();
player.GetInput();
}
在更新屏幕上的内容之前,我将如何等待玩家给出输入?
Say we have (pseudo) code like this:
GetInput()
{
//insert keyboard stuff here
}
Update()
{
//insert rendering stuff here
}
void game::loop()
{
game.Update();
player.GetInput();
}
How will I go about waiting for the player to give input before updating whats on screen?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么不切换顺序(首先是
GetInput
),然后阻塞GetInput
并且在用户输入可接受的内容之前不返回?Why not switch the order (
GetInput
first), then block inGetInput
and don't return until the user has entered something acceptable?您确定真的要等待吗?无意打扰,但通常在游戏中,最好在等待任何类型的输入时继续在屏幕上绘制内容。因为玩家不想一直看到完全相同的画面。
Are you sure you really want to wait? Don't mean to be intrusive, but usually in games it's best to continue drawing stuff on the screen while waiting for any kind of input. Because players don't want to see the very same picture all the time.
为了使游戏成为回合制游戏,我建议使用一系列对象,其中的方法代表玩家在游戏中的能力。您可以有一个游戏循环、一个嵌套回合循环、一个嵌套回合循环以及一个在玩家获胜时退出回合和回合循环的布尔值。
使用当前的伪代码,您可以在 Update 方法中使用开关和整数来等同这些嵌套循环。
Tic Tac Toe AI 示例:
如果您不想占用 CPU,SDL_WaitEvent() 比 SDL_PollEvent() 更高效。
To make the game turn-based, I recommend an array of objects with methods that represent a player's abilities in the game. You can have a game loop, a nested round loop, a nested turn loop, and a boolean that exits the round and turn loops when a player wins.
With your current pseudocode, you could use a switch and integer in the Update method to equate these nested loops.
Example Tic Tac Toe AI:
SDL_WaitEvent() is ridiculously more efficient than SDL_PollEvent(), if you don't want to hog the CPU.