在c中实现pacman,幽灵运动
我正在用 c 创建一个 pacman,目前我为每个幽灵使用一个线程,每个幽灵代表一个“#”,但是当我运行它时,所有屏幕都充满了幽灵,并且并非所有幽灵都只移动一两个。
我使用这个逻辑
创建一个由 5 个幽灵组成的结构,每个幽灵包含 x,y 位置。 创建一个包含 5 个线程的数组,每个线程实现一个 Ghost 每个幽灵在屏幕上随机移动,对于它移动的每个空间,我打印 在旧位置添加一个空格,然后在新位置打印一个“#”。
你能给我一个如何实现幽灵运动的例子吗? 或者我所做的实现是正确的方法?
谢谢
I am creating a pacman in c and currently I am usisng a single thread for each ghost and each ghost represents a '#' but when I run it all the screen gets full of ghosts and not all the ghosts move just one or two.
Im using this logic
create a struct of 5 ghost, each ghost contains the x,y position.
create an array of 5 threads and each thread implements one ghost
each ghost moves randomly on the screen, for each space that it moves I print
a space in the old position and then I print a '#' in the new position.
Could you provide me please an example of how to implement the movement of the ghost,
or the implementation Im doing is the correct way?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
每个代理一个线程并不是构建游戏的一种非常常见的方法。对于大型场景来说,它很快就变得不可行。传统的解决方案是定义一个代表幽灵的状态机,并使用某种“提前”方法使其有机会将其内部状态调整到下一个时间量子。创建此状态机的多个实例,并在游戏循环的每次迭代中调用它们的所有“高级”方法。所有这些都可以在单个线程中发生。
虽然还有很多内容,但它可以帮助您入门。
One thread per agent is not a very common approach to building games. It quickly becomes unworkable for large scenes. The conventional solution is to define a state machine representing a ghost, with some kind of "advance" method that gives it a chance to adjust its internal state to the next time quantum. Create multiple instances of this state machine, and call all their "advance" methods on each iteration of the game loop. All of this can happen in a single thread.
There's quite a bit more to it than this, but it'll get you started.
尝试从多个线程同时更新屏幕需要屏幕更新代码周围的互斥体。
Trying to update the screen simultaneously from several threads requires a mutex around the screen update code.