我用tim flappy bird Python flappy bird AI教程(与NEAT一起)学习了这项技术



我在第4个视频中遇到问题。我刚刚完成了主要功能,并运行了程序以查看它的外观,然后出现了如下错误:

AttributeError: 'Pipe' object has no attribute 'set_height'

这是代码中错误产生的部分:

class Pipe:
GAP=200
VEL=5

def __init__(self,x):
self.x=x
self.height=0

self.top=0
self.bottom=0
self.PIPE_TOP=pygame.transform.flip(PIPE_IMG,False,True)
self.PIPE_BOTTOM=PIPE_IMG   
self.passed=False
self.set_height()
def set_height(self):
self.height=random.randrange(50,450)
self.top=self.height- self.PIPE_TOP.get_height()
self.bottom=self.height +self.GAP
def move(self):
self.x-=self.VEL
def draw(self,win):
win.blit(self.PIPE_TOP,(self.x,self.top))
win.blit(self.PIPE_BOTTOM,(self.x,self.bottom))

def collide(self,bird):
bird_mask=bird.get_mask()
top_mask=pygame.mask.from_surface(self.PIPE_TOP)
bottom_mask=pygame.mask.from_surface(self.PIPE_BOTTOM)
top_offset=(self.x-bird.x,self.top-round(bird.y))
bottom_offset=(self.x-bird.x,self.bottom-round(bird.y))
b_point=bird_mask.overlap(bottom_mask,bottom_offset)
t_point=bird_mask.overlap(top_mask,top_offset)
if t_point or b_point:
return True
return False 

我真的很想知道我做错了什么,这样我就可以从中吸取教训,因为这是我第一次尝试制作游戏和人工智能来运行它。谢谢你的努力。

这里的问题似乎是您的缩进。尝试将__init__内部的方法移动到类定义内部的外部。

class Pipe:
def __init__(self):
# x, y, 
def set_height(self):
# enter code here
def move(self):
# stuff
.
.
.

最新更新