そのため、コードを別のファイルにリファクタリングしようとして以来、この 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 というクラスを持つことは、あまり良いコーディング方法ではないと思いますよね?
ご協力いただきありがとうございます!