0

在目录树看起来像这样

PyPong + Main.py + Rectangle.py

现在,我在 Main.py 中像这样导入了 Rectangle.py

import pygame, sys, Rectangle

但是,每当我尝试像这里一样创建类 Rectangle.py 的实例时

rectangles.append(Rectangle(400 + x * rectangleWidth + x * 10, 30 + y * rectangleHeight + y * 10, rectangleWidth, rectangleHeight, (randint(0, 255), randint(0, 255), randint(0, 255)), screen))

进入这个数组

rectangles = []

我收到此错误:

TypeError: 'module' object is not callable

任何帮助是极大的赞赏

另外,这里是完整的 Rectangle.py

class Rectangle:

    y = 0
    x = 0
    width = 0
    height = 0
    color = 0
    screen = 0

    GO_UP = 1
    GO_DOWN = 2
    GO_LEFT = 3
    GO_RIGHT = 4

    closeX = 0
    closeY = 0

    removed = False

    def __init__(self, x, y, width, height, color, screen):
        self.x = x
        self.y = y
        self.height = height
        self.width = width
        self.color = color
        self.screen = screen

    def render(self):
        pygame.draw.rect(self.screen, self.color, (self.x, self.y, self.width, self.height), 0)
        pass

    def intersects(self, x, y, r):
        #TOP SIDE
        self.closeX = 0
        self.closeY = 0
        intersectsTop = True;
        if x <= self.x: self.closeX = self.x
        elif x >= self.x + self.width: self.closeX = self.x + self.width
        else: self.closeX = self.x
        self.closeY = self.y
        if abs(x - self.closeX) >= r: intersectsTop = False
        if abs(y - self.closeY) >= r: intersectsTop = False
        if intersectsTop: 
            self.remove()
            return self.GO_UP

        #LEFT SIDE
        self.closeX = 0
        self.closeY = 0
        intersectsLeft = True
        if y <= self.y: self.closeY = self.y
        elif y >= self.y + self.height: self.closeY = self.y + self.height
        else: self.closeY = y;
        self.closeX = self.x
        if abs(x - self.closeX) >= r: intersectsLeft = False
        if abs(y - self.closeY) >= r: intersectsLeft = False
        if intersectsLeft: 
            self.remove()
            return self.GO_LEFT

        #RIGHT SIDE
        self.closeX = 0
        self.closeY = 0
        intersectsRight = True
        if y <= self.y: self.closeY = self.y
        elif y >= self.y + self.height: self.closeY = self.y + self.height
        else: self.closeY = y;
        self.closeX = self.x + self.width
        if abs(x - self.closeX) >= r: intersectsRight = False
        if abs(y - self.closeY) >= r: intersectsRight = False
        if intersectsRight: 
            self.remove()
            return self.GO_RIGHT

        #BOTTOM SIDE
        self.closeX = 0
        self.closeY = 0
        intersectsBottom = True;
        if x <= self.x: self.closeX = self.x
        elif x >= self.x + self.width: self.closeX = self.x + self.width
        else: self.closeX = self.x
        self.closeY = self.y + self.height
        if abs(x - self.closeX) >= r: intersectsBottom = False
        if abs(y - self.closeY) >= r: intersectsBottom = False
        if intersectsBottom: 
            self.remove()
            return self.GO_DOWN
        pass

    def remove(self):
        self.removed = True
        pass
4

2 回答 2

4

您需要从模块中导入类:

from Rectangle import Rectangle

或将类称为您导入的模块的属性:

rectangles.append(Rectangle.Rectangle(400 + x * rectangleWidth + x * 10, 30 + y * rectangleHeight + y * 10, rectangleWidth, rectangleHeight, (randint(0, 255), randint(0, 255), randint(0, 255)), screen))

这是Python 样式指南 PEP-8建议您为模块文件使用全小写名称的原因之一,以避免将模块与模块的内容混淆。

于 2013-07-08T16:20:45.953 回答
0

您导入了Rectangle模块,但没有导入RectangleRectangle 模块中的类。

要引用模块中的类,一般语法是module.Class. 因此,您可以更改对类的引用Rectangle.Rectangle或直接导入该类:

from Rectangle import Rectangle
于 2013-07-08T16:38:13.847 回答