皮加梅。每次它吃鱼时,我如何调整我的鱼播放器图像的大小?

2024-09-28 21:26:33 发布

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

每次吃鱼的时候,我都想让我的鱼变大,但我不确定为什么它不起作用,这就是我做的

其他一切都正常,但我的球员身高或宽度没有增加 video

# our main loop
runninggame = True
while runninggame:
    clock.tick(fps)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            runninggame = False

         #[...........]
    for blac in blacs:
        if playerman.rect.colliderect(blac.rect):
            playerman.width += 10
            playerman.height += 10
            blac.x = 880

我的完整代码: https://pastebin.com/iL5h4fst


Tags: inrectloopeventforif宽度main
1条回答
网友
1楼 · 发布于 2024-09-28 21:26:33

当大小减小时,需要使用^{}缩放播放器的所有图像:

if playerman.rect.colliderect(blac.rect):
    playerman.width += 10
    playerman.height += 10
    blac.x = 880
    playerman.right = [pygame.transform.scale(image, (playerman.width,playerman.height)) for image in playerman.right]
    playerman.left = [pygame.transform.scale(image, (playerman.width,playerman.height)) for image in playerman.left]
    playerman.rect = pygame.Rect(playerman.x, playerman.y, playerman.width, playerman.height)

属性self.widthself.height必须通过图像的缩小大小来初始化(image.get_width()//8分别image.get_height()//8

随着播放器的增长,您需要将播放器的大小乘以比例因子以保持纵横比。使用pygame.transform.scale时,将宽度和高度四舍五入为整数:

class player:
    def __init__(self,x,y,height,width,color):
        # [...]

        self.right_original = [pygame.image.load("r" + str(i) + ".png") for i in range(1, 13)]
        self.left_original = [pygame.image.load("l" + str(i) + ".png") for i in range(1, 13)]
        self.width = self.right_original[0].get_width() / 8
        self.height = self.right_original[0].get_height() / 8
        self.scale_images()

        # [...]

    def scale_images(self):
        w, h = round(self.width), round(self.height)
        self.right = [pygame.transform.scale(image, (w, h)) for image in self.right_original]
        self.left = [pygame.transform.scale(image, (w, h)) for image in self.left_original]
        self.rect = pygame.Rect(self.x, self.y, w, h)

    def collide(self, other_rect):
        return self.rect.colliderect(other_rect)

    def grow(self, scale):
        self.width *= scale
        self.height *= scale
        self.scale_images()
for blac in blacs:
    if playerman.collide(blac):
        blac.x = 880
        playerman.grow(1.1)

相关问题 更多 >