From d203b8b5e322f08b97ad2dc08a9f540aa750a536 Mon Sep 17 00:00:00 2001 From: "11589262996@qq.com" <2042271667> Date: Tue, 15 Jun 2021 19:47:37 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=BB=E9=9D=A2=E9=87=8D=E7=BB=98=E5=8F=8A?= =?UTF-8?q?=E9=83=A8=E5=88=86=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- game_functions.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/game_functions.py b/game_functions.py index 05f8975..3287653 100644 --- a/game_functions.py +++ b/game_functions.py @@ -95,3 +95,48 @@ def check_keyup_events(event, ship): ship.moving_left = False +def check_play_button(ai_settings, screen, game_stats, scoreb, play_button, ship, + aliens, bullets, mouse_x, mouse_y): + """ + 在玩家单击Play按钮时开始游戏 + """ + button_clicked = play_button.button_rect.collidepoint(mouse_x, mouse_y) + # 单击Play按钮且游戏处于非活跃状态时----避免了游戏处于活跃状态下单击到了Play按钮区域重启游戏 + if button_clicked and not game_stats.game_active: + """如果鼠标单击位置在msg_image_rect范围内则将游戏置于活跃状态""" + + # # 重置游戏设置 + ai_settings.initialize_dynamic_settings() + + # 游戏开始后Play按钮隐藏起来 + pygame.mouse.set_visible(False) + + start_game(ai_settings, screen, ship, bullets, game_stats, scoreb, aliens) + + +def check_events(ai_settings, screen, game_stats, scoreb, play_button, ship, aliens, bullets): + """ + 响应鼠标和键盘事件 + """ + # 游戏退出并把最高分写入文件 + for event in pygame.event.get(): + if event.type == pygame.QUIT: + with open('Max_score.json', 'w', encoding='UTF-8') as file: + json.dump(game_stats.high_score, file) + sys.exit() + + # 响应Play按钮操作 + elif event.type == pygame.MOUSEBUTTONDOWN: + mouse_x, mouse_y = pygame.mouse.get_pos() # 该函数返回一个元组--获取鼠标单击Play按钮时的位置 + check_play_button(ai_settings, screen, game_stats, scoreb, play_button, ship, + aliens, bullets, mouse_x, mouse_y) + + # 判读飞船向左还是向右移动 + elif event.type == pygame.KEYDOWN: + check_keydown_events(event, ai_settings, screen, ship, bullets, game_stats, scoreb, aliens) + + # 飞船停下 + elif event.type == pygame.KEYUP: + check_keyup_events(event, ship) + +