满意答案
輕霧丶澤
2017.09.13
采纳率:53% 等级:9
已帮助:818人
我的python在运行到events时,直接弹出:mouse_x is not defind 。哆啦A梦疑惑。
python3: 老是defind,我也很惆怅啊!
——————代码贴下面——————
def check_events(ai_settings, screen, stats, play_button, 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:
check_play_button(ai_settings, screen, stats, play_button, ship,
aliens, bullets, mouse_x, mouse_y)
def check_play_button(ai_settings, screen, stats, play_button, ship, aliens,
bullets, mouse_x, mouse_y):
"""在玩家单击Play按钮时开始新游戏"""
button_clicked = play_button.rect.collidepoint(mouse_x, mouse_y)
if button_clicked and not stats.game_active:
stats.reset_stats()
stats.game_active = True
你的 mouse_x和mouse_y都只是 check_play_button() 这个函数的形参,check_events()是无法直接访问的,因此提示:变量未定义。因为python支持函数直接访问外部的变量,因此建议你在函数之前加上
mouse_x=pygame.mouse.get_pos()[0]
mouse_y=pygame.mouse.get_pos()[1]
这两行。这样 check_play_button() 函数可以直接访问mouse_x和mouse_y,而check_events() 函数正好通过参数传递传进去。
00分享举报