Python introduction to the practical aircraft game, everything is normal before adding bullets to collide with aliens, but the error is added after the collision.

topic description

without the pygame.sprite.groupcollide () code, everything works fine
when the pygame.sprite.groupcollide () code is added, an error occurs when you press the space. Run normally without pressing spaces

Traceback (most recent call last):
  File "alien_invasion.py", line 41, in <module>
    run_game()
  File "alien_invasion.py", line 33, in run_game
    gf.update_bullet(bullets,aliens)
  File "G:\python_work\alien_invasion\game_functions.py", line 115, in update_bullet
    cllisions = pygame.sprite.groupcollide(bullets,aliens,True,True)
  File "C:\Users\\AppData\Roaming\Python\Python36\site-packages\pygame\sprite.py", line 1550, in groupcollide
    c = SC(s, groupb, dokillb, collided)
  File "C:\Users\\AppData\Roaming\Python\Python36\site-packages\pygame\sprite.py", line 1513, in spritecollide
    spritecollide = sprite.rect.colliderect
AttributeError: "Bullet" object has no attribute "rect"

sources of topics and their own ideas

when the topic is an introduction to python programming to practical examples, there is no difference between typing by yourself and that in a book

related codes

/ / Please paste the code text below (do not replace the code with pictures)

-sharp alien.py
import pygame
from pygame.sprite import Sprite

class Alien(Sprite):
    
    def __init__(self,screen,ai_settings):
        super().__init__()
        -sharp
        self.screen = screen
        self.ai_settings = ai_settings
        -sharp
        self.image = pygame.image.load("images/alien_aircraft.bmp")
        self.alien_rect = self.image.get_rect()
        -sharp
        self.alien_rect.x = self.alien_rect.width
        self.alien_rect.y = self.alien_rect.height
    
    def blitme(self):
        """"""
        self.screen.blit(self.image,self.alien_rect)
    
    def update(self):
        """"""
        self.speed = self.ai_settings.alien_speed_factor
        self.x += self.speed * self.ai_settings.speed_argue
        self.alien_rect.x = self.x
    
    def check_alien_edges(self):
        """"""
        screen_rect = self.screen.get_rect()
        if self.alien_rect.right >= screen_rect.right:
            return True
        elif self.alien_rect.left <= screen_rect.left:
            return True
        

what result do you expect? What is the error message actually seen?

Mar.28,2021

Hello, I have the same problem, how to solve it?

Menu