使用索引替换数字时,pygame中的图像会变形

时间:2015-01-06 02:34:45

标签: python image resize pygame transform

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

enter image description here

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

i.imgur.com/c0bgVju.png

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)

2 个答案:

答案 0 :(得分:0)

请注意,由于greenScoreList[gfindex]的值将是一个整数,如果原始图片(如您所注明的)不是greenfishes[gfindex].image,则50的大小比例会失真广场。

原始尺寸为24 x 8像素,可产生无畸变图像,宽高比恰好为3:1。在您的第一张图片中,尺寸为74 x 58px(或24 + 50 x 8 + 50;来自greenScoreList[gfindex]的{​​{1}}),其比例约为14:11(1.276:1),已经相当扭曲了。在你发布的下一张图片中,它会增长到84 x 68px,或者比最后一张增加十倍,将其比例进一步扭曲到大约(21:17,或1.235:1)。

简而言之,在每个轴的大小上添加单个值会使原始图像变得越来越严重,因为它变得更像方形。您可能想要做的是将每个维度的大小乘以因子基于得分,而不是固定值得分代表。考虑:

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分可产生约3倍的鱼)宽于零),但因素取决于你。将尺寸相乘而不是添加尺寸将保留原始图像的纵横比(加或减一个像素或两个像素),并使图像看起来干净!

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

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

...在全局图书馆中使用原始greenfish图像的副本,而不是重新缩放“陈旧”图像。之一。

尽可能多地,您希望转换原始图像的副本,而不是进行复合转换,以便最大限度地减少转换工件的数量每个修订创建。只要确保你没有改变原件(无意中)!

答案 1 :(得分:0)

它采取这种方式的原因是因为绿鱼的update()函数取决于大小。如果我将大小重置为图像[" spr_greenfish"]而不是每一帧都对其进行pygame变换,这将被修复。 Pygame不喜欢这样。