将对象添加到二维列表时抛出错误?
Throwing error when adding object to a 2d list?
我正在尝试初始化一个游戏板,我在其中将一个棋子对象添加到棋盘的内部表示中。但是我不断收到错误消息:
File "/Users/student/PycharmProjects/pythonProject4/Board.py", line 20, in __init__
self.board[row].append(Piece(row, col))
TypeError: 'module' object is not callable
import Constants as c
class Piece:
def __init__(self, x, y):
self.x = 0
self.y = 0
self.isActive = False
self.pieces = 12
self.radius = c.SQUARE // 2 - 8
def getLoc(self, row, col):
self.x = c.SQUARE * col + c.SQUARE // 2
self.y = c.SQUARE * row + c.SQUARE // 2
import Constants as con
import pygame as pg
import Player as Piece
class Board:
columns = 8
rows = 8
def __init__(self):
self.board = []
for row in range(8):
self.board.append([])
for col in range(8):
even = (row + col) % 2 == 1
if even:
self.board[row].append(0)
elif row <= 2:
self.board[row].append(Piece(row, col))
elif row >= 5:
self.board[row].append(Piece(row, col))
else:
self.board[row].append(1)
有人能帮我解决这个问题吗?我想做跳棋游戏,目前我有一个 2d 列表,其中 0=从未占用,1=当前未占用,PieceTeamOne(r,c)
和 PieceTeamTwo(r,c)
作为,我认为您的代码中的问题出在这部分:
import Player as Piece
您应该将该部分更改为:
from Player import Piece
我正在尝试初始化一个游戏板,我在其中将一个棋子对象添加到棋盘的内部表示中。但是我不断收到错误消息:
File "/Users/student/PycharmProjects/pythonProject4/Board.py", line 20, in __init__ self.board[row].append(Piece(row, col)) TypeError: 'module' object is not callable
import Constants as c
class Piece:
def __init__(self, x, y):
self.x = 0
self.y = 0
self.isActive = False
self.pieces = 12
self.radius = c.SQUARE // 2 - 8
def getLoc(self, row, col):
self.x = c.SQUARE * col + c.SQUARE // 2
self.y = c.SQUARE * row + c.SQUARE // 2
import Constants as con
import pygame as pg
import Player as Piece
class Board:
columns = 8
rows = 8
def __init__(self):
self.board = []
for row in range(8):
self.board.append([])
for col in range(8):
even = (row + col) % 2 == 1
if even:
self.board[row].append(0)
elif row <= 2:
self.board[row].append(Piece(row, col))
elif row >= 5:
self.board[row].append(Piece(row, col))
else:
self.board[row].append(1)
有人能帮我解决这个问题吗?我想做跳棋游戏,目前我有一个 2d 列表,其中 0=从未占用,1=当前未占用,PieceTeamOne(r,c)
和 PieceTeamTwo(r,c)
作为
import Player as Piece
您应该将该部分更改为:
from Player import Piece