Python 如何将精灵从矩形更改为图像?
所以我从互联网上复制了一些代码()只是为了试验pygame 我尝试用Python 如何将精灵从矩形更改为图像?,python,image,pygame,pygame-surface,Python,Image,Pygame,Pygame Surface,所以我从互联网上复制了一些代码()只是为了试验pygame 我尝试用self.rect=pygame.image.load(“TheArrow.png”)替换self.image.fill(蓝色) 下面是我的一段代码 def __init__(self): """ Constructor function """ # Call the parent's constructor super().__init__() # Crea
self.rect=pygame.image.load(“TheArrow.png”)
替换self.image.fill(蓝色)
下面是我的一段代码
def __init__(self):
""" Constructor function """
# Call the parent's constructor
super().__init__()
# Create an image of the block, and fill it with a color.
# This could also be an image loaded from the disk.
width = 40
height = 60
self.image = pygame.Surface([width, height])
self.image.fill(BLUE)
self.rect = pygame.image.load("TheArrow.png")
# Set a referance to the image rect.
self.rect = self.image.get_rect()
# Set speed vector of player
self.change_x = 0
self.change_y = 0
# List of sprites we can bump against
self.level = None
这是原始代码
def __init__(self):
""" Constructor function """
# Call the parent's constructor
super().__init__()
# Create an image of the block, and fill it with a color.
# This could also be an image loaded from the disk.
width = 40
height = 60
self.image = pygame.Surface([width, height])
self.image.fill(RED)
# Set a referance to the image rect.
self.rect = self.image.get_rect()
# Set speed vector of player
self.change_x = 0
self.change_y = 0
# List of sprites we can bump against
self.level = None
我希望显示图像
TheArrow.png
而不是矩形….Rect
对象不用于存储图像pygame.image.load()
返回带有图像的曲面。它可以直接使用,也可以在另一个表面上使用
def __init__(self):
""" Constructor function """
# Call the parent's constructor
super().__init__()
width = 40
height = 60
self.image = pygame.image.load("TheArrow.png") #use the image Surface directly
self.rect = self.image.get_rect()
#the rest as in the original code
或:
在前一种情况下,曲面
(其关联的rect,可通过self.image.get_rect()
获得)的大小与加载的图像文件相同。
在后者中,您可以使用[with,height]
设置大小。如果这些设置与图像大小不一致,则图像将被剪切(如果更大)
顺便说一句,在另一个曲面上快速显示曲面
,就是在屏幕上显示曲面。在pygame中,屏幕只是另一个曲面
,有点特殊
查看以获取更多信息。pygame.image.load()
返回一个曲面
,因此您也可以创建一个与图像大小相同的曲面,只需加载它:cat\u image=pygame.image.load(“kitteh.png”)
,然后立即使用它:screen.blit(cat\u image,(10,10))
。
def __init__(self):
""" Constructor function """
# Call the parent's constructor
super().__init__()
width = 40
height = 60
myimage = pygame.image.load("TheArrow.png")
self.image = pygame.Surface([width, height])
self.image.blit(myimage) #blit the image on an existing surface
self.rect = self.image.get_rect()
#the rest as in the original code