7

とにかく、タイマーコマンドに与える必要があるイベントを完全には理解していません.オンラインのどこにも何時間も検索した場所はありません. だから私は、ほとんどの人が「USEREVENT + 1」を使用していると思われるものを使用しました。正しいかどうかわかりませんが、タイマーが機能していません。私はそれを正しく使用していますか?これが私のコードです:

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

次に、実装した 2 番目のページのコードに定義します (問題なく動作します)。nyansound を実行しますが、6.5 秒 (6500 ミリ秒) 後に nyansoundm を実行しません。より複雑なものに移る前に、python と pygame の基本を学ぶのに役立つように、このプログラムを作成しています。YouTube にアクセスして貴重な帯域幅を浪費することなく、ニャンキャットや他のループ曲を聴きたいときにも使用できます。しかし、それについて心配する必要はありません。

ああ、これは私がループに入れたコードですが、これはあまり重要ではないと思います:

#music
        nyansecond,nyanint,spin = nyanmusic(nyansecond,nyanint,spin)
4

1 に答える 1

14

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
于 2013-02-24T21:13:45.943 に答える