gpt4 book ai didi

python - __init__ 的用途是什么?

转载 作者:行者123 更新时间:2023-11-28 18:48:47 25 4
gpt4 key购买 nike

<分区>

我是 python 的新手,我正在阅读一本关于使用 pygame 创建游戏的书。我在书中看到了这段代码。我不明白为什么 Sprite.__init__(self) 是必要的。我已经运行了这段注释掉的代码,它以相同的方式运行。

class Creep(Sprite):

def __init__(
self, screen, img_filename, init_position,
init_direction, speed)

Sprite.__init__(self)

self.screen = screen
self.speed = speed
self.base_image = pygame.image.load(img_filename).convert_alpha()
self.image = self.base_image

有人知道为什么会出现这种情况以及为什么人们调用 init 吗?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com