Pygame 除非我退出并返回,否则不会出现矩形

Pygame Rectangle Doesn't Appear Unless I Tab-out and Back In

我有这个代码:

import pygame, sys
from pygame.locals import *

pygame.init()

WHITE = (255,255,255)

class setup():
    def set_resolution(x, y):
        global surf
        global screen
        surf = pygame.Surface((x,y))
        screen = pygame.display.set_mode((x,y))
        pygame.display.set_caption("Lewis' Game")
        pygame.draw.rect(screen, WHITE,(200,150,150,50))

    def menu():
        pass

setup.set_resolution(1024,768)
setup.menu()

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()

出于某种原因,设置函数末尾的 pygame.draw.rect(screen, WHITE,(200,150,150,50)) 实际上并没有出现,除非我显示桌面并将它们重新打开。我不确定为什么会发生这种情况,因为它从来没有发生过之前发生过。我应该提一下,我仍在学习 pygame,此代码仅供练习。

非常感谢。

更改您的 while 循环以包含此内容:

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()
    pygame.display.flip()    # pygame.display.update works as well

如果您想了解更多关于它们的区别,请参阅