2016-12-31 22 views
0

所以我剛剛學會了如何使用類和我做了這個類,但我不能讓它跑我使用類提供一個錯誤,但沒有運行?

spacecraft1 = spacecraft(500,500,100,34) 

使課堂 然後循環我使用spacecraft1.motion()更新/移動。從所有這一切我得到錯誤:

Traceback (most recent call last): 
    File "C:\Users\Redstone3\Desktop\Desctop\Centers\game5\gameBuild 0.04.py", line 107, in <module> 
    spacecraft1 = spacecraft(500,500,100,34) 
TypeError: 'pygame.Surface' object is not callable 

,並有可能創造這樣的飛船在同一時間的40沒有確定他們的每個人都像

spacecraft1 = spacecraft(500,500,100,34) 
spacecraft2 = spacecraft(500,500,100,34) 
spacecraft3 = spacecraft(500,500,100,34) 
spacecr... 

這裏是我的課,如果有幫助的東西。這個類和主要的遊戲文件都在單獨的文件中,但在相同的文件夾中。

import pygame 
import random 



class BaseClass(pygame.sprite.Sprite): 

allsprites = pygame.sprite.Group() 
def __init__(self, x, y, width, height,): 


    pygame.sprite.Sprite.__init__(self) 
    BaseClass.allsprites.add(self) 



    self.shipDefaultUp = pygame.image.load("textures/ships/shipDefault/shipUP.png") 
    self.shipDefaultRight = pygame.image.load("textures/ships/shipDefault/shipRIGHT.png") 
    self.shipDefaultDown = pygame.image.load("textures/ships/shipDefault/shipDOWN.png") 
    self.shipDefaultLeft = pygame.image.load("textures/ships/shipDefault/shipLEFT.png") 

    self.shipCargo1Up = pygame.image.load("textures/ships/shipCargo1/shipCargo1UP.png") 
    self.shipCargo1Right = pygame.image.load("textures/ships/shipCargo1/shipCargo1RIGHT.png") 
    self.shipCargo1Down = pygame.image.load("textures/ships/shipCargo1/shipCargo1DOWN.png") 
    self.shipCargo1Left = pygame.image.load("textures/ships/shipCargo1/shipCargo1LEFT.png") 

    self.shipCargo2Up = pygame.image.load("textures/ships/shipCargo2/shipCargo2UP.png") 
    self.shipCargo2Right = pygame.image.load("textures/ships/shipCargo2/shipCargo2RIGHT.png") 
    self.shipCargo2Down = pygame.image.load("textures/ships/shipCargo2/shipCargo2DOWN.png") 
    self.shipCargo2Left = pygame.image.load("textures/ships/shipCargo2/shipCargo2LEFT.png") 



    self.image = pygame.image.load("textures/ships/shipDefault/shipUP.png") 

    self.rect = self.image.get_rect() 
    self.rect.x = x 
    self.rect.y = y 

    self.width = width 
    self.height = height 

    #self.dirrection = random.randrange(1,5) 
    self.dirrection = 1 
    self.timer = random.randrange(10,50) 
    self.speed = random.randrange(2,10) 

    self.shiptype = 3#random.randrange(1,3) 

    #shiptypes# 
    #1 = shipDefault 
    #2 = shipCargo1 
    #3 = shipCargo2 



    self.move1 = ((1),(2),(4)) 
    self.move2 = ((1),(2),(3)) 
    self.move3 = ((2),(3),(4)) 
    self.move4 = ((1),(3),(4)) 

class spacecraft(BaseClass): 
    ships = pygame.sprite.Group() 



    def __init__(self, x, y, width, height,): 
     BaseClass.__init__(self, x, y, width, height,) 
     spacecraft.ships.add(self) 

    def motion(self): 

     #1 = UP 
     #2 = RIGHT 
     #3 = DOWN 
     #4 = LEFT 


     self.timer -=1 

     if self.dirrection == 1: ##############SHIP UP 
      self.rect.y -=self.speed 

      if self.shiptype == 1: 
       self.image = self.shipDefaultUp 

      if self.shiptype == 2: 
       self.image = self.shipCargo1Up 



     if self.dirrection == 2:################SHIP RIGHT 
      self.rect.x +=self.speed 

      if self.shiptype == 1: 
       self.image = self.shipDefaultRight 

      if self.shiptype == 2: 
       self.image = self.shipCargo1Right 

      if self.shiptype == 3: 
       self.image = self.shipCargo2Right 



     if self.dirrection == 3: ##############SHIP DOWN 
      self.rect.y +=self.speed 

      if self.shiptype == 1: 
       self.image = self.shipDefaultDown 

      if self.shiptype == 2: 
       self.image = self.shipCargo1Down 

      if self.shiptype == 3: 
       self.image = self.shipCargo2Down 


     if self.dirrection == 4: ################SHIP LEFT 
      self.rect.x -=self.speed 

      if self.shiptype == 1: 
       self.image = self.shipDefaultLeft 

      if self.shiptype == 2: 
       self.image = self.shipCargo1Left 

      if self.shiptype == 3: 
       self.image = self.shipCargo2Left 



     if self.dirrection == 5: ####IF SHIP IS NOT MOVING#### 
      print("loitter") 

     if self.timer < 0:####GET NEW DIRECTION FOR SHIP#### 
      self.dirrection = random.randrange(1,6) 
      self.timer = random.randrange(10,50) 

Full code here(Google Drive)

+0

對於簡單的問題:您可以使用'list'來創建一堆飛船並將它們全部存儲在一個變量中。 –

+0

爲類使用'CamelCase'名稱 - '類Spacecraft(BaseClass):' - 它使代碼更具可讀性。 – furas

+0

怎麼樣? 'epicList = []'然後呢?我需要使用類似'spacecraft1.append?'的東西嗎? – cmdtvt

回答

1

鑑於錯誤說「面對象不可調用的」,這意味着在你的函數某些時候,你以爲你是實例航天器的實例,您之前已經創建了一個包含一個局部變量一個pygame.Surface對象,覆蓋全球範圍內的類「航天器」。你的類不是問題所在,它是你試圖實例化它的函數中的前面的代碼。

例如

class spacecraft: 
    ... 

def someOtherFunction: 
    ... 
    # this valid line of code is actually where the problem 
    # is as it hides your spacecraft class with a local variable instead. 
    spacecraft = some pygame.Surface instance 
    ... 
    spacecraft1 = spacecraft(...) 
    ... 
+0

所以如果我有這個權利重寫pygame.Surface? – cmdtvt

+0

不,我已更新我的回覆以說明可能發生的情況。 –

+0

我的課沒有得到任何東西/ def的應該恢復'宇宙飛船=別的東西至少我看不到它。我的所有課程都在我的主要職位上。 – cmdtvt

相關問題