PlayerPaddle对象没有属性" centery" ;

时间:2017-12-05 08:30:37

标签: python inheritance attributes pygame sprite

我目前正在尝试通过绘制PlayerPaddle在主GameScene中运行我的代码。我目前正在使用Sprites重构我的代码。现在,我收到错误,说我的PlayerPaddle对象没有属性居中。如果我对该行进行评论,那么我将了解其他属性,例如centerx和rect也不是属性,尽管我不确定如何使用sprite来修复self.rect.center ... < / p>

这是PlayerPaddle类,它继承自Paddle Class,它继承自Image类。

class Image(pygame.sprite.Sprite):

    def __init__(self, screen_size, color=(255,0,0)):
        super().__init__()
        self.screen_size = screen_size

        self.angle = 0
        self.dir = 0

        self.original_image = pygame.image.load('naruto.png').convert()
        self.colorkey = self.original_image.get_at((0,0))

        self.original_image = pygame.transform.scale(self.original_image, (20, 100))

        self.update()

桨类

class Paddle(Image):

    def __init__(self, screen_size, color = (255,0,0)):
        super().__init__(screen_size, color = (255, 0, 0))

    def update(self):
        super().update()

    def Render(self, screen_size):
        super().draw(screen_size)

PlayerPaddle类

类PlayerPaddle(图片):

def __init__(self, screen_size, color = (255,0,0)):

    self.rect.centerx = 10
    self.rect.centery = screen_size[1] // 2

    self.height = 100
    self.width = 20

    self.speed = 3
    self.direction = 0


    super().__init__(screen_size, color = (255, 0, 0))

def update(self):

    super().update()
    self.centery += self.direction*self.speed
    #self.rect.centery += self.direction*self.speed 
    self.rect.center = (self.centerx, self.centery)

    if self.rect.top < 0:
        self.rect.top = 0

    if self.rect.bottom > self.screen_size[1]-1:
        self.rect.bottom = self.screen_size[1]-1


    def draw(self, screen_size):

        super().draw(screen_size)

最后,这是我的GameScreen,它继承自Scene类(未图示)。这是我创建对象并定义所有事件和更新的地方。

class GameScreen(SceneBase):

    def __init__(self):
        SceneBase.__init__(self)

        self.player_paddle = classes.PlayerPaddle(screen_size)
        self.my_font = pygame.font.SysFont("Arial", 50)

    def ProcessInput(self, events, pressed_keys):
        for event in events:
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    self.player_paddle.direction = -1
                elif event.key == pygame.K_DOWN:
                    self.player_paddle.direction = 1       

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_UP and self.player_paddle.direction == -1:
                    self.player_paddle.direction = 0
                elif event.key == pygame.K_DOWN and self.player_paddle.direction == 1:
                    self.player_paddle.direction = 0

    def Update(self):
        self.player_paddle.update() 

    def Render(self):
        screen.fill(WHTIE)
        self.player_paddle.Render(screen)

    def SwitchToScene(self, next_scene):
        self.next = next_scene

我不确定为什么我会返回属性错误,因为我在PlayerPaddle类中定义了属性,例如self.centerx和self.centery。另外,我如何处理self.rect.center与我的代码的情况?谢谢,非常感谢。

Traceback (most recent call last):
  File "C:\Users\matth\OneDrive\Documents\15-112\Term Project\TP3Pong\run_game.py", line 50, in <module>
    run_game(1200, 800, 64, scenes.GameScreen())
  File "C:\Users\matth\OneDrive\Documents\15-112\Term Project\TP3Pong\scenes.py", line 94, in __init__
    self.player_paddle = classes.PlayerPaddle(screen_size)
  File "C:\Users\matth\OneDrive\Documents\15-112\Term Project\TP3Pong\classes.py", line 172, in __init__
    self.rect.centerx = 10
AttributeError: 'PlayerPaddle' object has no attribute 'rect)

0 个答案:

没有答案