"优化Pygame的精灵图片?"

2024-06-16 11:11:13 发布

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

我正在用pygame开发一个游戏,一切正常。但我想了些什么,不确定是否重要。你知道吗

我的游戏使用了sprite类的射弹,当前代码如下所示

class Enemy_Attack_Sprite(pygame.sprite.Sprite):
    def __init__(self, image, w=200, h=20):
        pygame.sprite.Sprite.__init__(self)
        image_load = pygame.image.load(image)
        self.image = pygame.transform.smoothscale(image_load,(w,h))

“Image”作为字符串作为文件名。所以我会打电话给你

projectile = Enemy_Attack_Sprite('projectile.png')

这是不是意味着每次一个镜头被触发,程序都会再次加载.png文件?这样做会更有效还是会产生重大影响:

projectile_image = pygame.image.load('projectile.png')
class Enemy_Attack_Sprite(pygame.sprite.Sprite):
    def __init__(self, image, w=200, h=20):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.transform.smoothscale(image,(w,h))

然后叫它:

projectile = Enemy_Attack_Sprite(projectile_image)

现在一切正常,但我想知道在我的游戏中重新编写图像和精灵函数是否会对性能产生影响?你知道吗


Tags: imageself游戏pnginitdeftransformload