2017-09-25 295 views
1

這是我第一次提出問題。我在python中製作了一個名爲「Alien Invasion」的遊戲。當我想點擊「Play」時,但這並沒有幹活不斷收到此錯誤:有關pygame的一些信息:'Group'對象沒有'rect'屬性

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' 

這是我對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() 

我想知道這是爲什麼發生的事情,以及如何糾正it.Thanks你的幫助。

回答

1

您按錯誤順序傳遞參數。他們需要以相同的順序在函數的定義:因爲你傳遞aliens,這顯然是一個精靈組,作爲第四個參數發生

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

錯誤。然後在check_play_button函數中,這個aliens組被分配到本地變量play_button,由於它沒有rect屬性,Python引發了AttributeError

+0

謝謝你這麼糊塗! – Christine