所以我从互联网(http://programarcadegames.com/python_examples/f.php?file=platform_moving.py)上复制了一些代码,只是为了试验pygame...
我已经尝试过用self.rect = pygame.image.load("TheArrow.png")
替换self.image.fill(BLUE)
下面是我的代码的一小段。
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
显示出来,而不是一个矩形....
发布于 2019-04-12 08:52:51
Rect
对象不是用来存储图像的。pygame.image.load()
返回一个带有图像的Surface
。它可以直接使用,也可以在另一个Surface
上使用。
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
或者:
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
在前一种情况下,可以使用self.image.get_rect()
获得的Surface
(与其关联的rect )的大小与加载的图像文件相同。
在后者中,使用[with, height]
设置大小。如果这些值与图像大小不符,则图像将被剪切(如果较大)。
顺便说一句,在另一个Surface
上对一个Surface
进行what操作就是在屏幕上显示这个Surface
。在pygame中,屏幕只是另一个Surface
,有点特别。
有关更多信息,请查看intro tutorial。
https://stackoverflow.com/questions/55642923
复制相似问题