Getting an Error Trying to Create an Object in Pyt

2019-08-03 17:45发布

问题:

I am trying to create an object from a class in python but I am getting an Error, "e_tank = EnemyTank() TypeError: 'Group' object is not callable"

I am not sure what this means, I have tried Google but I couldn't get a clear answer on what is causing this error. Does anyone understand why I am unable to create an object from my EnemyTank Class?

Here is my code:

#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()

回答1:

You've declared a class called EnemyTank and then you've overwritten it with this line:

EnemyTank = allgroup

EnemyTank after this point is not a class, but a group, and no longer callable. What you want to do is:

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


回答2:

Here is your problem :

You instantiate the Group class :

allgroup = pygame.sprite.Group() :

Then you put the object in a EnemyTank variable

EnemyTank = allgroup

Then you try to call the EnemyTank object :

e_tank = EnemyTank()


回答3:

Looks like you've instantiated a Group class:

allgroup = pygame.sprite.Group()

And then you try to call the object:

EnemyTank = allgroup
e_tank = EnemyTank()

I guess pygame.sprite.Group class does not define a __call__() method so it cannot be called



回答4:

In addition to the already mentioned, I should also add that you are very close in another instance to repeating variable names, and you should really not do that. Here's every instance of something like EnemyTank I can see.

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


回答5:

You define a class EnemyTank but then create a new variable with the same name. EnemyTank

bunker = Bunker()5 # this is also invalid

To use the spritegroups, you do something like

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

then: drawing

tank.draw(screen)

collision: spritecollide or any of the other collision functions in : http://www.pygame.org/docs/ref/sprite.html

see also: http://www.pygame.org/docs/ref/sprite.html#pygame.sprite.Group