当用索引替换数字时,pygame中的图像会失真

2024-09-30 22:23:58 发布

您现在位置:Python中文网/ 问答频道 /正文

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

enter image description here

greenScoreList[gfindex]依赖于greenScoreList的每一列中的分数时,每个{}的大小取决于分数增加的程度。这会产生扭曲的图像:

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)

Tags: rect图像imageselfalphatruenewif
2条回答

它这样做的原因是因为green fish的update()函数依赖于大小。如果我将大小重置回images[“spr_nugreenfish”]而不是在每一帧上都对其进行pygame变换,那么这个问题就会得到解决。Pygame不喜欢这样。在

请注意,由于greenScoreList[gfindex]的值将是一个整数,greenfishes[gfindex].image的大小比将在原始图像(如您所述)不是正方形的情况下扭曲。在

您的原始尺寸是24 x 8像素,这将产生一个高宽比正好为3:1的未失真图像。在第一张图片中,尺寸是74×58px(或24+50×8+50;50来自greenScoreList[gfindex]),其比率约为14:11(1.276:1),并且已经严重失真。在你发布的下一张图片中,它的像素增加到84 x 68像素,比上一张增加了10倍,使其比例进一步扭曲到大约(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时,鱼的宽度约为0的三倍),但因素取决于您。乘以尺寸而不是增加尺寸将保持原始图像的纵横比(加上或减去一两个像素),并保持图像看起来干净!在

我还应该指出,串行转换几乎肯定会导致generation loss,即使是几次放大。您可能希望将转换线替换为以下内容,以防止这种失真:

^{pr2}$

…它使用全局库中原始greenfish图像的副本,而不是重新缩放“过时”图像。在

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

相关问题 更多 >