0

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();
}
Redeyery
  • 25
  • 4
  • Yes, sounds like the best solution is to use concurrent loops. You could also do it in a single loop, managing all present objects and have a timestamp on them to check when they were moved recently. But I think it will appear more reactive for the player when you do these asynchonously. – πάντα ῥεῖ Aug 14 '13 at 18:05

4 Answers4

3

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

}
gifnoc-gkp
  • 1,506
  • 1
  • 8
  • 17
1

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/

Erik Godard
  • 5,930
  • 6
  • 30
  • 33
1

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/

Himanshu Pandey
  • 726
  • 5
  • 13
0

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
}
Kyle G.
  • 870
  • 2
  • 10
  • 22
  • Why're you seperating movement from the game logic? – gifnoc-gkp Aug 14 '13 at 18:17
  • You're right, I should have specified that the loop should include the game logic. The pseudocode was only to represent the logic of how to separate the enemies' movements from the player's, in the same loop, with variable speed ratios and offsets. – Kyle G. Aug 14 '13 at 18:21
  • This is essentially what I need. Thank you. But the other examples are slightly more readable because they use a more top down approach. – Redeyery Aug 14 '13 at 18:22
  • @Redeyery If you wan't to study game design. You should consider using a library such as `irrlicht`. It will take care of the rendering part for you and you'll be free to design your Entities using pure OOP. Cheers! – gifnoc-gkp Aug 14 '13 at 18:29
  • @TheOtherGuy I'm making games for practice because it's a lot of fun and has proven to steepen my learning curve. Irrlicht seems interesting. But I'm more interested in 2D games. Do you happen to have a library suggestion for 2D games(I have a pixel-art fetish :D)? – Redeyery Aug 14 '13 at 18:35
  • @Redeyery Irrlicht supports 2D :D. Rendering 2d with opengl in 3D might even be faster than 2D-only renderer functions :P – gifnoc-gkp Aug 14 '13 at 18:42