Pygame和pygame.time.wait函数以一种奇怪的方式工作

问题描述

我正在尝试更改pygame屏幕。首先,我画一个矩形。然后,我再次填充屏幕。在中间,我添加了等待功能以查看更改。当我运行程序时,屏幕开始黑屏,并等待所有等待功能,而无需进行任何更改。然后,显示最终结果。

import pygame
BlockList=[]
pygame.init()
block_side_length=30
black = 0,0
GameArea_start_x=5
GameArea_start_y=5
GameArea_width=300
GameArea_height=270
gameDisplay = pygame.display.set_mode((400,300))
pygame.display.set_caption("Tetris")
gameDisplay.fill((0,100,100))
GameArea = pygame.draw.rect(gameDisplay,black,[GameArea_start_x,GameArea_start_y,GameArea_width,GameArea_height])
lowest_block_y =GameArea_start_y+GameArea_height-block_side_length
pygame.time.wait(2000)
gameDisplay.fill((0,100))
pygame.draw.rect(gameDisplay,(0,250,0),[50,50,40,90])
pygame.time.wait(2000)
gameDisplay.fill((0,100))
GameArea

输出:

enter image description here

4秒后

enter image description here

注意:我试图通过调用矩形的名称来绘制矩形。没用有没有类似的时装?

解决方法

要更新pygame窗口中显示的内容,您必须像这样调用pygame.display.update()

import pygame
BlockList=[]
pygame.init()
block_side_length=30
black = 0,0
GameArea_start_x=5
GameArea_start_y=5
GameArea_width=300
GameArea_height=270
gameDisplay = pygame.display.set_mode((400,300))
pygame.display.set_caption("Tetris")
gameDisplay.fill((0,100,100))
GameArea = pygame.draw.rect(gameDisplay,black,[GameArea_start_x,GameArea_start_y,GameArea_width,GameArea_height])
lowest_block_y =GameArea_start_y+GameArea_height-block_side_length
pygame.display.update()
pygame.time.wait(2000)
gameDisplay.fill((0,100))
pygame.draw.rect(gameDisplay,(0,250,0),[50,50,40,90])
pygame.display.update()
pygame.time.wait(2000)
gameDisplay.fill((0,100))
pygame.display.update()

请注意,长时间调用pygame.time.wait()的方法将导致窗口冻结并在等待时无响应。 this stackoverflow question中描述了一些更好的方法。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...