我对编码还很陌生,所以这可能很简单。我在代码中一直停留在此错误上一段时间。我复制了我认为很重要的代码。当我调用highlightSide函数时会出现问题,因为它会检查squares [i] [j] .contains(x,y)。我认为,既然它说的是'NoneType'对象,那么这将意味着在2d数组中创建正方形存在问题,但是我不确定。如果这是问题所在,那么我应该尝试解决各种可能的解决方案,但又会导致错误以不同的方式传达,我该如何解决呢?
def highlightSide(x, y):
rows = len(squares)
cols = len(squares[0])
for i in range(rows):
for j in range(cols):
if squares[i][j].contains(x, y):
side = squares[i][j].highlightSide(x,y)
break
else:
continue
break
def newGame():
#random chance of who starts the new game
playersTurn = random.randint(0, 1) > 0
#set up the squares
squares = np.empty((GAME_SIZE, GAME_SIZE), dtype=object)
for i in range(GAME_SIZE):
for j in range(GAME_SIZE):
squares[i][j] = (createSquare(w, dotX(j), dotY(i), BOX))
#Create square from rectangle
class createSquare:
def __init__(self, canvas,x,y,length):
self.canvas = canvas
self.length = length
self.length = height
self.bot = y + length
self.left = x
self.right = x + length
self.top = y
self.highlight = None
self.NumSelected = 0
self.owner = None
#look into to make sure it work
#to see if in the range of the square
def contains(self, x, y):
if y>self.bot and x < self.right and y <= self.top and y >= self.left:
return True
else:
return False
def highlightSide(self, x, y):
#calcualte the distance to each side
dBot = self.bot - y
dLeft = x - self.left
dRight = self.right - x
dTop = y - self.top
#find which of them is the closest
dClosest = min(dBot, dLeft, dRight, dTop)
#make sure the color changes to highlighted color for closest if doesn't already have line
if dClosest == dBot and not self.sideBot.selected:
self.highlight = constants.BOT
elif dClosest == dLeft and not self.sideLeft.selected:
self.highlight = constants.LEFT
elif dClosest == dRight and not self.sideRight.selected:
self.highlight = constants.RIGHT
elif dClosest == dTop and not self.sideTop.selected:
self.highlight = constants.Top
#return the highlight
return self.highlight