嗨,我一直收到错误AttributeError:类型对象“Pipe1”没有属性“height” 我试图用pygame/python创建flappy bird。只是一个注释,这还没有完成,只是想看看我有没有什么大的错误。如果有帮助的话,这就是python2.7.7。在
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()
以下是错误消息:
^{pr2}$
我不认为你从拥有两个不同的管道课程中得到什么好处。它们之间唯一真正不同的是图像,而且每个管道基本上都有相同的行为,因此用一个类来表示它们是有意义的。我觉得你应该试试:
Pipe
类Pipe
类分开,如 这就是你最初的问题所在。在基本上,您应该能够将}代码替换为:
Pipe1
、Pipe2
和{相关问题 更多 >
编程相关推荐