飞扬的小鸟 pygame "birds" 未定义

Flappy Bird pygame "birds" not defined

在解决了关卡基础的上一个问题后,我在解决该问题后立即面临另一个问题。好像是同样的问题,错误依旧是

NameError: name 'birds' is not defined

但这一次,bird 实际上被定义为一个列表。上一题我在题中放了太多不必要的代码,对此我深表歉意,所以我尽量只放最重要的代码来找出问题所在。

def draw_window(win, bird, pipes, base, score):
    win.blit(BG_IMG, (0, 0))

    for pipe in pipes:
        pipe.draw(win)

    text = STAT_FONT.render('Score: ' + str(score), 1, (255, 255, 255))
    win.blit(text, (WIN_WIDTH - 10 - text.get_width(), 10))

    base.draw(win)  

    for bird in birds:   # Here is the line where the error occurs
        bird.draw(win)

    pygame.display.update()

def main(genomes, config):
    nets = []
    ge = []
    birds = []      # Here it appears defined as a list
    base = Base(730)

    for _, g in genomes:
        net = neat.nn.FeedForwardNetwork.create(g, config)
        nets.append(net)
        birds.append(Bird(230, 350))
        g.fitness = 0
        ge.append(g) 

    pipes = [Pipe(600)]
    win = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
    clock = pygame.time.Clock()

    score = 0

    run = True
    while run:
        clock.tick(30)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
                pygame.quit()
                quit()

        pipe_ind = 0
        if len(birds) > 0:
            if len(pipes) > 1 and birds[0].x > pipes[0].x + pipes[0].PIPE_TOP.get_width():
                pipe_ind = 1
        else:
            run = False
            break

        for x, bird in enumerate(birds):
            bird.move()
            ge[x].fitness += 0.1

            output = nets[x].activate((bird.y, abs(bird.y - pipes[pipe_ind].height), abs(bird.y - pipes[pipe_ind].bottom)))

            if output[0] > 0.5:
                bird.jump()

        #bird.move()
        add_pipe = False
        rem = []
        for pipe in pipes:
            for x, bird in enumerate(birds):
                if pipe.collide(bird):
                    ge[x].fitness -= 1
                    birds.pop(x)
                    nets.pop(x)
                    ge.pop(x)

                if not pipe.passed and pipe.x < bird.x:
                    pipe.passed = True
                    add_pipe = True

            if pipe.x + pipe.PIPE_TOP.get_width() < 0:
                rem.append(pipe)

            pipe.move()

        if add_pipe:
            score += 1
            for g in ge:
                g.fitness += 5
            pipes.append(Pipe(600))

        for r in rem:
            pipes.remove(r)

        for x, bird in enumerate(birds):
            if bird.y + bird.img.get_height() >= 730 or bird.y < 0:
                birds.pop(x)
                nets.pop(x)
                ge.pop(x)

        base.move()
        draw_window(win, birds, pipes, base, score)

问题是因为您尝试在函数 draw_window 中迭代列表 birds。但是变量 birds 没有在 draw_window:

的范围内定义
for bird in birds:   # Here is the line where the error occurs
   bird.draw(win)

函数 draw_window 中的参数名称必须是 birds 而不是 bird:

def draw_window(win, bird, pipes, base, score):

def draw_window(win, birds, pipes, base, score):

注意,调用函数时实际参数是列表birds:

draw_window(win, birds, pipes, base, score)