【问题标题】:Scaling a sprite in main loop [duplicate]在主循环中缩放精灵[重复]
【发布时间】:2021-01-29 15:06:43
【问题描述】:

我想创建一个游戏,其中圆圈在表面上随机生成并开始增长。当两个圆圈相互接触时,游戏结束。所以,除了在循环期间调整精灵的大小之外,一切都在工作。当我使用transform.scale 时,我会得到这样的结果:

然后我在文档中找到了transform.smoothscale。我使用更改了我的代码来使用它,然后它看起来像这样:

我也尝试使用Rect.inflate,但这对我的精灵没有任何作用。我尝试了Rect.infalte_ip,如果我使用它,精灵不会长大,它更有可能移出框架。有什么想法可以让这些 Sprite 在适当的位置生长并调整它们的大小吗?

class Bubbles(pygame.sprite.Sprite):
def __init__(self):
    super().__init__()
    self.image_scale = 100
    self.image_scale_factor = 1
    self.image = resources.BUBBLE_SKIN[0].convert_alpha()
    self.image = pygame.transform.scale(self.image, (self.image_scale, self.image_scale))
    self.rect = self.image.get_rect()
    self.rect.centerx = (random.randrange(Settings.object_range_limit + (self.image_scale//2), (Settings.width - Settings.object_range_limit - self.image_scale)))
    self.rect.centery = (random.randrange(Settings.object_range_limit + (self.image_scale//2), (Settings.height - Settings.object_range_limit - self.image_scale)))

    self.growth_rate = random.randint(1, 4)

def update(self):
    self.image_scale += self.growth_rate
    self.image = pygame.transform.scale(self.image, (self.image_scale, self.image_scale))

【问题讨论】:

  • 你是在缩放原始精灵还是逐渐缩放精灵?
  • 请出示一些代码以便我们为您提供帮助
  • 我将精灵的图像保存在“self.image”中。我用这个变量来缩放图像。
  • 你是否经常使用self.image= pygame.trasnform.smoothscale(self.image, ...)`之类的东西?

标签: python pygame pygame-surface


【解决方案1】:

您必须缩放原始精灵,而不是逐渐缩放精灵。每次扩展 sprint 时,质量都会下降。如果你逐渐缩放精灵,质量会越来越差。将精灵存储到属性orignal_image 并缩放orignal_image

如果要按图像中心缩放图像,则必须在使用新的图像大小缩放图像后更新rect 属性。
How do I scale a PyGame image (Surface) with respect to its center?

class Bubbles(pygame.sprite.Sprite):
    def __init__(self):
        # [...]
    
        self.image = resources.BUBBLE_SKIN[0].convert_alpha()
        self.image = pygame.transform.scale(
            self.image, (self.image_scale, self.image_scale))

        self.original_image =  self.image

    def update(self):
        self.image_scale += self.growth_rate
        self.image = pygame.transform.scale(
            self.original_image, (self.image_scale, self.image_scale))

        self.rect = self.image.get_rect(center = self.rect.center)

【讨论】:

  • 感谢到目前为止的工作。有没有办法解决我在关于“就地调整大小”的问题中所说的其他问题?
  • @Lilin20 我不知道你在说什么。你的问题不清楚。要设置初始比例吗?
  • 抱歉不清楚。所以当我现在缩放精灵时,它可以工作并且不会变形。但是当它缩放时,精灵会移动到我的右下角。
  • @Lilin20 我已经扩展了答案。
猜你喜欢
  • 1970-01-01
  • 2023-03-07
  • 1970-01-01
  • 2021-05-18
  • 1970-01-01
  • 1970-01-01
  • 2012-02-23
  • 1970-01-01
  • 2017-01-06
相关资源
最近更新 更多