Question

After a few hours of looking for code to reference. I failed i tried to re-write the collision code but it failed. Now my Question is

How can i fix my code to make it delete the Enemy ship when the bullet hits him

Note: Im not asking for you to do it, i just need it further explained what the problem or where im making a mistake

The Code Segment

    for bullet in Bullets:
    Hit_list = pygame.sprite.Group()
    if pygame.sprite.collide_rect(bullet, Enemy_list):
        Hit_list.add(hit)
        for hit in Hit_list:
            Bullets.remove(bullet)
            All.remove(bullet)
            All.remove(hit)
            Enemy_list.remove(hit)
            # If the bullet goes off the screen it deletes
        if bullet.rect.y < 0:
            Bullets.remove(bullet)
            All.remove(bullet)

The Error

Traceback (most recent call last):
  File "C:\Users\jb127996\Desktop\Invaders in Space\Space Shooter.py", line 92, in     <module>
    if pygame.sprite.collide_rect(bullet, Enemy_list):
  File "C:\Python32\lib\site-packages\pygame\sprite.py", line 1290, in collide_rect
    return left.rect.colliderect(right.rect)
AttributeError: 'Group' object has no attribute 'rect'

Entire Code

import pygame
import Player_func
import AI
from pygame.locals import *


#Background 
Background = pygame.image.load('Tempback.png')
Background_size = Background.get_size()
Background_rect = Background.get_rect()

# Screens Resoultion
Width = 640
Height = 480
Scr = pygame.display.set_mode((Width,Height))

# Fps Clock Vars( Clock.tick(FPS) )
Clock = pygame.time.Clock()
FPS = 60

# Colors
White = (255, 255, 255)
Black = (0, 0, 0)
Green = (0, 255, 0)
Red = (255, 0, 0)
Blue = (0, 0, 255)
Orange = (255, 140, 0)
Default = White


# Sprite Lists
Bullets = pygame.sprite.Group()
Enemy_list = pygame.sprite.Group()
All = pygame.sprite.Group()

# Shorting the Class files 
AI = AI.Enemy()
Ply = Player_func.Player()


# CORDS for Player
AI.rect.x, AI.rect.y = 100, 50
AI_pos = (AI.rect.x, AI.rect.y)

Ply.rect.x, Ply.rect.y = 580, 425
P_pos = (Ply.rect.x, Ply.rect.y)

# True false statements and Vars
running = True
Gun_Overload = 0
Fire = 20

while running:
    Enemy_list.add(AI)
    All.add(AI)
    All.add(Ply)

    # Collision Detection for walls on the spaceship
    if Ply.rect.x <= 0:
        Ply.rect.x += 5.5
    elif Ply.rect.x >= 590:
        Ply.rect.x -= 5.5

    # Events
    for event in pygame.event.get():
        if event.type == QUIT:
            running = False

        elif event.type == KEYDOWN:
            if event.key == K_SPACE and Gun_Overload == Fire:
                bullet = Player_func.Bullet()
                bullet.rect.x = (Ply.rect.x +15)
                bullet.rect.y = Ply.rect.y
                Bullets.add(bullet)
                All.add(bullet)
                Gun_Overload = 0


    #Cool Down
    if Gun_Overload < Fire:
        Gun_Overload += 1

    AI.movement()
    Ply.Controls()

    # Updates the Sprite lists so they can all be drawn
    All.update()

    # Checks if a bullet hits the enemy on the list
    for bullet in Bullets:
        Hit_list = pygame.sprite.Group()
        if pygame.sprite.collide_rect(bullet, Enemy_list):
            Hit_list.add(hit)
            for hit in Hit_list:
                Bullets.remove(bullet)
                All.remove(bullet)
                All.remove(hit)
                Enemy_list.remove(hit)
                # If the bullet goes off the screen it deletes
            if bullet.rect.y < 0:
                Bullets.remove(bullet)
                All.remove(bullet)


    Scr.blit(Background, Background_rect)
    All.draw(Scr)
    pygame.display.flip()

    # FPS Clock Loaded in
    Clock.tick(FPS)

pygame.quit()
Was it helpful?

Solution

Your If condition uses collide_rect:

pygame.sprite.collide_rect()
Collision detection between two sprites, using rects.
collide_rect(left, right) -> bool

You pass 2 parameters - a Bullet, and a SpriteGroup. The error is telling you that a SpriteGroup does not have a Rect attribute.

I think you wanted to use:

pygame.sprite.spritecollide()
Find sprites in a group that intersect another sprite.
spritecollide(sprite, group, dokill, collided = None) -> Sprite_list

Apart from this, I would change all the variable names to lowercase. I have also noticed that your if bullet.rect.y < 0: is under the collide test. I think you want to remove a Bullet from the screen even if there is no collision.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top