尝试调用方法时收到名称错误

尤金·莱文森

我具有以下功能,用于控制敌人的产卵及其行为。最后,我使用相同的构造函数创建了多个敌人。但是,当在主循环中调用这些对象的方法时,会得到一个name error

我的功能:

#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)

这是我调用方法的主要游戏循环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)

和我收到的错误:

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

可能有一个简单的解决方法,但是我还没有弄清楚。请告诉我是否需要更多代码。

谢谢!

马特·林德

您正在遍历enemies变量i-不是Enemy

for i in enemies:
    Enemy.load()

通常,您会想要以下内容:

for enemy in enemies:
    enemy.Load()

这样,您实际上是在当前迭代的对象上调用该方法。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

尝试创建XML文件时,我不断收到错误消息

来自分类Dev

尝试更新Intellij时收到OutOfMemory错误

来自分类Dev

FullCalendar:尝试使用json文件显示事件时收到错误

来自分类Dev

尝试将focus()调用到元素ID时收到错误消息:“未捕获的TypeError:无法读取属性'focus'为null”

来自分类Dev

尝试上传到Cloudinary时收到令人困惑的错误

来自分类Dev

收到(400)错误的请求。尝试发送GCM消息时

来自分类Dev

尝试连接Google Awareness服务时收到错误

来自分类Dev

尝试在Python中打破while循环时收到错误消息

来自分类Dev

尝试删除文件时收到错误?

来自分类Dev

在委托方法中调用IBOutlet变量时收到错误

来自分类Dev

尝试在Python Pandas DataFrame中比较日期时收到错误

来自分类Dev

调用生成的serializer()方法时收到NoClassDefFoundError

来自分类Dev

尝试在Puppeteer调用中访问函数参数时收到ReferenceError

来自分类Dev

尝试更改标题标签的值时收到错误

来自分类Dev

尝试使用openpyxl写入Excel时收到“'dict'对象不可调用”错误

来自分类Dev

尝试运行serve -s build时收到错误消息

来自分类Dev

我收到“ Windows与[时间服务器名称]同步时发生错误”。尝试自动同步PC时间时

来自分类Dev

尝试删除文件时收到错误?

来自分类Dev

我在调用Read()之前收到错误的无效尝试访问字段

来自分类Dev

尝试上传到Cloudinary时收到令人困惑的错误

来自分类Dev

CLion(c ++)。尝试调试时收到“错误127”

来自分类Dev

收到(400)错误的请求。尝试发送GCM消息时

来自分类Dev

尝试在Python中打破while循环时收到错误消息

来自分类Dev

当我尝试使用 JavaScript 数组方法时,我不断收到错误消息

来自分类Dev

尝试按键查找值时收到错误(python)

来自分类Dev

尝试循环 if 条件时收到错误:“参数长度为零”

来自分类Dev

当我尝试调用此函数时,为什么会收到 NoneType object is not callable 的错误消息?

来自分类Dev

Django - 视图调用模型方法时的名称错误

来自分类Dev

我收到状态 401 错误:当我尝试通过 keycloak 调用我的安全 API 时未授权

Related 相关文章

  1. 1

    尝试创建XML文件时,我不断收到错误消息

  2. 2

    尝试更新Intellij时收到OutOfMemory错误

  3. 3

    FullCalendar:尝试使用json文件显示事件时收到错误

  4. 4

    尝试将focus()调用到元素ID时收到错误消息:“未捕获的TypeError:无法读取属性'focus'为null”

  5. 5

    尝试上传到Cloudinary时收到令人困惑的错误

  6. 6

    收到(400)错误的请求。尝试发送GCM消息时

  7. 7

    尝试连接Google Awareness服务时收到错误

  8. 8

    尝试在Python中打破while循环时收到错误消息

  9. 9

    尝试删除文件时收到错误?

  10. 10

    在委托方法中调用IBOutlet变量时收到错误

  11. 11

    尝试在Python Pandas DataFrame中比较日期时收到错误

  12. 12

    调用生成的serializer()方法时收到NoClassDefFoundError

  13. 13

    尝试在Puppeteer调用中访问函数参数时收到ReferenceError

  14. 14

    尝试更改标题标签的值时收到错误

  15. 15

    尝试使用openpyxl写入Excel时收到“'dict'对象不可调用”错误

  16. 16

    尝试运行serve -s build时收到错误消息

  17. 17

    我收到“ Windows与[时间服务器名称]同步时发生错误”。尝试自动同步PC时间时

  18. 18

    尝试删除文件时收到错误?

  19. 19

    我在调用Read()之前收到错误的无效尝试访问字段

  20. 20

    尝试上传到Cloudinary时收到令人困惑的错误

  21. 21

    CLion(c ++)。尝试调试时收到“错误127”

  22. 22

    收到(400)错误的请求。尝试发送GCM消息时

  23. 23

    尝试在Python中打破while循环时收到错误消息

  24. 24

    当我尝试使用 JavaScript 数组方法时,我不断收到错误消息

  25. 25

    尝试按键查找值时收到错误(python)

  26. 26

    尝试循环 if 条件时收到错误:“参数长度为零”

  27. 27

    当我尝试调用此函数时,为什么会收到 NoneType object is not callable 的错误消息?

  28. 28

    Django - 视图调用模型方法时的名称错误

  29. 29

    我收到状态 401 错误:当我尝试通过 keycloak 调用我的安全 API 时未授权

热门标签

归档