【发布时间】:2019-09-21 23:35:29
【问题描述】:
我是脏矩形动画的新手,我目前正在尝试存储主显示表面窗口的快照,但是我只想存储我的项目将被 blit 的区域,以便下一帧我可以调用这个存储的快照而不是重新传输整个背景。
我查看了 Surface.copy() 的文档,但它不接受参数,除了 pygame.pixelcopy() 之外我找不到任何类似的东西,据我所知,这不是我想要的。如果 Surface.copy() 不是我想要的,请告诉我替代方案。
import pygame, time
pygame.init()
screen = pygame.display.set_mode((500, 500))
screen.fill((128, 128, 128))
pygame.display.update()
#immagine a complex pattern being blit to the screen here
pygame.draw.rect(screen, (128, 0, 0), (0, 0, 50, 50))
pygame.draw.rect(screen, (0, 128, 0), (50, 0, 50, 50))
pygame.draw.rect(screen, (0, 0, 128), (200, 0, 50, 50))
#my complex background area that i want to save ()
area_to_save = pygame.Rect(0, 0, 100, 50)
rest_of_background = pygame.Rect(200, 0, 50, 50)
#updating for demo purposes
dirty_rects = [area_to_save, rest_of_background]
for rect in dirty_rects:
pygame.display.update(rect)
temp_screen = screen.copy()
time.sleep(3)
#after some events happen and I draw the item thats being animated onto the background
item_to_animate = pygame.Rect(35, 10, 30, 30)
pygame.draw.rect(screen, (0, 0, 0), item_to_animate)
pygame.display.update(item_to_animate)
time.sleep(3)
item_to_animate = pygame.Rect(50, 60, 30, 30)
pygame.draw.rect(screen, (0, 0, 0), item_to_animate)
#now that the item has moved, draw back old frame, which draws over the whole surface
screen.blit(temp_screen, (0, 0))
pygame.display.update()
#I understand swapping the drawing of the new item location to after temp_surface blit
#will provide me the desired outcome in this scenario but this is a compressed version of my problem
#so for simplicity sake, is there a way of not saving the whole surface, only those rects defined?
我希望这段代码的输出显示我的背景 3 秒,然后黑色方块覆盖图案,再过 3 秒,黑色方块出现在我的图案下方。
P.S.:我是这个网站的新手,如果我做错了什么请告诉我!
编辑:对于任何想知道此解决方案(在将项目覆盖之前保存背景,然后在新项目位置被覆盖之前重绘保存的背景)是否比重绘整个背景然后对项目进行移动更有效的人,在方格图案上使用简单的方形动画,每次重绘整个背景,将我的整体 fps 从 1000(重绘背景之前)降低约 50% 到平均 500。在使用脏矩形和上面的这种方法时,我得到了大约 900 fps。
【问题讨论】:
标签: python animation pygame updating surface