我正在研究这个(现在非常凌乱)代码,用于我的学习练习。
我添加了
之后super(TetrisBoard, self).__init__()
在我的代码中使用TetrisBoard __init__
方法,我一直收到以下错误:
# Error: Error when calling the metaclass bases
# __init__() takes exactly 3 arguments (4 given)
# Traceback (most recent call last):
# File "<maya console>", line 4, in <module>
# TypeError: Error when calling the metaclass bases
# __init__() takes exactly 3 arguments (4 given) #
我已经尝试过阅读但我真的无法理解为什么会发生这种错误以及额外的争论来自哪里。 这是我的代码:
import math
from abc import ABCMeta, abstractmethod
class Point(object):
def __init__(self, x, y):
super(Point, self).__init__()
self.x = x
self.y = y
def rotatePoint(self, centerOfRotation, radians):
coordinateRelativeToCenter = Point.pointOnNewPlane(self, centerOfRotation)
newRelativeX = round(coordinateRelativeToCenter.x * math.cos(radians) - coordinateRelativeToCenter.y * math.sin(radians))
newRelativeY = round(coordinateRelativeToCenter.x * math.sin(radians) + coordinateRelativeToCenter.y * math.cos(radians))
coordinateRelativeToOrigin = Point.pointOnNewPlane(Point(newRelativeX, newRelativeY), Point(-centerOfRotation.x, -centerOfRotation.y))
self.x = coordinateRelativeToOrigin.x
self.y = coordinateRelativeToOrigin.y
def translatePoint(self, translateX, translateY):
self.x += translateX
self.y += translateY
@classmethod
def pointOnNewPlane(cls, point, origin):
resultPoint = Point(point.x, point.y)
resultPoint.x -= origin.x
resultPoint.y -= origin.y
return resultPoint
def getX(self):
return self.x
def getY(self):
return self.y
class GameObjectManager(object):
_gameObjects = [None]
_freeId = 0
@classmethod
def nextId(cls):
id = cls._freeId
cls._updateFreeId()
return id
@classmethod
def _updateFreeId(cls):
try:
cls._freeId = cls._gameObjects[cls._freeId:].index(None)
except ValueError:
cls._gameObjects.append(None)
cls._updateFreeId
@classmethod
def addGameObject(cls, object):
cls._gameObjects[object.getId()] = object
@classmethod
def deleteGameObject(cls, id):
cls._gameObjects[id] = None
if ( id < cls._freeId ):
cls.freeId = id
@classmethod
def getObjects(cls):
return cls._gameObjects
class CollisionManager(object):
__metaclass__ = ABCMeta
@abstractmethod
def collides(self, positions):
pass
class BoardCollisionManager(CollisionManager):
def __init__(self, board):
super(BoardCollisionManager, self).__init__()
self.board = board
def collides(self, id, position):
return self.collidesWithOtherObjects(id, position) or self.collidesWithTheEnviroment(id, position)
def collidesWithOtherObjects(self, id, positions):
result = False
for position in positions:
if self.board.isCellOccupied(position) and self.board.getCell(position) != id:
result = True
return result
def collidesWithTheEnviroment(self, id, positions):
result = False
for position in positions:
if self.board.isOutsideBoardBounds(position):
result = True
return result
class GameObject(object):
STATES = {"DEFAULT":0}
def __init__(self):
super(GameObject, self).__init__()
self._id = GameObjectManager.nextId()
self.state = "DEFAULT"
@abstractmethod
def update(self):
pass
def getId(self):
return self._id
class DrawableGameObject(GameObject):
__metaclass__ = ABCMeta
DEFAULT_SPEED = 1
DEFAULT_DIRECTION = Point(0, -1)
DEFAULT_ACCELERATION = 1
def __init__(self):
super(DrawableGameObject, self).__init__()
self.speed = DrawableGameObject.DEFAULT_SPEED
self.direction = DrawableGameObject.DEFAULT_DIRECTION
self.acceleration = DrawableGameObject.DEFAULT_ACCELERATION
@abstractmethod
def draw(self, canvas):
pass
def nextPosition(self, position, direction, speed):
return Point(position.x + (direction.x * speed), position.y + (direction.y * speed))
class TetrisBoard(DrawableGameObject):
def __init__(self, width, height):
super(TetrisBoard, self).__init__()
self.width = width
self.height = height
self.state = []
for row in range(height):
self.state.append([None for dummy in range(width)])
def drawShape(self, shape):
for point in shape.getPoints():
self.state[point.getY()][point.getX()] = shape.getId()
def isCellOccupied(self, position):
return True if self.getCell(position) is not None else False
def getCell(self, position):
return self.state[position.getY()][position.getX()]
def isOutsideBoardBounds(self, position):
return True if ((position.x > self.width or position.x < 0) or (position.y > self.height or position.y < 0)) else False
def resetBoard(self):
for row in self.state:
for cell in row:
cell = None
def update(self):
self.resetBoard()
def draw(self, canvas):
pass
class Shape(GameObject):
def __init__(self, points, center=0):
super(Shape, self).__init__()
self.points = points
self.center = center
def rotateAroundCenter(self, radians):
for point in self.points:
point.rotatePoint(self.center, radians)
def getPoints(self):
return self.points
class Tetromino(DrawableGameObject):
STATES = {"FALLING":0, "ANCHORED":1, "MOVINGLEFT":3, "MOVINGRIGHT":4, "ROTATING":5}
def __init__(self, shape, collisionManager=None, position=Point(0, 0)):
super(Tetromino, self).__init__()
self.shape = shape
self.collisionManager = collisionManager
self.position = position
self.acceleration = 0
def update(self):
if ( self.state == Tetromino.STATES["ANCHORED"] ):
pass
if ( self.state == Tetromino.STATES["ROTATING"]):
self.shape.rotateAroundCenter(math.radians(90))
if ( self.state == Tetromino.STATES["MOVINGLEFT"] ):
self.direction.x = -1
if ( self.state == Tetromino.STATES["MOVINGRIGHT"]):
self.direction.x = 1
nextPositions = [self.nextPosition(position, self.direction, self.speed) for position in self.shape.getPoints()]
if ( self.collisionManager.collides(self._id, nextPositions)):
nextPositions = self.shape.getPoints()
self.shape.points = nextPositions
self.state = Tetromino.STATES["FALLING"]
def draw(self, canvas):
canvas.drawShape(self.shape)
def moveLeft(self):
self.state = Tetromino.STATES["MOVINGLEFT"]
def moveRight(self):
self.state = Tetromino.STATES["MOVINGRIGHT"]
def rotate(self):
self.state = Tetromino.STATES["ROTATING"]
感谢任何能解释我做错的人。
编辑:我不确定它是否有意义,但我在Maya 2017更新4实例中运行此代码
答案 0 :(得分:1)
您的代码工作正常,除了以下方法中缺少“自我”引用(您直接调用board而不是self.board):
def collidesWithOtherObjects(self, id, positions):
result = False
for position in positions:
if self.board.isCellOccupied(position) and self.board.getCell(position) != id:
result = True
return result
def collidesWithTheEnviroment(self, id, positions):
result = False
for position in positions:
if self.board.isOutsideBoardBounds(position):
result = True
return result
修改强>
如果仍有错误,可以尝试以不同的方式初始化超类:
如果您使用的是python 3,则可以简化您的呼叫:
super().__init__()
您也可以通过调用下面的__init__
方法来初始化超类,虽然这不是最好的方法,但它可以帮助您找到问题,请阅读以下内容以获取更多信息:{{3} }
DrawableGameObject.__init__(self)