简体   繁体   中英

Measuring time and doing actions in a game in C

I'm trying to develop a game in C using conio for creating an ascii interface.

I have to control my hero in a maze and finding the exit, avoiding the enemies.

Each enemy should perform a move every 1 second.

But I don't know how to implement the control of my hero and the control of the movement of each enemy (every 1 second).

Is it possible to do this without using threads?

time_t last_time_moved = 0;
time_t delay = 1;
int user_input;

for(;;) { // in your game loop
    time_t now = time(NULL); // check for the current time

    // ...

    if(_kbhit()) {
        user_input = _getch();

        // act on user input
    }

    // ...

    if(now > (last_time_moved + delay)) {

        // move your enemies

        last_time_moved = time(NULL);
    }

    // ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM