SDL:延迟直到键盘输入

发布于 2024-10-08 05:33:43 字数 228 浏览 2 评论 0原文

假设我们有这样的(伪)代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我的奇迹 2024-10-15 05:33:43

为什么不切换顺序(首先是 GetInput),然后阻塞 GetInput 并且在用户输入可接受的内容之前不返回?

Why not switch the order (GetInput first), then block in GetInput and don't return until the user has entered something acceptable?

骄兵必败 2024-10-15 05:33:43

您确定真的要等待吗?无意打扰,但通常在游戏中,最好在等待任何类型的输入时继续在屏幕上绘制内容。因为玩家不想一直看到完全相同的画面。

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.

茶色山野 2024-10-15 05:33:43

为了使游戏成为回合制游戏,我建议使用一系列对象,其中的方法代表玩家在游戏中的能力。您可以有一个游戏循环、一个嵌套回合循环、一个嵌套回合循环以及一个在玩家获胜时退出回合和回合循环的布尔值。

使用当前的伪代码,您可以在 Update 方法中使用开关和整数来等同这些嵌套循环。

Tic Tac Toe AI 示例:

#include "Shared.h"
#include "Board.h"

#ifndef AI_H_
#define AI_H_

class AI{
    public:
    AI();
    enum difficulty{Easy, Medium, Hard};
    void setAlgorithm(difficulty h);
    void applyAlgorithm(Board* b, int columns, int rows);
    private:
    Board::spot type;
        difficulty harder;
    void easy(Board* b, int columns, int rows);
    void medium(Board* b, int columns, int rows);
    void hard(Board* b, int columns, int rows);
};

#endif /*AI_H_*/

如果您不想占用 CPU,SDL_WaitEvent() 比 SDL_PollEvent() 更高效。

while(!quit){
    if(SDL_WaitEvent(&event)){
        if(event.type == SDL_QUIT){
            quit = true;
        }else if(event.type == SDL_MOUSEBUTTONDOWN){
            if(!menu.isClicked()){
                if(menu.handleEvent(&event)){
                }
            }
        }
    }
}

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:

#include "Shared.h"
#include "Board.h"

#ifndef AI_H_
#define AI_H_

class AI{
    public:
    AI();
    enum difficulty{Easy, Medium, Hard};
    void setAlgorithm(difficulty h);
    void applyAlgorithm(Board* b, int columns, int rows);
    private:
    Board::spot type;
        difficulty harder;
    void easy(Board* b, int columns, int rows);
    void medium(Board* b, int columns, int rows);
    void hard(Board* b, int columns, int rows);
};

#endif /*AI_H_*/

SDL_WaitEvent() is ridiculously more efficient than SDL_PollEvent(), if you don't want to hog the CPU.

while(!quit){
    if(SDL_WaitEvent(&event)){
        if(event.type == SDL_QUIT){
            quit = true;
        }else if(event.type == SDL_MOUSEBUTTONDOWN){
            if(!menu.isClicked()){
                if(menu.handleEvent(&event)){
                }
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文