Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pygame.time.set_timer confusion?

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 hours. So I just used what most people seemed to be using 'USEREVENT + 1'. I'm not sure if it is correct, but my timer is not working. Am I using it correctly? Here is my code:

nyansecond=462346
nyanint=0
spin=0
aftin=452345

def nyanmusic(nyansecond,nyanint,spin):
    if nyanint == 0:
        nyansound.play()
        nyanint= 1
    elif nyanint == 1:
        nyansecond = pygame.time.set_timer(USEREVENT+1,7000)
    if nyansecond < 200 and spin == 1:
        spin = 0
        nyansecond = pygame.time.set_timer(USEREVENT+1,7000)
    elif nyansecond > 6500 and nyansecond < 100000 and spin == 0:
        spin = 1
        nyansoundm.play()

    return nyansecond,nyanint,spin

I then def it into my code on the second page I implemented (which works fine). It runs the nyansound, but doesn't run nyansoundm after 6.5 seconds (6500 milliseconds). I'm making this program to help me learn the basics of python and pygame, before moving on to more complex stuff. I can also use it when I want to listen to nyan cat or other looped songs without having to go on youtube and waste precious bandwidth. Don't worry about that, though.

Oh, and here is the code I have put into my loop, although I do not think this matters too much:

#music
        nyansecond,nyanint,spin = nyanmusic(nyansecond,nyanint,spin)
like image 401
Bluetiger6001 Avatar asked Feb 24 '13 20:02

Bluetiger6001


1 Answers

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
like image 95
sloth Avatar answered Sep 21 '22 12:09

sloth