簡體   English   中英

調用元類基數時鍵入錯誤

[英]Type Error when calling the metaclass bases

我正在為研究中的練習編寫此(現在很混亂)代碼。

添加完后

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 Update 4實例中運行此代碼

  • 糾正了@Isma指出的代碼中的某些錯誤

您的代碼運行正常,但以下方法中缺少一些“自我”引用(您直接調用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__方法來初始化您的超類,盡管這不是最好的方法,但它可能會幫助您發現問題,請閱讀以下內容以獲取更多信息: 使用__init __()方法了解Python super()

DrawableGameObject.__init__(self)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM