Skip to main content
 首页 » 编程设计

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

2024年09月07日6JustinYoung

我有这个代码:

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 

如果您想了解更多关于它们的区别,请参阅 Difference between pygame.display.update and pygame.display.flip