如何使用 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()
我试图在一段时间后在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须在应用程序循环中创建对象,但不能在单独的循环中创建对象。这个额外的循环会冻结您的系统。一般来说,如果你想在 pygame 中随时间控制某些东西,你有两种选择:
使用
pygame.time.get_ticks()
来测量时间并实现根据时间控制对象的逻辑。使用计时器事件。使用
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:
Use
pygame.time.get_ticks()
to measure time and and implement logic that controls the object depending on the time.Use the timer event. Use
pygame.time.set_timer()
to repeatedly create aUSEREVENT
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.