I received a name error when trying to call a method

泪湿孤枕 提交于 2020-01-24 19:25:31

问题


I have the following function which controls the spawning of the enemies and what they do. At the end of it I create multiple enemies using the same constructor. However, when in the main loop I call the method of those objects I get a name error.

My function:

#cretating and manipulating enemies
def enemy_actions(enemies):

    free_lanes = 0
    free_lane_positions = []
    new_enemies_lanes = []

    #going through all lanes
    for i in lanes:
        lane_taken = i[1]   

        if not lane_taken:
            #counting how many free lanes there are
            free_lanes = free_lanes + 1
            #adding free lane position to a list
            free_lane_positions.append(i[0])

    #if atleast 2 lanes are free then we randomly select how many new enemies we will add
    if free_lanes > 1:
        #randomly selecting how many enemies will be added
        number_of_enemies = random.randint(1,3)

    #repeating action for the number of enemies required
    for i in range(number_of_enemies):
        #randomly selecting lanes for enemies
        lane_x = random.choice(free_lane_positions)

        #adding it to the list of taken lanes
        new_enemies_lanes.append(lane_x)

        #removing taken up lane from list of free lanes
        free_lane_positions.remove(lane_x)

        #marking lane_x as taken in lanes
        for i in lanes:
            if i[0] == lane_x:
                i.remove(False)
                i.append(True)

    #(self, place, x, y, length, width, path, speed):
    #building enemy 
    for i in new_enemies_lanes:
        Enemy = enemy(screen, i, enemy_y_start, 60, 60, enemy_path, random.randint(3,8))
        enemies.append(Enemy)

    #debugging
    print(enemies)

And here is the main game loop where I call the method Enemy.load().

#main loop
while not done:

        #checking for game events
        for event in pygame.event.get():

                #quitting game when window is closed
                if event.type == pygame.QUIT:
                        done = True

        #detecting key presses
        pressed = pygame.key.get_pressed()
        if pressed[pygame.K_a]:Player.move_left()
        if pressed[pygame.K_d]:Player.move_right()

        #checking for crushing
        crush_detected = crush_detection() 
        if crush_detected:
            game_over()

        #clear display
        screen.fill(0)

        #drawing the background
        screen.blit(background_image, [0, 0])

        #drawing the enemy
        for i in enemies:
            Enemy.load()

        #loading the player
        Player.load()

        #update display
        pygame.display.flip() 

        #controls FPS
        clock.tick(60)

And the error I receive:

Traceback (most recent call last):
  File "Pygame.py", line 234, in <module>
    Enemy.load()

There is probably an easy fix however I haven't figured it out. Please tell me if more code is required.

Thanks!


回答1:


You're iterating over enemies with the variable i - not Enemy:

for i in enemies:
    Enemy.load()

Usually you'd want something like:

for enemy in enemies:
    enemy.Load()

That way you're actually invoking the method on the object of your current iteration.



来源:https://stackoverflow.com/questions/59013177/i-received-a-name-error-when-trying-to-call-a-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!