【问题标题】:Pygame scaling a spritePygame 缩放精灵
【发布时间】:2014-01-13 00:25:59
【问题描述】:

如何将精灵的图像缩放为更大或更小?我可以更改 rect 和所有内容,但不能更改图像。

代码:(虽然我不确定你为什么需要它)

class test(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.image.load("testImage.png").convert()
        self.image.set_colorkey((255,255,255))
        self.rect = self.image.get_rect()

【问题讨论】:

    标签: python pygame


    【解决方案1】:

    你试过了吗

    pygame.transform.scale()
    

    官方文档说明你应该使用这个语法:

    scale(Surface, (width, height), DestSurface = None) -> Surface
    

    【讨论】:

    • TypeError: 必须是 pygame.Surface,而不是 None
    • 你是怎么引用它的?
    • self.image = pygame.image.load("testImage.png").convert() self.image.set_colorkey((255,255,255)) pygame.transform.scale(self.image, (9999 , 99999), None) 文档说 DestSurface 是可选的。
    • 试试,self.image = pygame.transform.scale(self.image,(999,999))
    • 调整大小后是否重绘图像?通常,您会将图像绘制到表面,调整其大小,清除屏幕(可选),然后重绘屏幕(我相信是 pygame.display.flip())。
    【解决方案2】:

    了解 pygame 的转换模块:http://www.pygame.org/docs/ref/transform.html

    class test(pygame.sprite.Sprite):
        def __init__(self):
            pygame.sprite.Sprite.__init__(self)
            self.image = pygame.image.load("testImage.png").convert()
            self.image.set_colorkey((255,255,255))
            # return a width and height of an image
            self.size = self.image.get_size()
            # create a 2x bigger image than self.image
            self.bigger_img = pygame.transform.scale(self.image, (int(self.size[0]*2), int(self.size[1]*2)))
            # draw bigger image to screen at x=100 y=100 position
            self.screen.blit(self.bigger_img, [100,100])
    

    【讨论】:

      【解决方案3】:

      如果你想在 pygame 中缩放图像,你可以使用 pygame.transform.scale(image, (size, size)) 例如:

      image1_not_scaled = pygame.image.load('path_of_the_file')
      image1 = pygame.transform.scale(image1_not_scaled, (64, 64))
      
      [...]
      
      screen.blit(image1, (0, 0))
      

      对于您的示例,您可以这样写:

      class test(pygame.sprite.Sprite):
          def __init__(self):
              pygame.sprite.Sprite.__init__(self)
      
              self.image = pygame.image.load("testImage.png").convert()
              self.image.set_colorkey((255,255,255))
              self.image_scaled = pygame.image.load(self.image, (size, size))
              self.rect = self.image_scaled.get_rect()
      

      【讨论】:

        【解决方案4】:

        刚刚创建了一个帐户来在这里分享我的解决方案,因为我自己也遇到了这个问题。因此,对于一些上下文,我正在为玩家的精灵、运动和碰撞运行一个 Player() 类。在 init 方法中,我使用的是

        self.image = self.sprites_idle[self.current_sprite] # 在变量中设置图片 self.rect = self.image.get_rect(topleft = pos) # 设置图片的矩形

        current_sprite 是一个循环精灵索引以创建运动的变量,其余的非常简单。起初我尝试添加

        self.rect = self.image.get_rect()

        每次我蹲下或不蹲下更新矩形时,当然是在更新图像之后。与

        self.image = self.sprites_idle[self.current_sprite]

        但是我发现更新 rect() 不起作用,因为它不断重置 x,y 以及 w,h。 (因此,每次循环迭代都会将玩家传送到起始位置,即每秒 60 次,这意味着我们无法离开起始位置。如果这是您的问题,请执行以下操作:

        解决办法:

        self.image = self.sprites_idle[self.current_sprite] self.rect.height = self.image.get_height()

        通过仅更新高度,它现在可以按预期完美运行。

        【讨论】:

          猜你喜欢
          • 2023-03-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-09-14
          • 1970-01-01
          • 2023-03-02
          • 1970-01-01
          相关资源
          最近更新 更多