如何使用 Pygame 中的时钟/刻度函数在一段时间后生成另一个对象?

发布于 2025-01-09 07:30:55 字数 1118 浏览 0 评论 0原文

class Sideways:
"""Overall class to manage game assets"""

    def __init__(self):
    """Initialize the game and create game resources"""
    pygame.init()
    self.screen = pygame.display.set_mode((1200, 600))
    self.screen_rect = self.screen.get_rect()
    pygame.display.set_caption("Pew Pew")
    self.bg_color = (204, 255, 255)
    self.ship = Ship(self)
    self.moving_up = False
    self.moving_down = False
    self.moving_left = False
    self.moving_right = False
    self.bullets = pygame.sprite.Group()
    self.aliens = pygame.sprite.Group()


    FIRE_EVENT  = pygame.USEREVENT + 1  # This is just a integer.
    pygame.time.set_timer(FIRE_EVENT, 1000)  # 1000 milliseconds is 1 seconds.
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            quit()
        elif event.type == FIRE_EVENT:  # Will appear once every second.
            self._create_fleet()

我试图在一段时间后在 pygame 中生成另一艘船。

我刚刚从我之前的问题中了解到时钟/刻度函数被定向到另一个向我展示它的线程。谢谢。在本节中,我尝试将其合并到我的代码中。

然而,当我运行它时,游戏冻结并崩溃,就好像它同时产生太多东西来处理和过载一样。我该如何正确使用这个功能?

class Sideways:
"""Overall class to manage game assets"""

    def __init__(self):
    """Initialize the game and create game resources"""
    pygame.init()
    self.screen = pygame.display.set_mode((1200, 600))
    self.screen_rect = self.screen.get_rect()
    pygame.display.set_caption("Pew Pew")
    self.bg_color = (204, 255, 255)
    self.ship = Ship(self)
    self.moving_up = False
    self.moving_down = False
    self.moving_left = False
    self.moving_right = False
    self.bullets = pygame.sprite.Group()
    self.aliens = pygame.sprite.Group()


    FIRE_EVENT  = pygame.USEREVENT + 1  # This is just a integer.
    pygame.time.set_timer(FIRE_EVENT, 1000)  # 1000 milliseconds is 1 seconds.
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            quit()
        elif event.type == FIRE_EVENT:  # Will appear once every second.
            self._create_fleet()

I'm trying to spawn another ship in pygame after a certain interval of time.

I just learned about the clock/ticks function from my previous question being directed to another thread that showed me it. Thank you. I tried to incorporate that into my code during this section.

However, when I run it, the game freezes up and crashes as if it's spawning too many things at the same time to handle and overloads. How would I manage to use this function correctly?

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

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

发布评论

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

评论(1

马蹄踏│碎落叶 2025-01-16 07:30:55

您必须在应用程序循环中创建对象,但不能在单独的循环中创建对象。这个额外的循环会冻结您的系统。一般来说,如果你想在 pygame 中随时间控制某些东西,你有两种选择:

  1. 使用 pygame.time.get_ticks() 来测量时间并实现根据时间控制对象的逻辑。

  2. 使用计时器事件。使用 pygame.time.set_timer() 重复创建 USEREVENT 在事件中 队列。事件发生时更改对象状态。

例如,请参阅 生成python 中同时存在同一个对象创建时钟并定期执行操作等等类似的答案。

You must create the objects in the application loop, but not in a separate loop. This extra loop freezes your system. In general, if you want to control something over time in pygame, you have two options:

  1. Use pygame.time.get_ticks() to measure time and and implement logic that controls the object depending on the time.

  2. Use the timer event. Use pygame.time.set_timer() to repeatedly create a USEREVENT in the event queue. Change object states when the event occurs.

For example see Spawning multiple instances of the same object concurrently in python or create clock and do action at intervals and many more similar answers.

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