1

I get the error message 'Enemy' object has no attribute '_Sprite__g' and I have no idea how to use the sprite groups. I'm trying to get them to spawn randomly across the x-axis only.

import pygame, os, random
pygame.init()
 
FPS=60

SCREEN = pygame.display.set_mode((400,500))
pygame.display.set_caption('caption')

x=50
y=450
vel = 3
width = 20
height = 20

class Player(object):
    def __init__(self,x,y,width,height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.vel = 3
        self.image = pygame.Surface((width, height))
        self.image.fill((0,0,0))
    def draw(self,SCREEN):
        SCREEN.blit(self.image, (self.x,self.y))
class B():
    def __init__(self,x,y,radius, color):
        self.x = x
        self.y = y
        self.color = color
        self.radius = radius
        self.vel = vel
    def draw(self,SCREEN):
        pygame.draw.circle(SCREEN, self.color, (self.x,self.y),self.radius)
    
class Enemy(pygame.sprite.Sprite):
    def __init__(self):
        self.image = pygame.Surface((20,20))
        self.x = random.randrange (0,400)
        self.y = 500
        self.speed = random.randrange(1,3)
    def draw (self,SCREEN):
        SCREEN.blit(self.image,(self.x,self.y))
 
player = Player(x, y, width, height)
bullets = []
enemies = pygame.sprite.Group()
# Main loop
running = True
clock = pygame.time.Clock()
while running:
    clock.tick(FPS)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    for bullet in bullets:
        if bullet.x < 450 and bullet.x >0:
            bullet.x += bullet.vel
        else:
            bullets.pop(bullets.index(bullet))
     
    keys = pygame.key.get_pressed()

    if keys[pygame.K_SPACE]:
        if len(bullets)< 5:
            bullets.append(B(round(player.x + player.width //2), round(player.y + player.height//2),3, (0,0,0)))
            
    if keys[pygame.K_w] and player.y > 30 - player.width - player.vel:
        player.y -= player.vel
    if keys[pygame.K_s] and player.y < 500 - player.width - player.vel:
        player.y += player.vel
 

    SCREEN.fill((190, 232, 220))
    player.draw(SCREEN)
    for bullet in bullets:
            bullet.draw(SCREEN)
    for i in range (8):
        e = Enemy()
        enemies.add(e)
    pygame.display.update()

pygame.quit()

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
user
  • 37
  • 5

2 Answers2

0

Per the documentation:

"When subclassing the Sprite, be sure to call the base initializer before adding the Sprite to Groups."

So the first two lines of Enemy __init__() should be:

def __init__(self):
    pygame.sprite.Sprite.__init__(self)

You also need to set a rect for the enemy:

self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
marienbad
  • 1,461
  • 1
  • 9
  • 19
0

the obects need to be derives from pygame.sprite.Sprite. The base class needs to be initialized as well. Use super() to delegate to the base class constructor. e.g.:

class Player(object):
    def __init__(self,x,y,width,height):
        super().__init__() 
        # [...]

pygame.sprite.Group.draw() and pygame.sprite.Group.update() are methods which are provided by pygame.sprite.Group.

The former delegates the to the update method of the contained pygame.sprite.Sprites - you have to implement the method. See pygame.sprite.Group.update():

Calls the update() method on all Sprites in the Group [...]

The later uses the image and rect attributes of the contained pygame.sprite.Sprites to draw the objects - you have to ensure that the pygame.sprite.Sprites have the required attributes. See pygame.sprite.Group.draw():

Draws the contained Sprites to the Surface argument. This uses the Sprite.image attribute for the source surface, and Sprite.rect. [...]

Therefore the Sprite class must have the attributes .rect and .image. However you don't need the draw method:

class Player(pygame.sprite.Sprite):
    def __init__(self,x,y,width,height):
        super().__init__() 
        self.vel = 3
        self.image = pygame.Surface((width, height))
        self.image.fill((0,0,0))
        self.rect = self.image.get_rect(topleft = (x, y))

Create a transparent pygame.Surface with the SRCALPHA flag and draw a circle on it:

class B(pygame.sprite.Sprite):
    def __init__(self,x,y,radius, color):
        super().__init__() 
        self.x = x
        self.y = y
        self.color = color
        self.radius = radius
        self.vel = vel
        self.image = pygame.Surface((self.radius * 2, self.radius * 2), pygame.SRCALPHA)
        pygame.draw.circle(self.image, self.color, (self.radius, self.radius), self.radius)
        self.rect = self.image.get_rect(center = (self.x, self.y))

Add pygame.sprite.Groups for all sprites. add() the player, the enemies and bullets to the Groups:

enemies = pygame.sprite.Group()
bullets = pygame.sprite.Group()
all_sprites = pygame.sprite.Group()
all_sprites.add(player)

for i in range (8):
    e = Enemy()
    enemies.add(e)
    all_sprites.add(e)

Draw the Sprites in the all_sprites Group with pygame.sprite.Group.draw. Remove a Sprite (form all Groups) with kill:

while running:
    # [...]

    for bullet in bullets:
        if 0 < bullet.rect.y < 500:
            bullet.rect.y -= bullet.vel
        else:
            bullet.kill()

    # [...]

    all_sprites.draw(SCREEN)

Use the keyboard event instead of pygame.key.get_pressed() to fire a bullet. See How do I stop more than 1 bullet firing at once?:

The collision of the enemies and bullets can be detected with pygame.sprite.groupcollide(). When the doKill arguments are set True, the sprites will be automatically removed:

pygame.sprite.groupcollide(bullets, enemies, True, True)

Complete example:

import pygame, os, random
pygame.init()
 
FPS=60

SCREEN = pygame.display.set_mode((400,500))
pygame.display.set_caption('caption')

x=50
y=450
vel = 3
width = 20
height = 20

class Player(pygame.sprite.Sprite):
    def __init__(self,x,y,width,height):
        super().__init__() 
        self.vel = 3
        self.image = pygame.Surface((width, height))
        self.image.fill((0,0,0))
        self.rect = self.image.get_rect(topleft = (x, y))
   
class B(pygame.sprite.Sprite):
    def __init__(self,x,y,radius, color):
        super().__init__() 
        self.x = x
        self.y = y
        self.color = color
        self.radius = radius
        self.vel = vel
        self.image = pygame.Surface((self.radius * 2, self.radius * 2), pygame.SRCALPHA)
        pygame.draw.circle(self.image, self.color, (self.radius, self.radius), self.radius)
        self.rect = self.image.get_rect(center = (self.x, self.y))
       
class Enemy(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__() 
        self.image = pygame.Surface((20,20))
        self.image.fill((255, 0, 0))
        y = random.randrange (0, 480)
        x = 400
        self.rect = self.image.get_rect(topleft = (x, y))
        self.speed = random.randrange(1,3)
 
player = Player(x, y, width, height)

enemies = pygame.sprite.Group()
bullets = pygame.sprite.Group()
all_sprites = pygame.sprite.Group()
all_sprites.add(player)

# Main loop
running = True
clock = pygame.time.Clock()
while running:
    clock.tick(FPS)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE and len(bullets) < 5:
                bullet = B(player.rect.centerx, player.rect.centery, 3, (0,0,0))
                bullets.add(bullet)
                all_sprites.add(bullet)

    if len(enemies) < 8:
        e = Enemy()
        enemies.add(e)
        all_sprites.add(e)

    for bullet in bullets:
        if bullet.rect.right < 500:
            bullet.rect.x += bullet.vel
        else:
            bullet.kill()
    for enemy in enemies:
        if enemy.rect.right > 0:
            enemy.rect.x -= enemy.speed
        else:
            enemy.kill()

    pygame.sprite.groupcollide(bullets, enemies, True, True)
            
    keys = pygame.key.get_pressed()            
    if keys[pygame.K_w] and player.rect.top > player.vel:
        player.rect.y -= player.vel
    if keys[pygame.K_s] and player.rect.bottom < 500 - player.vel:
        player.rect.y += player.vel
    
    SCREEN.fill((190, 232, 220))
    all_sprites.draw(SCREEN)
    pygame.display.update()

pygame.quit()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174