简体   繁体   English

如何在 pygame 中制作无敌定时器?

[英]How can I make a invincibility timer in pygame?

I have a star power-up in my game, and when it collides with the player I want an 'invincibility timer' that starts up.我的游戏中有一个星星加电,当它与玩家发生碰撞时,我想要一个启动的“无敌计时器”。 This timer would basically turn off all collisions for 5 seconds, and after the 5 seconds are over, they would turn on again.这个计时器基本上会关闭所有碰撞 5 秒,在 5 秒结束后,它们会再次打开。 Is there a better way to accomplish this, and if not, how can I write this in pygame?有没有更好的方法来实现这一点,如果没有,我怎么能在 pygame 中写这个?

Use a boolean => invisibility = false Set it to true once the player colides with the star power-up.使用 boolean => invisibility = false 一旦玩家与星星加电发生碰撞,将其设置为 true。

Then in your if else statements or loops, state that if invisibility = true, no collisions will take place.然后在您的 if else 语句或循环中,state 如果 invisibility = true,则不会发生冲突。

In pygame exists a timer event.在 pygame 中存在一个定时器事件。 Usepygame.time.set_timer() to repeatedly create a USEREVENT in the event queue.使用pygame.time.set_timer()在事件队列中重复创建USEREVENT The time has to be set in milliseconds.时间必须以毫秒为单位。
Make the player invisible when the collision occurs and start the timer event.发生碰撞时使玩家不可见并启动计时器事件。 Make the player visible when you get the timer event:当你得到计时器事件时让玩家可见:

player_visible = True
timer_interval = 5000 # 5 seconds
timer_event_id = pygame.USEREVENT + 1

run = True
while run :
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

         elif event.type == timer_event_id:
             player_visible = True

    # collision detection
    # [...]
    if collide:
        player_visible = False
        pygame.time.set_timer(timer_event_id, timer_interval, 1)
        

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM