当您使用时pygame.display.update()
有两种optional参数、单个矩形(默认为 None)和矩形列表。如果没有传递参数,它将更新整个表面区域 - 就像 display.flip() 所做的那样。
update(rectangle=None) -> None
update(rectangle_list) -> None
要仅更新特定元素,如果您想同时更新同一组,请创建这些元素的列表
background_rects = [star_rect, star_rect, star_rect, some_other_rect]
foreground_rects = [player_rect, enemy1_rect, enemy2_rect, bullet1_rect, bullet2_rect]
pygame.display.update(background_rects)
pygame.display.update(foreground_rects)
or call update(rect)
对各个元素多次:
pygame.display.update(star1_rect)
pygame.display.update(star2_rect)
pygame.display.update(star3_rect)
pygame.display.update(character_rect)
pygame.display.update(enemy_rect)
文档链接:https://www.pygame.org/docs/ref/display.html#pygame.display.update https://www.pygame.org/docs/ref/display.html#pygame.display.update
pygame 1.9.6 和 2.0.0.dev 分支的处理之间似乎存在一些差异(可能是无意的,因为文档中没有任何内容) - 下面是MRE https://stackoverflow.com/help/minimal-reproducible-example它适用于 1.9.6,但不适用于 2.0.0。dev10版本。在 1.9.6 中,更新显示方面的差异显而易见。我建议你安装stable如果您需要这个功能,请使用 1.9.6 版本!
如果其他人想试试运气,这里是我测试过的 MRE:
import pygame
import time
screen = pygame.display.set_mode((720, 480))
rect = pygame.Rect((10, 50), (32, 32))
image = pygame.Surface((32, 32))
image.fill((0,100,0))
rect2 = pygame.Rect((10, 10), (32, 32))
image2 = pygame.Surface((32, 32))
image2.fill((100,100,0))
i = 0
while True:
i += 1
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
screen.blit(image, rect)
screen.blit(image2, rect2)
rect.x += 1 # we update the position every cycle
rect2.x += 1 # we update the position every cycle
# but update the rect on screen at different times:
if i < 10:
pygame.display.update() # both
elif i > 50 and i < 75:
pygame.display.update(rect2) # only rect2
elif i >= 100:
pygame.display.update(rect) # only rect
time.sleep(0.1)