pygame.time.set_timer confusion?

前端 未结 1 1562
挽巷
挽巷 2020-12-11 17:12

So, I have a problem, I don\'t fully understand the event that is needed to be given to a timer command anyway, it doesn\'t say anywhere online, to where I searched for hour

1条回答
  •  猫巷女王i
    2020-12-11 18:10

    Let's recap what pygame.time.set_timer does:

    pygame.time.set_timer(eventid, milliseconds): return None

    Set an event type to appear on the event queue every given number of milliseconds. The first event will not appear until the amount of time has passed.
    Every event type can have a separate timer attached to it. It is best to use the value between pygame.USEREVENT and pygame.NUMEVENTS.

    pygame.USEREVENT and pygame.NUMEVENTS are constants (24 and 32), so the argument eventid you pass to pygame.time.set_timer should be any integer between 24 and 32.

    pygame.USEREVENT+1 is 25, so it's fine to use.

    When you call pygame.time.set_timer(USEREVENT+1,7000), the event with eventid 25 will appear in the event queue every 7000ms. You didn't show your event handling code, but I guess you do not check for this event, which you should do.

    As you can see, pygame.time.set_timer returns None, so your line

    nyansecond = pygame.time.set_timer(USEREVENT+1,7000)
    

    doesn't make sense since nyansecond will always be None, and hence comparing it against an integer

    if nyansecond < 200 ...
    

    is pointless.


    If you want to play a sound every 6.5 seconds using the event queue, simpy call pygame.time.set_timer once(!):

    PLAYSOUNDEVENT = USEREVENT + 1
    ...
    pygame.time.set_timer(PLAYSOUNDEVENT, 6500)
    

    and check the event queue for this event in your main loop:

    while whatever: # main loop
        ...
        # event handling
        if pygame.event.get(PLAYSOUNDEVENT): # check event queue contains PLAYSOUNDEVENT 
            nyansoundm.play() # play the sound
    

    0 讨论(0)
提交回复
热议问题