python - Getting AttributeError: type object 'Pipe1' has no attribute 'height' -
python - Getting AttributeError: type object 'Pipe1' has no attribute 'height' -
hi maintain getting error attributeerror: type object 'pipe1' has no attribute 'height' trying create flappy bird pygame/python. note, isn't completed yet, wanted see if had major errors. python 2.7.7 if 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() event in pygame.event.get(): if event.type == pygame.quit(): pygame.quit() sys.exit()
this error message:
traceback (most recent phone 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'
i don't think you're getting much benefit having 2 different pipe classes. thing differs between them image, , each pipe has same behaviour, makes sense represent both of them single class. think should try:
just having singlepipe
class keeping code generates heights pipes separate pipe
class, original problem arose. basically, should able replace pipe1
, pipe2
, 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(): # don't pipex in # method, not sure why referring 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)
python pygame
Comments
Post a Comment