Pygame scaling a sprite

烂漫一生 提交于 2019-12-22 10:49:07

问题


How would one scale a sprite's image to be bigger or smaller? I can change the rect and all, but not the image.

Code:(though i'm not sure why you would need it for this)

class test(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.image.load("testImage.png").convert()
        self.image.set_colorkey((255,255,255))
        self.rect = self.image.get_rect()

回答1:


Have you tried

pygame.transform.scale()

Official documentation notes that you should use this syntax:

scale(Surface, (width, height), DestSurface = None) -> Surface



回答2:


Read about pygame's transform module: http://www.pygame.org/docs/ref/transform.html

class test(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("testImage.png").convert()
        self.image.set_colorkey((255,255,255))
        # return a width and height of an image
        self.size = self.image.get_size()
        # create a 2x bigger image than self.image
        self.bigger_img = pygame.transform.scale(self.image, (int(self.size[0]*2), int(self.size[1]*2)))
        # draw bigger image to screen at x=100 y=100 position
        self.screen.blit(self.bigger_img, [100,100])


来源:https://stackoverflow.com/questions/21082145/pygame-scaling-a-sprite

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!