首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何将sprite从矩形更改为图像?

如何将sprite从矩形更改为图像?
EN

Stack Overflow用户
提问于 2019-04-12 08:36:27
回答 1查看 715关注 0票数 1

所以我从互联网(http://programarcadegames.com/python_examples/f.php?file=platform_moving.py)上复制了一些代码,只是为了试验pygame...

我已经尝试过用self.rect = pygame.image.load("TheArrow.png")替换self.image.fill(BLUE)

下面是我的代码的一小段。

代码语言:javascript
复制
 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

这是原始代码。

代码语言:javascript
复制
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显示出来,而不是一个矩形....

EN

回答 1

Stack Overflow用户

发布于 2019-04-12 08:52:51

Rect对象不是用来存储图像的。pygame.image.load()返回一个带有图像的Surface。它可以直接使用,也可以在另一个Surface上使用。

代码语言:javascript
复制
 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

或者:

代码语言:javascript
复制
 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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55642923

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档