Search code examples
c++concurrencyncursescursespdcurses

Do I need concurrent loops?


I'm making a text based game using the Curses library. There is a part of the game where the player enters an "arena". When inside of the arena the program needs to run a loop(1) that allows the player to move, and it also needs to run a loop(2) that moves the enemies around. Loop(2) needs to be delayed using Sleep so that the enemies move slower than the player. In researching this question I've come across something called multi-threading. I'm not sure that I need to learn this in order to get the results I want. I need to have one of these functions loop slower than the other.

While ( true )
{
    movePlayer(player_xy);
    arena.moveEnemies();
}

Solution

  • The structure of a simple game will update the positions before rendering every frame.

    Here's a simplified example that should suit your needs. For some information visit gamedev.net, there you'll find loads of tutorials and guides for your game.

    Pseudocode below

    MainLoop()
    {
    // Get Keyboard Input
    ...
    
    // Game Logic Impl
    ...
    
    // Update Positions
    UpdatePlayerPosition();
    
    for each ai
        UpdateAiPosition();
    
    // Check for sound triggers
    ProcessSoundTriggers();
    
    // Draw the scene if needed, you can skip some to limit the FPS
    DrawScene();
    
    // Flip the buffers if needed ...
    DoubleBufferFlip();
    
    }