简体   繁体   English

Pygame 显示仅在我退出时更新

[英]Pygame display only updates when I quit

Fiddling around with pygame and I'm not sure what's happening.摆弄 pygame,我不确定发生了什么。 code is just supposed to make a red box and bounce it around a gray screen.代码只是应该制作一个红色框并在灰色屏幕周围弹跳。 It works, but only when I quit the display.它有效,但仅当我退出显示器时。

I've checked out questions that are similar but none seem to have an answer that applies to me (might be wrong about that).我检查了类似的问题,但似乎没有一个适用于我的答案(可能是错误的)。 Does anyone know how this code could be improved?有谁知道如何改进这段代码?

import pygame
from pygame.locals import *
from rect import *
##from pygame.font import *

RED = (255, 0, 0)
GRAY = (150, 150, 150)

width = 500
height = 200

pygame.init()

screen = pygame.display.set_mode((width, height))

rect = Rect(100, 50, 50, 50)
v = [2, 2]

##moving = False
running = True

while running:
    for event in pygame.event.get():
        if event.type == QUIT:
            running = False
   
    rect.move_ip(v)

    if rect.left < 0 or rect.right > width:
        v[0] *= -1
    if rect.top < 0 or rect.bottom > height:
        v[1] *= -1

    screen.fill(GRAY)
    pygame.draw.rect(screen, RED, rect)
    pygame.display.flip()
    
pygame.quit()

It seems that the problem is the program running too fast.看来问题是程序运行太快了。 Since so little work is being done in each loop, the rectangle ends up moving around too quickly to clearly see what is happening.由于在每个循环中完成的工作很少,因此矩形最终移动得太快而无法清楚地看到发生了什么。

You can restrict the speed of the program using a pygame.time.Clock object.您可以使用pygame.time.Clock对象限制程序的速度。 Before the start of your loop, probably right after your screen definition, you can construct a clock.在循环开始之前,可能就在屏幕定义之后,您可以构建一个时钟。

clock = pygame.time.Clock()

Then in your main loop, as the very first step each iteration (right after while running: ) you can put clock.tick(60) to restrict the loop to running 60 times per second (60fps).然后在您的主循环中,作为每次迭代的第一步(在while running:之后clock.tick(60)您可以将clock.tick(60)限制为每秒运行 60 次 (60fps)。 This makes the program run smoothly, and you get the nice bouncing rectangle!这使程序运行顺利,您将获得漂亮的弹跳矩形!

The tick method works well, but on my system it seems to have small hitches every so often. tick方法效果很好,但在我的系统上,它似乎每隔一段时间就会出现小故障。 If you also experience this or if you want to be more accurate, you can use clock.tick_busy_loop(60) instead.如果您也遇到过这种情况或者想要更准确,可以改用clock.tick_busy_loop(60)

Both of these tick methods work the same way: By measuring the amount of time that passed since the last call to clock.tick() and delaying a certain additional amount based on that so that the target fps will be met.这两种滴答方法的工作方式相同:通过测量自上次调用clock.tick()以来经过的时间量,并在此基础上延迟一定的额外时间,以便达到目标 fps。

More info on Clock at Pygame docs . Pygame 文档中有关Clock更多信息。

I also needed to import pygame.rect instead of just import rect , but if you're not getting an import error you should be fine.我还需要import pygame.rect而不仅仅是import rect ,但如果您没有收到导入错误,您应该没问题。

I've figured it out.我已经想通了。 Stupidly I had another file in my test directory named "rect.py", I changed the file name and my code to from pygame.rect import * and it's working fine now.愚蠢的是,我的测试目录中有另一个名为“rect.py”的文件,我将文件名和代码更改为from pygame.rect import *并且现在工作正常。 Thank you Baked Potato for the help and for making me wonder where x=50, y=60, w=200, h=80 left=50, top=60, right=250, bottom=140 center=(150, 100) was coming from!谢谢烤土豆的帮助,让我想知道x=50, y=60, w=200, h=80 left=50, top=60, right=250, bottom=140 center=(150, 100)来自!

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

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