Getting AttributeError: type object 'Pipe1' has no attribute 'height'
Question: Hi I keep getting the error AttributeError: type object 'Pipe1' has no
attribute 'height' I was trying to create flappy bird with pygame/python. Just
a note, this isn't completed yet, just wanted to see if I had any major
errors. This is python 2.7.7 if that helps.

    
    
    import pygame, random, sys
    
    pygame.init()
    screen = pygame.display.set_mode([284, 512])
    
    pipex = 335
    
    class Bird(pygame.sprite.Sprite):
        def __init__(self, image, x, y):
            pygame.sprite.Sprite.__init__(self)
            self.image = pygame.image.load(image)
            self.rect = self.image.get_rect()
            self.pos = [x, y]
    
        def move():
            self.pos[0] += 3
            self.pos[1] += 3
            self.rect.center = self.pos
    
    class Pipe1(pygame.sprite.Sprite):
        def __init__(self, image, height):
            pygame.sprite.Sprite.__init__(self)
            self.image = pygame.image.load(image)
            self.rect = self.image.get_rect()
            self.height = height
            self.pos = [pipex, height]
            oppositepipe = Pipe2('flappybirdpipe2.png')
    
        def scroll():
            global pipex
            self.pos[0] -= 3
            self.rect.center = self.pos
    
    class Pipe2(pygame.sprite.Sprite):
        def __init__(self, image):
            pygame.sprite.Sprite.__init__(self)
            self.image = pygame.image.load(image)
            self.rect = self.image.get_rect()
            self.height = 397 - Pipe1.height
            self.pos = [pipex, self.height]
    
        def scroll():
            global pipex
            self.pos[0] -= 3
            self.rect.center = self.pos
    
    def draw_pipes():
        newpipe = Pipe1('flappybirdpipe.png', random.randint(115, screen.get_height()))
    
    while True:
        draw_pipes()
        for event in pygame.event.get():
            if event.type == pygame.QUIT():
                pygame.quit()
                sys.exit()
    

This is the error message:

    
    
    Traceback (most recent call last):
      File "C:/Users/User/Downloads/Python Code/flappybird/flappybird.py", line 51, in <module>
        draw_pipes()
      File "C:/Users/User/Downloads/Python Code/flappybird/flappybird.py", line 48, in draw_pipes
        newpipe = Pipe1('flappybirdpipe.png', random.randint(115, screen.get_height()))
      File "C:/Users/User/Downloads/Python Code/flappybird/flappybird.py", line 27, in __init__
        oppositepipe = Pipe2('flappybirdpipe2.png')
      File "C:/Users/User/Downloads/Python Code/flappybird/flappybird.py", line 39, in __init__
        self.height = 397 - Pipe1.height
    AttributeError: type object 'Pipe1' has no attribute 'height'
    


Answer: I don't think you're getting much benefit from having two different pipe
classes. The only thing that really differs between them is the image, and
each pipe basically has the same behaviour, so it makes sense to represent
both of them with a single class. I think you should try:

  * Just having a single `Pipe` class
  * Keeping the code that generates heights for the pipes separate from the `Pipe` class, as this is where your original problem arose.

Basically, you should be able to replace your `Pipe1`, `Pipe2` and
`draw_pipes()` code with:

    
    
    class Pipe(pygame.sprite.Sprite):
        def __init__(self, image, height):
            pygame.sprite.Sprite.__init__(self)
            self.image = pygame.image.load(image)
            self.rect = self.image.get_rect()
            self.height = height
            self.pos = [pipex, height]
    
        def scroll():
            # You don't actually do anything with pipex in this
            # method, so not sure why you were referring to it?
            self.pos[0] -= 3
            self.rect.center = self.pos
    
    def draw_pipes():
        pipe1_height = random.randint(115, screen.get_height())
        pipe1 = Pipe('flappybirdpipe.png', pipe1_height)
        pipe2_height = 397 - pipe1_height
        pipe2 = Pipe('flappybirdpipe2.png', pipe2_height)
    

