【发布时间】:2015-03-12 21:51:08
【问题描述】:
我正在尝试为国际象棋游戏创建 Pawn 类,但在“can_move”函数的第一个 if 语句中出现错误“NameError: name 'self' is not defined”,即使我将颜色定义为在初始化函数中输入?有什么想法吗?
class Pawn(object):
def __init__(self, x, y, colour):
#store the pawns coords
self.x = x
self.y = y
#store the colour of the piece
self.colour = colour
#store if it moved yet or not
self.moved = False
self.print_value = self.colour+"P"
def can_move(newx, newy):
#func to check if piece can move along those coords
if self.colour == "W":
pieces = game_tracker.live_white
elif self.colour == "B":
pieces = game_tracker.live_black
【问题讨论】:
-
将自己传递给 can_move。