在C中实现线程

发布于 2025-02-07 01:33:44 字数 836 浏览 3 评论 0原文

我目前正在从事一个迷你游戏项目,并且我对这个特定的机械师有问题:

void monsterMove(){
   //monster moves randomly
}

void playerMove(){
   //accepting input as player movement using W, A, S, D
}

但是,即使玩家没有移动,该项目也需要怪物始终继续前进。

经过一番研究,我发现需要多线程来实现此机械,因为monstermove() and playermove()即使play> playermove()也需要同时运行。尚未收到用户的任何输入。

具体而言,我要解决两个问题:

  1. 需要将哪个功能作为线程?
  2. 如何构建线程?

可悲的是,我没有发现具体的此类问题的资源,因为Internet上的所有内容似乎都指出了多线程在某种程度上可以作为并行性工作,而在这种实现中却没有。

我当时认为monstermove()将在playermove()时作为线程进行递归运行,因为monsterMove()将需要即使playermove()线程尚未完成(尚未输入),也要运行每个n秒。虽然我可能在大多数情况下都错了。

谢谢您!

(PS只是为了避免误解,我专门询问线程和多线程的工作原理,而不是Mech的逻辑。)

编辑:程序现在正在工作!但是,非常感谢与该程序如何完成此程序有关的任何答案/代码。 :)

I'm currently working on a mini game project, and I have a problem with this specific mechanic:

void monsterMove(){
   //monster moves randomly
}

void playerMove(){
   //accepting input as player movement using W, A, S, D
}

However, the project requires the monsters to keep moving at all times, even when the player is not moving.

After some research, I figured out that multithreading is needed to implement this mech since both monsterMove() and playerMove() needs to run concurrently even when playerMove() hasn't received any input from user.

Specifically there are 2 questions I want to address:

  1. Which function needs to be made as a thread?
  2. How to build the thread?

Sadly, I found no resource with specifically this type of question because everything on the Internet just seem to point out how multithreading can work as parallelism in a way, but not in such implementations.

I was thinking that monsterMove() will be run recursively while playerMove() will be made as the thread, since monsterMove() will need to run every n seconds even when playerMove() thread is not finished yet (no input yet). Although I might be wrong for the most part.

Thank you in advanced!

(P.S. Just to avoid misunderstandings, I am specifically asking about how thread and multithreading works, not the logic of the mech.)

Edit: Program is now working! However, any answer/code related to how this program is done with multithreading is immensely appreciated. :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

表情可笑 2025-02-14 01:33:44

您不需要多线程阅读:

基本结构在此伪代码中描述:

while (1)
{
   check if input is available using kbhit

   if (input available)
     read user input using getch

   move player depending on user input
   move monsters
}

可以使用 costed> costedthread 功能,但是您的代码将变得过于复杂。

You don't need multithreading for this:

The basic structure is depicted in this pseudocode:

while (1)
{
   check if input is available using kbhit

   if (input available)
     read user input using getch

   move player depending on user input
   move monsters
}

You could use multithreading using the CreatdThread function, but your code will just become overly complex.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文