因此,自从我尝试将代码重构为不同的文件以来,这个 Python 问题一直给我带来问题。我有一个名为 object.py 的文件,其中相关代码是:
class Object:
#this is a generic object: the player, a monster, an item, the stairs...
#it's always represented by a character on screen.
def __init__(self, x, y, char, color):
self.x = x
self.y = y
self.char = char
self.color = color
def move(self, dx, dy):
#move by the given amount, if the destination is not blocked
#if not map[self.x + dx][self.y + dy].blocked:
self.x += dx
self.y += dy
现在,当我尝试专门编译此文件时,我收到此错误:
TypeError: unbound method __init__() must be called with Object instance as first argument (got int instance instead)
试图调用它的代码是:
player = object_info.Object.__init__(BurglaryConstants.SCREEN_WIDTH/2, BurglaryConstants.SCREEN_HEIGHT/2, '@', libtcod.white)
编译时导致此错误:
AttributeError: 'module' object has no attribute 'Object'
那么这一切到底是怎么回事,我应该如何重构呢?另外我认为拥有一个名为 Object 的类不是一个很好的编码实践,对吗?
谢谢你的帮助!