首页 > 解决方案 > Is there a function in Pygame to save a portion of the screen as an image?

问题描述

I am working on making a drawing program in Pygame. All features are basically complete except for the ability to save your image. Is there any built in Pygame function that does this, or will I have to find some other way (E.G. taking a screenshot or something).

标签: pythonpygame

解决方案


screen是一个Surface对象,它具有subsurface(Rect)方法来获取表面的一部分 - 这部分也是一个Surface对象。

还有函数pygame.image.save(surface, filename)


其他方法:在开始时创建较小的Surface并在此表面上绘制(而不是直接在 上绘制screen)和blit()此表面screen以显示它并将save() 此表面归档。

您甚至可以使用许多表面并将它们列在列表中以创建功能Undo。当您使用新工具时,您复制表面并将其放在列表中。当您使用时Undo,您会从列表中获得最后一个表面。


推荐阅读