使用 pygame.time.set_timer [英] Using pygame.time.set_timer

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

问题描述

我正在用 Pygame 和 Python(一种 Asteroids 的形式)制作游戏.如果屏幕上一次不超过 10 个,游戏应该每 10 秒再制造一个小行星.当我查看有关如何实现它的示例代码时,我发现我必须在代码中使用 USEREVENT.我可以用函数代替它吗?例如:

I am making a game in Pygame and Python, a form of Asteroids. Every 10 seconds, the game should make another asteroid if there isn't more than 10 on the screen at a time. As I was looking over the example code on how to implement it, I saw that I have to use USEREVENT in the code. Could I replace this with a function? For example:

    def testfunc():
      print "Test"

    pygame.time.set_timer(testfunc, 100)

如果我不能以这种方式使用该函数,我将如何将 USEREVENT 与此代码一起使用?

If I can't use the function this way, how would I use the USEREVENT with this code?

谢谢

Python Timer 对象可以使用 Pygame 而不是使用 Pygame set_timer() 吗?http://docs.python.org/2/library/threading.html#timer-objects

Could the Python Timer object with Pygame instead of using the Pygame set_timer()? http://docs.python.org/2/library/threading.html#timer-objects

推荐答案

看文档,似乎事件不是函数,而是表示事件类型的数字 ID.因此,您想声明自己的事件类型,如下所示:

Looking at the documentation, it seems that events are not functions, but numeric IDs representing the type of event. Thus, you want to declare your own event type, something like this:

SPAWNASTEROID = USEREVENT + 1

然后你可以这样排队:

pygame.time.set_timer(SPAWNASTEROID, 100)

最后向主循环添加一个处理程序,就像按钮点击等一样:

Finally add a handler to main loop, just like for button clicks and whatnot:

elif event.type == SPAWNASTEROID:
    if len(asteroids) < 10:
        asteroids.append(Asteroid())

编辑(示例代码):

自定义事件声明的类型

设置定时器

检查事件类型

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

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