Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在pygame中刷新图像_Python_Python 3.x_Pygame - Fatal编程技术网

Python 在pygame中刷新图像

Python 在pygame中刷新图像,python,python-3.x,pygame,Python,Python 3.x,Pygame,Python版本:3.5.1和PyGame版本:1.9.2a0 我的主要目标是在屏幕上闪烁图像。打开0.5秒,关闭0.5秒 我知道,以下可以工作60帧/秒 frameCount = 0 imageOn = False while 1: frameCount += 1 if frameCount % 30 == 0: #every 30 frames if imageOn == True: #if it's on imageO

Python版本:3.5.1和PyGame版本:1.9.2a0

我的主要目标是在屏幕上闪烁图像。打开0.5秒,关闭0.5秒

我知道,以下可以工作60帧/秒

frameCount = 0
imageOn = False
while 1:

    frameCount += 1



    if frameCount % 30 == 0:   #every 30 frames
        if imageOn == True:   #if it's on
            imageOn = False   #turn it off
        elif imageOn == False:   #if it's off
            imageOn = True   #turn it on



    clock.tick(60)
但是我认为用整数来计算帧是不现实的。最终我的帧号会太大而无法存储在整数中


如何在不将当前帧(在本例中为帧数)存储为整数的情况下,每x秒刷新一次图像?或者这真的是最实用的方法吗?

我不知道这对你有多大帮助,但是为了防止你的
帧数过大,你只要在改变
imageOn
的状态时将其设为
0
,例如

if frameCount % 30 == 0:
    if imageOn == True:
        imageOn = False
        frameCount = 0
    elif imageOn == False:
        imageOn = True
        frameCount = 0
然而,如果没有其他人以更好的方式回答这个问题,我只建议将此作为最后手段。希望这有帮助,哪怕是一点点

编辑:我刚刚意识到,您也可以通过简单地使
imageOn=notimageon
更整洁地构建代码:

if frameCount % 30 == 0:
    imageOn = not imageOn
    frameCount = 0

你可以尝试使用pygame


避免让游戏依赖于帧速率,因为它会根据帧速率改变一切,如果计算机无法运行帧速率,整个游戏就会变慢

这个变量将帮助我们跟踪经过了多长时间。 while循环之前:

elapsed_time = 0
if elapsed_time > 500 # milliseconds, so .5 seconds
    imageOn = False if imageOn else True
    elapsed_time = 0 # so you can start counting again
找到拍摄一帧所需的时间。my_clock是pygame时钟对象,60是任意的

elapsed_time += my_clock.tick(60) # 60 fps, time is in milliseconds
您可以在while循环的某个地方使用if语句:

elapsed_time = 0
if elapsed_time > 500 # milliseconds, so .5 seconds
    imageOn = False if imageOn else True
    elapsed_time = 0 # so you can start counting again

编辑:我建议查看Chritical的答案,寻找一种更简单的方法来更改imageOn的真假值。我使用了一个内联条件,它可以工作,但没有必要。

谢谢,这两个都很有用,我想知道python是否有你提到的技巧注意python int不限于32位:它们会自动转换为“bigint”。还要注意的是,在每秒60帧时,你的游戏需要运行大约2.3年才能超过32位。有趣的是Daan。还有Racialz,如果您担心它,您可以使用if语句来重置它。如果frameCount>1000000:frameCount=0编辑:其中一个答案解决了我之前所说的问题