代码之家  ›  专栏  ›  技术社区  ›  Nemo

为什么Python类需要依赖于在其作用域之外定义的函数?

  •  0
  • Nemo  · 技术社区  · 1 年前

    我发现了以下代码 draws a health bar above a sprite

    import pygame
    import math
    
    def draw_health_bar(surf, pos, size, borderC, backC, healthC, progress):
        pygame.draw.rect(surf, backC, (*pos, *size))
        pygame.draw.rect(surf, borderC, (*pos, *size), 1)
        innerPos  = (pos[0]+1, pos[1]+1)
        innerSize = ((size[0]-2) * progress, size[1]-2)
        rect = (round(innerPos[0]), round(innerPos[1]), round(innerSize[0]), round(innerSize[1]))
        pygame.draw.rect(surf, healthC, rect)
        
    class Player(pygame.sprite.Sprite):
        def __init__(self, x, y):
            pygame.sprite.Sprite.__init__(self)
            self.original_image = pygame.image.load('CarBlue64.png')
            self.original_image = pygame.transform.rotate(self.original_image, 90)
            self.image = self.original_image
            self.rect = self.image.get_rect(center=(x, y))
            self.direction = pygame.math.Vector2((0, -1))
            self.velocity = 5
            self.position = pygame.math.Vector2(x, y)
            self.health = 10
    
    ...
    
        def draw_health(self, surf):
            health_rect = pygame.Rect(0, 0, self.original_image.get_width(), 7)
            health_rect.midbottom = self.rect.centerx, self.rect.top
            max_health = 10
            draw_health_bar(surf, health_rect.topleft, health_rect.size, 
                    (0, 0, 0), (255, 0, 0), (0, 255, 0), self.health/max_health) 
    

    有人能帮我理解以下问题吗

    1. 为什么是功能 draw_health_bar 在类外声明 Player ?
    2. 我能移动功能吗 draw_health_bar 在课堂内部 游戏者 ,然后定义函数 draw_health 内部 游戏者 的儿童班?例如
    class Player(pygame.sprite.Sprite):
        def __init__(self, x, y):
            pygame.sprite.Sprite.__init__(self)
    
    ...
        def draw_health_bar(surf, pos, size, borderC, backC, healthC, progress):
            pygame.draw.rect(surf, backC, (*pos, *size))
            pygame.draw.rect(surf, borderC, (*pos, *size), 1)
            innerPos  = (pos[0]+1, pos[1]+1)
            innerSize = ((size[0]-2) * progress, size[1]-2)
            rect = (round(innerPos[0]), round(innerPos[1]), round(innerSize[0]), round(innerSize[1]))
            pygame.draw.rect(surf, healthC, rect)
    
    ...
    
    class A(Player):
        def __init__(self, x, y):
            super().__init__(x, y)
    
    ...
    
        def draw_health(self, surf):
            health_rect = pygame.Rect(0, 0, self.original_image.get_width(), 7)
            health_rect.midbottom = self.rect.centerx, self.rect.top
            max_health = 10
            super().draw_health_bar(surf, health_rect.topleft, health_rect.size, 
                    (0, 0, 0), (255, 0, 0), (0, 255, 0), self.health/max_health) 
    
    1 回复  |  直到 1 年前
        1
  •  4
  •   Barmar    1 年前

    为什么是功能 draw_health_bar 在类外声明 Player ?

    因为它不会做任何特定于玩家的事情。它不引用的任何实例变量 游戏者 对象,或调用任何 游戏者 方法。

    我能移动功能吗 draw_health_bar 在课堂内部 游戏者 ,然后定义函数 draw_health 在玩家的子类中?

    你可以。但如果你这样做,你应该添加 @staticmethod 装饰师,因为它没有 self 参数然后你应该称之为使用 Player.draw_health_bar(...) 语法——类层次结构没有为名称建立作用域。