2013-07-08 148 views
0

在目錄樹是這個樣子問題與Python模塊中導入

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

你需要從模塊導入類:

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 style guide PEP-8建議您使用全部小寫的名字你的模塊文件,以避免混淆模塊與模塊的內容。

0

您在Rectangle模塊中導入了Rectangle模塊,但未導入Rectangle類。

要引用模塊中的類,一般語法是module.Class。所以你可以改變你對Rectangle.Rectangle的引用,或者改爲直接導入類:

from Rectangle import Rectangle