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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…