pygame 中的图像在用索引替换数字时变形

Image in pygame gets distorted when replacing number with index

我不确定是否有错误、我的图像或我的代码有问题。例如,当 greenScoreList[gfindex] 是一个固定数字时,它会产生一个好的图像(忽略透明度不足):

greenScoreList[gfindex]取决于greenScoreList每一列的分数时,每个GreenFish的大小根据分数增加多少而增加。这会产生扭曲的图像:

def load_image(file, name, transparent, alpha):
    new_image = pygame.image.load(file)
    if alpha == True:
        new_image = new_image.convert_alpha()
    else:
        new_image = new_image.convert()
    if transparent:
        colorkey = new_image.get_at((0,0))
        new_image.set_colorkey(colorkey, RLEACCEL)
    images[name] = new_image

class GreenFish(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = images["spr_greenfish"]
        self.rect = self.image.get_rect()
        allsprites.add(self)
        self.random_direction()
        global greenWidth, greenHeight, greenScoreList
        greenWidth, greenHeight = (24, 8) #orig image size
        greenScoreList = [0, 0, 0, 0, 0]
    def update(self):
        newpos = (self.rect.topleft[0]+self.direction[0],self.rect.topleft[1]+self.direction[1])
        self.rect.topleft = newpos
        for greenFish in greenfishes:
            gfindex = greenfishes.index(greenFish)
            #the image looks distorted when removing the line below
            greenScoreList[gfindex] = 50 #50 is as fixed number for example
            greenfishes[gfindex].image = pygame.transform.scale(greenfishes[gfindex].image, ((greenWidth+greenScoreList[gfindex]), greenHeight+greenScoreList[gfindex]))
    def collision_with_redFish(self, greenFishIndex):
        for i in range(5):
            if i == greenFishIndex:
                greenScoreList[i] += 10
                break
spr_greenfish = load_image("sprites/greenfish3.png", "spr_greenfish", True, True)

请注意,由于 greenScoreList[gfindex] 的值将是一个整数,如果原始图像(如您所述)不是正方形。

您的原始尺寸为 24 x 8 像素,这将生成宽高比正好 3:1 的未失真图像。在您的第一张图片中,尺寸为 74 x 58 像素(或 24+50 x 8+50;50 来自 greenScoreList[gfindex]),比例约为 14:11 (1.276:1) ,并且已经相当扭曲。在您 post 的下一张图片中,它增长到 84 x 68 像素,或者比上一张多十倍,将其比例进一步扭曲到大约(21:17,或 1.235:1)。

简而言之,将单个值添加到每个轴的大小会随着原始图像变得更像正方形而越来越严重地扭曲它。您可能想要做的是 每个维度的大小乘以单个 factor, based在分数上,而不是分数代表的固定值。考虑:

for greenFish in greenfishes:
    gfindex = greenfishes.index(greenFish)
    greenScoreList[gfindex] = 50 # The score for this greenFish can stay as fifty...
    imagescale = 1 + (greenScoreList[gfindex] / 25.0) 
      # ...but the score needs to be converted to a factor instead of a hard value...

    greenfishes[gfindex].image = pygame.transform.scale(greenfishes[gfindex].image, (int(greenWidth * imagescale), int(greenHeight * imagescale)))
      # The fish's incoming dimensions are rescaled according to a resizing factor, based on its score! The result still has to be an integer.

在这种情况下,我选择将分数除以 25.0,以保持鱼的增大有点像您显示的大小增加(分数为 50 产生的鱼大约比 50 宽三倍零),但因素取决于您。将尺寸相乘而不是相加将保留原始图像的纵横比(加减一两个像素),并让您的图像看起来干净!

我还应该指出,串行转换几乎肯定会导致 generation loss 即使放大几次。您可能希望用类似这样的东西替换转换线以防止这种失真:

greenfishes[gfindex].image = pygame.transform.scale(images["spr_greenfish"], (int(greenWidth * imagescale), int(greenHeight * imagescale))) 

...它使用全局库中原始绿鱼图像的副本,而不是重新缩放 'stale' 图像。

尽可能多地转换原始图像的副本,而不是进行复合转换,以便最小化每次修订创建的转换工件的数量。请确保您没有(无意中)更改原件!

之所以这样,是因为绿鱼的update()函数依赖于大小。如果我将大小重置回 images["spr_greenfish"] 而不是每帧都对其进行 pygame 变换,这将得到修复。 Pygame 不喜欢那样。