简体   繁体   English

在C语言中衡量时间并在游戏中执行动作

[英]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. 我正在尝试使用conio开发C语言中的游戏来创建ascii接口。

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. 每个敌人应该每1秒执行一次移动。

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). 但是我不知道如何实现对英雄的控制以及对每个敌人的移动的控制(每1秒)。

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

    // ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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