为什么我不能更改 Pygame.sprites 中 Group class 实例的 update() 方法的名称?

Why can't I change the name of the update() method for Group class instances in Pygame.sprites?

我正在从 Eric Mattis 的书中学习。我注意到有一次,当我更改方法的名称时,出现错误。

这是一个例子(为了不分散注意力,我删除了所有不必要的东西):

# Importing the Group class
from pygame.sprite import Group

# create an instance of the Group class
something = group()

# main loop
while True:

    something.update()


# Another class description file and its method

class Some_Class(Sprite):

    def __init__(self):
        super().__init__()

    def update(self):

        something is happening

书上对此只字未提,一开始我以为取update这个名字是为了写的方便,因为很清楚此处正在更新某些内容,无需另写名称。我过去常常自行决定更改方法的名称,以便更好地理解这里发生的事情,例如,不仅仅是 def update() ,而是 def screen_update()def position_update()

但是这次我运行报错了,想了半天,终于知道改不了名字了update 到别的地方。

我是否理解正确,在这种情况下,这不仅仅是方法的 运行dom 名称,而是 pygame 中的一些保留名称Group() 个对象?

您通常需要记住的是: 如果我导入 from pygame.sprite import Group 并创建 Group() class 的实例,然后访问基于方法在那个 class 上,然后我必须只使用主循环中的方法 update() ?

我只是想了解为什么会这样而不是其他...

如果这是内置方法,那我为什么要在class文件中创建一个def update()方法,因为这里我选择了给自己起名字?重点是什么?或者我只是参考这个方法并说明它会做什么?

如果我创建一个没有继承的 class 的简单实例,那么我可以为方法选择任意名称,例如:

# Import class some_class
from some_class import Some_class

# create an instance of the class Some_class
some_class = Some_class()

# main loop
while True:

    some_class.some_method()

    # OR THIS:

    some_class.updating_something_because_it_needs_to_be_updated()


# Another class description file and its method

class Some_class():

    def __init__(self):

    def updating_something_because_it_needs_to_be_updated(self):

        something is happening
        ...

方法名称必须是 update,因为这是一个 pygame 功能。 pygame.sprite.Group.update() is a methods which is provided by pygame.sprite.Group, it delegates to the update methods of the contained pygame.sprite.Sprites.

pygame.sprite.Group.update():

Calls the update() method on all Sprites in the Group. [...]

简答

是的,在您创建的精灵 class 中始终必须调用该方法 update()(在您的情况下为 Some_Class)。此外,您在主循环中调用 Group 的 update() 方法是正确的,但您必须确保将您的精灵添加到组中!

更长的答案

您在this图片中看到的是pygame库中定义的精灵class。当您编写 Some_Class(Sprite) 时,您将其用作基础 class 并在您自己定义的 class 中添加其他功能。 图中也可以看到Spriteclass有更新功能,默认为空。在 Some_Class 中编写您自己的 update() 函数时,您覆盖了该方法,它执行您希望它执行的操作。

当你问“为什么这个方法有名字 'update' 很重要?”,然后看看 Group class 来自 pygame 库(不要被名称 'AbstractGroup' 打扰)。 Group.update() 方法从组内的每个精灵调用 update() 函数。所以如果你不命名方法 update() 那么它就不会在组中被调用。

最后,您要么按照命名约定在主循环中使用 Group.update(),要么创建自己的更新函数并自行调用它们。您可以在下面看到包含这两个选项的代码示例。

import pygame


class Some_Class(pygame.sprite.Sprite):

    def __init__(self):
        pygame.sprite.Sprite.__init__(self)

    def update(self):
        # pygames update method, called by the Group.update()
        pass

    def my_own_update(self):
        # your own update method, that you have to call yourself
        pass


pygame.init()
window = pygame.display.set_mode((800, 800))

# Create sprite and group
sprite = Some_Class()
group = pygame.sprite.Group()

# Make the sprite a part of the group
group.add(sprite)

while True:

    # Either update the sprite through the group or use your own update method
    group.update()
    sprite.my_own_update()

    pygame.display.update()