2017-09-25 45 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