功能函数更改
This commit is contained in:
parent
a6dae52a9a
commit
be8d7bfa2e
|
@ -84,3 +84,101 @@ def start_game(ai_settings, screen, ship, bullets, game_stats, scoreb,
|
||||||
sleep(0.5)
|
sleep(0.5)
|
||||||
|
|
||||||
|
|
||||||
|
def check_keyup_events(event, ship):
|
||||||
|
""""
|
||||||
|
响应鼠标松开---飞船停下
|
||||||
|
"""
|
||||||
|
if event.key == pygame.K_RIGHT:
|
||||||
|
ship.moving_right = False
|
||||||
|
|
||||||
|
elif event.key == pygame.K_LEFT:
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
||||||
|
def update_screen(ai_settings, screen, game_stats, scoreb, ship, aliens, bullets, play_button):
|
||||||
|
"""
|
||||||
|
更新屏幕上的图像并切换到新屏幕
|
||||||
|
"""
|
||||||
|
# 每次循环时都重绘屏幕
|
||||||
|
screen.fill(ai_settings.bg_color) # 先填充背景后绘制飞船
|
||||||
|
ship.blitem()
|
||||||
|
aliens.draw(screen)
|
||||||
|
|
||||||
|
# 在飞船后重绘所有子弹
|
||||||
|
for bullet in bullets.sprites():
|
||||||
|
bullet.draw_bullet()
|
||||||
|
# 显示得分
|
||||||
|
scoreb.show_score()
|
||||||
|
|
||||||
|
# 如果游戏处于非活动状态就绘制Play按钮
|
||||||
|
if not game_stats.game_active:
|
||||||
|
play_button.draw_button()
|
||||||
|
|
||||||
|
# 让最近绘制的屏幕可见
|
||||||
|
pygame.display.flip()
|
||||||
|
|
||||||
|
|
||||||
|
def start_new_level(ai_settings, screen, game_stats, scoreb, ship, aliens, bullets):
|
||||||
|
"""
|
||||||
|
当消灭干净外星人提高一个等级
|
||||||
|
"""
|
||||||
|
if len(aliens) == 0:
|
||||||
|
# 删除现有子弹
|
||||||
|
bullets.empty()
|
||||||
|
|
||||||
|
# 逐渐提高速度加快游戏节奏
|
||||||
|
ai_settings.increase_speed()
|
||||||
|
|
||||||
|
# 当前屏幕外星人全部被消灭就提高一个等级
|
||||||
|
game_stats.level += 1
|
||||||
|
scoreb.prep_level()
|
||||||
|
|
||||||
|
# 创建一群新的外星人
|
||||||
|
create_fleet(ai_settings, screen, ship, aliens) # 调用创建外星人群的函数
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue