2012-11-15 102 views
1

我想從python中的類創建一個對象,但我得到一個錯誤,「e_tank = EnemyTank() TypeError:'Group'對象不可調用」嘗試在Python中創建一個對象時出錯

我不確定這是什麼意思,我試過谷歌,但我無法得到明確的答案是什麼導致這個錯誤。有誰知道爲什麼我無法從我的EnemyTank Class創建一個對象?

這裏是我的代碼:

#Image Variables 
bg = 'bg.jpg' 
bunk = 'bunker.png' 
enemytank = 'enemy-tank.png' 

#Import Pygame Modules 
import pygame, sys 
from pygame.locals import * 

#Initializing the Screen 
pygame.init() 
screen = pygame.display.set_mode((640,360), 0, 32) 

background = pygame.image.load(bg).convert() 



bunker_x, bunker_y = (160,0) 



class EnemyTank(pygame.sprite.Sprite): 
    e_tank = pygame.image.load(enemytank).convert_alpha() 
    def __init__(self, startpos): 
     pygame.sprite.Sprite.__init__(self, self.groups) 
     self.pos = startpos 
     self.image = EnemyTank.image 
     self.rect = self.image.get_rect() 
    def update(self): 
     self.rect.center = self.pos 

class Bunker(pygame.sprite.Sprite): 
    bunker = pygame.image.load(bunk).convert_alpha() 
    def __init__(self, startpos): 
     pygame.spriter.Sprite.__init__(self, self.groups) 
     self.pos = startpos 
     self.image = Bunker.image 
     self.rect = self.image.get_rect() 
    def getCollisionObjects(self, EnemyTank): 
     if (EnemyTank not in self._allgroup, False): 
      return False 
     self._allgroup.remove(EnemyTank) 
     result = pygame.sprite.spritecollide(EnemyTank, self._allgroup, False) 
     self._allgroup.add(EnemyTank) 
    def update(self): 
     self.rect.center = self.pos 


#Setting Up The Animation 
x = 0 
clock = pygame.time.Clock() 
speed = 250 

allgroup = pygame.sprite.Group() 
EnemyTank = allgroup 
Bunker = allgroup 

e_tank = EnemyTank() 
bunker = Bunker()5 

#Main Loop 
while True: 
    for event in pygame.event.get(): 
     if event.type == QUIT: 
      pygame.quit() 
      sys.exit() 

    screen.blit(background, (0,0)) 
    screen.blit(bunker, (bunker_x, bunker_y)) 
    screen.blit(e_tank, (x, 0)) 
    pygame.display.flip() 

    #Animation 
    milli = clock.tick() 
    seconds = milli/1000. 
    dm = seconds*speed 
    x += dm 

    if x>640: 
     x=0 


    #Update the Screen 
    pygame.display.update() 
+0

與其要求讀者仔細閱讀所有代碼,能否剪掉那些不必要的位,並嘗試創建一個最小的問題示例?你也可以通過這種方式親自接近答案! –

+0

下一次,如果您將錯誤發生在行上,將會有所幫助。 – PygameNerd

回答

4

你已經宣佈了一個名爲EnemyTank類,然後你用這條線覆蓋它:

EnemyTank = allgroup 

EnemyTank後這點不一個班級,但一個班級,不再可以召回。你想要做的是:

allgroup pygame.sprite.Group() 
e_tank = EnemyTank() 
allgroup.add(e_tank) 
# Or.. 
e_tank.add(allgroup) 
1

這裏是你的問題:

您實例化集團

allgroup = pygame.sprite.Group() : 

然後你把對象在EnemyTank變量

EnemyTank = allgroup 

然後嘗試調用EnemyTank 對象

e_tank = EnemyTank() 
1

看起來你已經實例化的組類:

allgroup = pygame.sprite.Group() 

然後你嘗試調用該對象:

EnemyTank = allgroup 
e_tank = EnemyTank() 

我想pygame.sprite.Group類沒有定義__call__()方法,所以它不能被稱爲

0

除了已經提到的,我還應該補充說你在另一個實例中非常接近重複變量名稱,並且你應該不這麼做。以下是我可以看到的所有例如EnemyTank

enemytank = 'enemy-tank.png' 
class EnemyTank(pygame.sprite.Sprite): 
EnemyTank = allgroup 
0

您定義了一個類EnemyTank,但隨後創建一個具有相同名稱的新變量。EnemyTank

bunker = Bunker()5 # this is also invalid

要使用spritegroups,你這樣做

player = PlayerTank() 
tanks = [ Tank() for x in range(5) ] 
tanks.append(player) 

則:拉絲

tank.draw(screen) 

碰撞: spritecollide或任何其他衝突功能:http://www.pygame.org/docs/ref/sprite.html

另請參閱:http://www.pygame.org/docs/ref/sprite.html#pygame.sprite.Group

相關問題