Pygame如何改变表面的大小?
如何更改 pygame 中具有图像的表面的大小(不缩放)。当我在 pygame 中加载图像时,表面变成图像的大小。我需要将表面的大小更改为框架(精灵表)的大小。
这是我用来解决问题的代码(感谢 Chris Dennett):
self.surface = pygame.Surface((20, 20))
self.surface.blit(pygame.image.load(imageName).convert_alpha(), (0,0), (0, 0, frameWidth, frameHeight))
How do you change the size of a surface in pygame that has an image (not scaling). When I load an image in pygame the surface becomes the size of the image. I need to change the size of the surface to be the size of a frame (sprite sheet).
Here is code I used to solve issue (thanks to Chris Dennett):
self.surface = pygame.Surface((20, 20))
self.surface.blit(pygame.image.load(imageName).convert_alpha(), (0,0), (0, 0, frameWidth, frameHeight))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于精灵表,您有两个不错的选择。
1) 将 spritesheet 作为一个 Surface 加载。
当位图传输时,使用源矩形参数。这只会从纸张上复制一帧。
2) 使用地下。
将精灵表加载为一个表面。
加载所需的每个帧的地下: http://www. pygame.org/docs/ref/surface.html#Surface.subsurface
就您而言,次表面是一个“新”表面,具有单元格的宽度和高度。但是,它不是重复内存。它是相连的。
来自文档:
For sprite sheets, you have 2 good options.
1) Load spritesheet as one Surface.
When blitting, use the source rect argument. This will blit just one frame, from the sheet.
2) Use subsurfaces.
Load spritesheet as one Surface.
Load a subsurface, of each frame you want : http://www.pygame.org/docs/ref/surface.html#Surface.subsurface
As far as you are concerned, the subsurface is a 'new' surface, with the width and height of the cell. But, it's not duplicate memory. It's linked.
From the docs:
创建一个新表面,传入所需的宽度和高度作为参数(即精灵有多大)。然后使用 newsurf.blit(spritesheet, (0, 0), (posX, posY, newsurf.getWidth(), newsurf.getHeight()))。应该有效。然后你可以使用 newsurf 作为你的精灵。
posX
和posY
应该分别是您想要在 sprite 表中进行 blit 的 x 和 y。Create a new surface, passing in the desired width and height as arguments (i.e., how big the sprite is). Then use
newsurf.blit(spritesheet, (0, 0), (posX, posY, newsurf.getWidth(), newsurf.getHeight()))
. Should work. Then you can use newsurf as your sprite.posX
andposY
should be the x and y you want to blit from in your sprite sheet respectively.