同时为游戏运行两个循环 - C.

时间:2022-06-01 19:15:30

I'm making a game in C for my programming class, and I have to place a time countdown on the game, but I can't make it work right, because if a put a countdown function and a delay(1000), it works for the countdown, but doesn't work for the game , because it makes wait the 1s every move.

我正在为我的编程课在C中制作一个游戏,我必须在游戏中进行倒计时,但是我不能让它正常工作,因为如果一个倒计时功能和一个延迟(1000),它适用于倒计时,但不适用于游戏,因为每次移动都会等待1秒。

My code so far is

到目前为止我的代码是

while(tempo > 0)
{
    tempo_na_tela(&tempo);
    contador_tempo(&tempo);
    if(kbhit())
    {
        mover_refem(getch(), p_refem, &refem.px, &refem.py, 
                    numero_inimigos_na_tela(n, in1));
    }
    mover_inimigo(n, p_terrorista, in1);
}

The function tempo_na_tela(..) puts the string of time on the screen, the contador_tempo(..) is the countdown, the mover_refem(...) is the function to move the game character, and the mover_inimigo(..)is a function that randomly moves the enemy in the screen.

函数tempo_na_tela(..)将时间串放在屏幕上,contador_tempo(..)是倒计时,mover_refem(...)是移动游戏角色的函数,mover_inimigo(..)是在屏幕中随机移动敌人的功能。

I need to place the tempo_na_tela and the contador_tempo functions in one loop, that run simultaneously with the other loop, that run the moving functions.

我需要将tempo_na_tela和contador_tempo函数放在一个循环中,该循环与运行移动函数的另一个循环同时运行。

How can I do it?

我该怎么做?

1 个解决方案

#1


You're experimenting a XY problem i think.

我想你正在试验一个XY问题。

You don't need to run simultaneously thoses 2 functions. What you're trying to accomplish is a game loop, it's very common in video-games, especially early ones.

您不需要同时运行2个函数。你想要完成的是一个游戏循环,它在视频游戏中很常见,尤其是早期游戏。

Ask yourself, when do you need to re-paint your elements ? The answer is probably after having updated all of your data (Time, ennemy position, and having logged your player's movement)

问问自己,什么时候需要重新绘制元素?答案可能是在更新了所有数据(时间,敌人位置,并记录了玩家的动作)之后

So, you don't need simultaneous looping (ie thread i would have suggested, even if it's not true simultaneous, but that's another story.)

所以,你不需要同时循环(即我建议的线程,即使它不是同时的,但那是另一个故事。)

Instead, you can stick with one loop, but you have to do something in this fashion :

相反,你可以坚持一个循环,但你必须以这种方式做一些事情:

while (game_not_ended())
{
   update_data();
   repaint_data();
}

I hope you'll take the time to reconsider your code and the scope of your issue.

我希望您花时间重新考虑您的代码和问题的范围。

#1


You're experimenting a XY problem i think.

我想你正在试验一个XY问题。

You don't need to run simultaneously thoses 2 functions. What you're trying to accomplish is a game loop, it's very common in video-games, especially early ones.

您不需要同时运行2个函数。你想要完成的是一个游戏循环,它在视频游戏中很常见,尤其是早期游戏。

Ask yourself, when do you need to re-paint your elements ? The answer is probably after having updated all of your data (Time, ennemy position, and having logged your player's movement)

问问自己,什么时候需要重新绘制元素?答案可能是在更新了所有数据(时间,敌人位置,并记录了玩家的动作)之后

So, you don't need simultaneous looping (ie thread i would have suggested, even if it's not true simultaneous, but that's another story.)

所以,你不需要同时循环(即我建议的线程,即使它不是同时的,但那是另一个故事。)

Instead, you can stick with one loop, but you have to do something in this fashion :

相反,你可以坚持一个循环,但你必须以这种方式做一些事情:

while (game_not_ended())
{
   update_data();
   repaint_data();
}

I hope you'll take the time to reconsider your code and the scope of your issue.

我希望您花时间重新考虑您的代码和问题的范围。