简体   繁体   English

如何在 pygame 中使用 time.sleep?

[英]How to use time.sleep in pygame?

So i am trying to display an image, wait 1 second and then display another image.所以我试图显示一个图像,等待 1 秒钟,然后显示另一个图像。 I'm making a matching game, so in my code I'm trying to say that if two images don't match, I want to change the image to just a preset generic one.我正在制作一个匹配游戏,所以在我的代码中我想说如果两个图像不匹配,我想将图像更改为预设的通用图像。 So my code for that would look something like this:所以我的代码看起来像这样:

if True:
    self.content = self.image
    time.sleep(1)
    self.content = Tile.cover

self.content is a variable for what is being displayed, self.image is the image that displays, and then Tile.cover is the generic image that covers the other one. self.content是显示内容的变量, self.image是显示的图像,然后Tile.cover是覆盖另一个的通用图像。 Yet whenever I do this the code skips the first line, and just sets the image to Tile.cover , why?然而,每当我这样做时,代码都会跳过第一行,并将图像设置为Tile.cover ,为什么?

Updated on 18/03/2019 . 2019 年 3 月 18 日更新

In Pygame, you have to use pygame.time.wait() instead of python's time.sleep() .在 Pygame 中,您必须使用pygame.time.wait()而不是 python 的pygame.time.wait() time.sleep()

It takes time in milliseconds :它需要以毫秒为单位的时间:

pygame.time.wait(1000)

The reason for not using time.sleep is because it will block pygame's event loop and so pygame will not be able to process other events.不使用time.sleep的原因是因为它会阻塞 pygame 的事件循环,因此 pygame 将无法处理其他事件。




Old answer旧答案

Following is the older version of this answer which was marked accepted .以下是标记为已接受的此答案的旧版本。 It is outdated and, most probably, incorrect .它已经过时,而且很可能是不正确的

The behaviour which you're getting is due to the way time.sleep()你得到的行为是由于time.sleep()

Example:例子:

I want you to try the following code in your console:我希望您在控制台中尝试以下代码:

>>> import time
>>> 
>>> def foo():
        print "before sleep"
        time.sleep(1)
        print "after sleep"
>>> 
>>> # Now call foo()
>>> foo()

Did you observe what happened during the output?你有没有观察到输出过程中发生了什么?

>>> # Output on calling foo()
... # Pause for 1 second
... 'before sleep'
... 'after sleep'

This is what is happening with your code too.这也是您的代码发生的情况。 First it sleeps, then it updates self.content to self.image and Time.cover at the same time.首先它休眠,然后同时将self.content更新为self.imageTime.cover

Fix:使固定:

To fix the code in the example above, you can use sys.stdout.flush() .要修复上面示例中的代码,您可以使用sys.stdout.flush()

>>> def foo():
        print "before sleep"
        sys.stdout.flush()
        time.sleep(1)
        sys.stdout.flush()
        print "after sleep"

>>> foo()
... 'before sleep'
... # pause for 1 second
... 'after sleep'

Disclaimer:免责声明:

I haven't tried sys.stdout.flush() with Pygame so I can't say if it will work for you, but you can try.我还没有在 Pygame 上试过sys.stdout.flush()所以我不能说它是否适合你,但你可以试试。

There seems to be a working solution on this SO question: How to wait some time in pygame?这个问题似乎有一个可行的解决方案: How to wait some time in pygame?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM