如何用自定义图像而不是普通块填充我的pygame.surface()?
我有此代码:
import pygame
class Tile(pygame.sprite.Sprite):
def __init__(self, pos, groups):
super().__init__(groups)
self.loaded_image = pygame.image.load('Folder/MyImage.png') # I added an image and I want to
self.image = pygame.Surface((64, 64)) # <------------------------ add it here
简单地说,我有一个pygame.surface
,我想将pygame.surface
的值更改为我的self.loaded_image < /代码>。是否可以?因为通常
pygame.surface
仅显示一个普通的块,您只能用纯色填充它,但是我希望它是自定义图像。如何?
I have this code:
import pygame
class Tile(pygame.sprite.Sprite):
def __init__(self, pos, groups):
super().__init__(groups)
self.loaded_image = pygame.image.load('Folder/MyImage.png') # I added an image and I want to
self.image = pygame.Surface((64, 64)) # <------------------------ add it here
In simple words, I have a pygame.Surface
and I want to change the pygame.Surface
's value to my self.loaded_image
. Is it possible? Because usually pygame.Surface
just displays a plain block and you can fill it with only plain colors, but instead of a plain block, I want it to be a custom image. How?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,请注意,如果图像不是完全固体并且具有透明的背景,则需要使用Alpha频道创建一个表面(请参阅如何在pygame中具有透明背景的表面):
如果图像的大小相同,只需
blit
图像如果图像不同的大小,请使用
pygame.transform.smoothscale
或
pygame.transform.scale.scale
First, note that if the image isn't completely solid and has a transparent background, you'll need to create a surface with an alpha channel (see How to make a surface with a transparent background in pygame):
If the images are the same size, simply
blit
the imageIf the images are different sizes, use
pygame.transform.smoothscale
or
pygame.transform.scale