Christine
Christine

Reputation: 13

something about pygame:'Group' object has no attribute to 'rect'

It's my first time to ask questions.I'm making a game called "Alien Invasion" in python.When I want to click the "Play",it does't work.I keep on getting this error:

Traceback (most recent call last):
  File "alien_invasion.py", line 30, in <module>
  File "alien_invasion.py", line 24, in run_game
  Flie "D:\python_work\alien_invasion\game_function.py", line 38, in check_events
  Flie "D:\python_work\alien_invasion\game_function.py", line 41, in check_play_button
AttributeError:'Group' object has no attribute 'rect'

This is my code about game_function:

def check_events(ai_settings,stats,play_button,screen,ship,aliens,bullets):
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            sys.exit()
        elif event.type==pygame.KEYDOWN:
            check_keydown_events(event,ai_settings,screen,ship,bullets)         
        elif event.type==pygame.KEYUP:
            check_keyup_events(event,ship)
        elif event.type==pygame.MOUSEBUTTONDOWN:
            mouse_x,mouse_y=pygame.mouse.get_pos()
            check_play_button(ai_settings,screen,ship,aliens,bullets,stats,play_button,mouse_x,mouse_y)

def check_play_button(ai_settings,screen,stats,play_button,ship,aliens,bullets,mouse_x,mouse_y):
    button_clicked=play_button.rect.collidepoint(mouse_x,mouse_y)
    if button_clicked and not stats.game_active:
        pygame.mouse.set_visible(False)
        stats.reset_stats()
        stats.game_active=True
        aliens.empty()
        bullets.empty()
        create_fleet(ai_settings,screen,ship,aliens)
        ship.center_ship()

I want to know why this is happening and how to correct it.Thanks for your help.

Upvotes: 1

Views: 759

Answers (1)

skrx
skrx

Reputation: 20438

You are passing the arguments in the wrong order. They need to be in the same order as in the function definition:

check_play_button(ai_settings,screen,stats,play_button,ship,aliens,bullets,mouse_x,mouse_y)

The error occurs because you pass aliens, which is obviously a sprite group, as the fourth argument. Then in the check_play_button function this aliens group gets assigned to the local variable play_button and since it has no rect attribute, Python raises the AttributeError.

Upvotes: 1

Related Questions