pygame跳过更新屏幕

问题描述 投票:1回答:1

[我最近刚开始学习pygame,目前正在研究一个示例示例,该示例的猫在窗口的边缘跑来跑去((我将猫替换为读取的矩形,以便您可以复制该示例)]

import pygame
import sys
from pygame.locals import *

pygame.init()

FPS = 5
fpsClock = pygame.time.Clock()

DISPLAYSURF = pygame.display.set_mode((400, 300), 0, 32)
pygame.display.set_caption('Animation')

WHITE = (255, 255, 255)
RED = (255, 0, 0)
# catImg = pygame.image.load('cat.png')
catx = 10
caty = 10
direction = 'right'

while True:
    DISPLAYSURF.fill(WHITE)

    if direction == 'right':
        catx += 5
        if catx == 280:
            direction = 'down'
    elif direction == 'down':
        caty += 5
        if caty == 220:
            direction = 'left'
    elif direction == 'left':
        catx -= 5
        if catx == 10:
            direction = 'up'
    elif direction == 'up':
        caty -= 5
        if caty == 10:
            direction = 'right'

    # DISPLAYSURF.blit(catImg, (catx, caty))
    pygame.draw.rect(DISPLAYSURF, RED, (catx, caty, 100, 50))

    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        pygame.display.update()
        fpsClock.tick(FPS)

但是如果我运行它,显示的图像将不是我期望的:除非将鼠标放在窗口上,否则红色矩形不会运行。 (这可能是设计选择。所以无论如何)更令人担忧的是,矩形并没有按照我预期的方式移动。它比沿路径向前跳几步,然后再移动一点,再跳一遍,依此类推。我找不到跳跃发生方式的模式。我唯一能说的是它不会沿着窗口的边缘离开路径。

如果我移动线:

DISPLAYSURF.fill(WHITE)

从while循环中,我可以看到屏幕沿路径的跳过部分之后仍然显示为红色。因此在我看来,代码仍在后台运行,并且矩形仍被写入虚拟DISPLAYSURF对象,但是该DISPLAYSURF对象并未打印到屏幕上。而且代码运行得非常快。

我使用python 3.8.0pygame 2.0.0.dev6在Windows上

我没发现任何事情。有人有同样的问题吗?这是哪里来的?

pygame screen python-3.8
1个回答
1
投票

这是Indentation的问题。 pygame.display.update()必须在应用程序循环而不是事件循环中完成:

© www.soinside.com 2019 - 2024. All rights reserved.