Traceback (most recent call last):
  File "C:\Users\James\Desktop\pvz.py", line 64, in <module>
    Pea((10,20))
  File "C:\Users\James\Desktop\pvz.py", line 48, in __init__
    Projectiles.add(self)
  File "C:\Python27\lib\site-packages\pygame\sprite.py", line 319, in add
    sprite.add_internal(self)
  File "C:\Python27\lib\site-packages\pygame\sprite.py", line 147, in add_internal
    self.__g[group] = 0
AttributeError: 'Pea' object has no attribute '_Sprite__g'



import pygame,traceback
width = 480
height = 640
pygame.init()
pygame.mixer.init()
pygame.time.delay(200)
font = pygame.font.Font(None,50)
screen =pygame.display.set_mode((width,height))
Plants,Zombies,Projectiles = pygame.sprite.Group(),pygame.sprite.Group(),pygame.sprite.Group()
class Zombie(pygame.sprite.Sprite):
    def __init__(self,y,image_file="Zombie.png",speed=20,attack=100):
        global Zombies
        super(Zombie,self).__init__()
        self.image = pygame.image.load(image_file)
        self.rect = self.image.get_rect()
        self.rect.left, self.rect.top = (width-100,y)
        self.speed = speed
        self.eating = None
        self.attack = attack
        Zombies.add(self)
    def move(self):
        self.rect = self.rect.move(self.speeed)
    def update(self):
        if eating:
            self.eating.health  -= self.attack
        else:
            self.move()
class Plant(pygame.sprite.Sprite):
    def __init__(self,location,image_file,health=300):
        super(Plant,self).__init__()
        self.image = pygame.image.load(image_file)
        self.rect = self.image.get_rect()
        self.rect.left, self.rect.top = location
        self.health = 0
        self.location = location
        Plants.add(self)
    def update(self):
        if self.health <= 0:
            self.Plants.remove(self)
class Pea(pygame.sprite.Sprite):
    def __init__(self,location,image_file="Pea.png",speed=50,damage=20):
        global Projectiles
        self.image = pygame.image.load(image_file)
        self.rect = self.image.get_rect()
        self.rect.left, self.rect.top = location
        self.speed = speed
        self.damage = damage
        Projectiles.add(self)
    def move(self):
        self.rect = self.rect.move(self.speed)
class Peashooter(Plant):
    def __init__(self,location):
        super(Peashooter,self).__init__(location,"Peashooter.png")
        self.shootloc = (self.location[0]+10,self.location[1])
    def update(self):
        super(Peashooter,self).move()
        self.shoot_pea()
    def shoot_pea(self):
        Pea(self.shootloc)
try:
    clock = pygame.time.Clock()
    Zombie(20)
    Peashooter((10,20))
    Pea((10,20))
    while 1:
        clock.tick(30)
        screen.fill((250,250,250))
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                break
        for zombie in Zombies.sprites():
            zombie.update()
            screen.blit(zombie.image,zombie.rect)
        for plant in Plants.sprites():
            plant.update()
            screen.blit(plant.image,plant.rect)
        for projectile in Projectiles.sprites():
            projectile.update()
            screen.blit(projectile.image,projectile.rect)
        collisions = pygame.sprite.groupcollide(Plants,Zombies,False,False)
        for plant in collisions:
            zombie = collisions[plant]
            zombie.eating = plant
        projectilecol = pygame.sprite.groupcollide(Projectiles,Zombies,False,False)
        for projectile in projectilecol:
            zombie = projectilecol[plant]
            zombie.health -= projectile.damage
        pygame.display.flip()
        print"In loop!"
except:
    traceback.print_exc()
    pygame.time.delay(9999)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.