我将coin.py
导入到main_game.py
中,如下所示:
import coin
coin.py
包含一个类Coin
,它不带附加参数。以下是coin.py
的内容
class Coin(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("gold.png").convert()
self.image.set_colorkey(BLACK)
self.bump_sound = pygame.mixer.Sound("coin.wav")
self.rect = self.image.get_rect()
当我试图实例化这个类的对象时,如下所示:
coin = coin.Coin()
我得到以下错误:
Traceback (most recent call last):
File "C:\dev_projects\python\pygame_projects\final_project\main_game.py", line 39, in <module>
coin = coin.Coin()
AttributeError: 'Coin' object has no attribute 'Coin'
最初的研究提出了一个缩进问题,所以我重新创建了这个小类,并将其保存在一个新文件中,并删除了这个旧文件。我也有同样的问题。
接下来,我将coin.py
重命名为treasure.py
,更新了main_game.py
文件,问题就消失了。我把它重命名为coi.py
来测试它,这也是没有问题的。当我将文件重命名为coin.py
时,问题再次出现。
再说一遍,我没有在这里摆弄任何类型的缩进,只是重命名为coin.py
。我可以继续前进,因为我有一个解决办法,但我想知道到底是什么原因导致这个问题的具体名称硬币?
你知道coin.py
会有什么问题吗?当文件名为help(coin)
时,这里是coin.py
:有关模块硬币对象中硬币的帮助:
class Coin(pygame.sprite.Sprite)
| simple base class for visible game objects
|
| pygame.sprite.Sprite(*groups): return Sprite
|
| The base class for visible game objects. Derived classes will want to
| override the Sprite.update() method and assign Sprite.image and Sprite.rect
| attributes. The initializer can accept any number of Group instances that
| the Sprite will become a member of.
|
| When subclassing the Sprite class, be sure to call the base initializer
| before adding the Sprite to Groups.
|
| Method resolution order:
| Coin
| pygame.sprite.Sprite
| builtins.object
|
| Methods defined here:
|
| __init__(self)
| Initialize self. See help(type(self)) for accurate signature.
|
| ----------------------------------------------------------------------
| Methods inherited from pygame.sprite.Sprite:
|
| __repr__(self)
| Return repr(self).
|
| add(self, *groups)
| add the sprite to groups
|
| Sprite.add(*groups): return None
|
| Any number of Group instances can be passed as arguments. The
| Sprite will be added to the Groups it is not already a member of.
|
| add_internal(self, group)
|
| alive(self)
| does the sprite belong to any groups
|
| Sprite.alive(): return bool
|
| Returns True when the Sprite belongs to one or more Groups.
|
| groups(self)
| list of Groups that contain this Sprite
|
| Sprite.groups(): return group_list
|
| Returns a list of all the Groups that contain this Sprite.
|
| kill(self)
| remove the Sprite from all Groups
|
| Sprite.kill(): return None
|
| The Sprite is removed from all the Groups that contain it. This won't
| change anything about the state of the Sprite. It is possible to
| continue to use the Sprite after this method has been called, including
| adding it to Groups.
|
| remove(self, *groups)
| remove the sprite from groups
|
| Sprite.remove(*groups): return None
|
| Any number of Group instances can be passed as arguments. The Sprite
| will be removed from the Groups it is currently a member of.
|
| remove_internal(self, group)
|
| update(self, *args)
| method to control sprite behavior
|
| Sprite.update(*args):
|
| The default implementation of this method does nothing; it's just a
| convenient "hook" that you can override. This method is called by
| Group.update() with whatever arguments you give it.
|
| There is no need to use this method if not using the convenience
| method by the same name in the Group class.
|
| ----------------------------------------------------------------------
| Data descriptors inherited from pygame.sprite.Sprite:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
因此,我可以调用硬币文件的帮助,它告诉我它有一个类硬币,但是即使我试图在它抛出的解释器中实例化它:
>>> coin = coin.Coin()
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
coin = coin.Coin()
AttributeError: 'Coin' object has no attribute 'Coin'
也许是来自pygame
的干扰吗?
发布于 2022-02-04 04:19:33
问题是,您正在声明一个名为硬币的新变量,然后尝试从它访问硬币类。这意味着该文件不再与变量硬币相关联,因此无法访问硬币class.To,该文件名为coin.py,您需要将变量硬币重命名为其他文件。
https://stackoverflow.com/questions/70985981
复制相似问题