Question

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();
}
Was it helpful?

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();

}

OTHER TIPS

You shouldn't need to use multithreading for this. One approach you can use is to calculate the amount of time that has elapsed between the each calling of the main loop and use that to update the position of the players. You can also check for user input and use that to update the user's position. You can multiply the elapsed time by different constants to control the relative rates of movement. A more detailed explanation of the game loop can be found here: http://www.koonsolo.com/news/dewitters-gameloop/

use stl library header file <thread> you can define loops in two functions For Example:

#include<chrono>
#include<thread>
void fun1(){
  while(/*condition*/){
  //statments
  std::this_thread::sleep_for (std::chrono::seconds(1));
  }
}
void fun2(int y){
  while(/*codition*/){
  //statments
  std::this_thread::sleep_for (std::chrono::seconds(2));
  }
}
void main(){
std::thread th1(fun1);
std::thread th2(fun2,5);
//now both thread are running concurrently
}

For more details refer to link: http://www.cplusplus.com/reference/thread/thread/

If you'd like to avoid multi-threading you can run a loop at high frequency and have the player able to move every X loops, while the enemies can only move every Y loops. In that way you can vary the player:enemy movement speed ratio and you can set offsets so that different enemies move at different times (i.e. during a different cycle of the loop).

Something like this (pseudocode):

int loop_counter = 0;
while(doing_battle)
{
    if (loop_counter is a multiple of X)
    {
        Move player;
    }

    if (loop_counter is a multiple of Y)
    {
        Move evenmy_0;
    }

    if ((loop_counter + offset_1) is a multiple of Y)
    {
        Move enemy_1;    // This enemy will move at a different time from enemy_0
    }

    loop_counter++;

    delay();    // Will vary based on how quickly things need to move and how long your loop takes to complete
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top