pygame.time.set_timer 混乱? [英] pygame.time.set_timer confusion?

查看:42
本文介绍了pygame.time.set_timer 混乱?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一个问题,我不完全理解需要提供给计时器命令的事件,它没有说在线任何地方,我搜索了几个小时.所以我只是使用了大多数人似乎正在使用的USEREVENT + 1".我不确定它是否正确,但我的计时器不起作用.我是否正确使用它?这是我的代码:

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

然后我将它定义到我实现的第二页上的代码中(效果很好).它运行 nyansound,但在 6.5 秒(6500 毫秒)后不运行 nyansoundm.我制作这个程序是为了帮助我学习 python 和 pygame 的基础知识,然后再学习更复杂的东西.当我想听 nyan cat 或其他循环歌曲时,我也可以使用它,而不必上 youtube 并浪费宝贵的带宽.不过不用担心.

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)

推荐答案

让我们回顾一下 pygame.time.set_timer 确实:

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

pygame.time.set_timer(eventid, 毫秒): 返回 None

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

将事件类型设置为每隔给定的毫秒数出现在事件队列中.第一个事件在经过一段时间后才会出现.
每个事件类型都可以附加一个单独的计时器.最好使用 pygame.USEREVENT 和 pygame.NUMEVENTS 之间的值.

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.USEREVENTpygame.NUMEVENTS 是常量(2432),所以参数 <您传递给 pygame.time.set_timer 的 code>eventid 应该是 2432 之间的任何整数.

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+125,所以用起来没问题.

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

当你调用pygame.time.set_timer(USEREVENT+1,7000)时,eventid为25的事件每7000ms就会出现在事件队列中.您没有显示您的事件处理代码,但我猜您没有检查此事件,而您应该这样做.

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.

如你所见,pygame.time.set_timer 返回 None,所以你的线路

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

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

没有意义,因为 nyansecond 总是 None,因此将它与整数进行比较

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

if nyansecond < 200 ...

毫无意义.

如果你想使用事件队列每 6.5 秒播放一次声音,只需调用 pygame.time.set_timer 一次(!):

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

这篇关于pygame.time.set_timer 混乱?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆