143 lines
4.5 KiB
Python
143 lines
4.5 KiB
Python
# 《外星人入侵》运行的所有函数
|
||
|
||
import sys
|
||
import pygame
|
||
|
||
from bullet import Bullet
|
||
from alien import Alien
|
||
from time import sleep
|
||
import json
|
||
import music
|
||
|
||
|
||
def play_bg_music():
|
||
"""背景音乐"""
|
||
if not pygame.mixer.music.get_busy():
|
||
pygame.mixer.music.play()
|
||
|
||
|
||
def check_keydown_events(event, ai_settings, screen, ship, bullets, game_stats, scoreb, aliens):
|
||
""""
|
||
响应鼠标按下操作
|
||
"""
|
||
# 按右箭头飞船向右移动
|
||
if event.key == pygame.K_RIGHT:
|
||
ship.moving_right = True
|
||
# 按左箭头飞船向左移动
|
||
elif event.key == pygame.K_LEFT:
|
||
ship.moving_left = True
|
||
|
||
# 按空格键创建一颗子弹并将其加入到编组bullets中
|
||
elif event.key == pygame.K_SPACE:
|
||
fire_bullet(ai_settings, screen, ship, bullets)
|
||
|
||
# 按p开始游戏
|
||
elif event.key == pygame.K_p:
|
||
start_game(ai_settings, screen, ship, bullets, game_stats, scoreb,
|
||
aliens)
|
||
|
||
# 按z暂停游戏3s
|
||
elif event.key == pygame.K_z:
|
||
sleep(2)
|
||
|
||
# 按q退出游戏并把最高分写入文件
|
||
elif event.key == pygame.K_q:
|
||
with open('Max_score.json', 'w', encoding='UTF-8') as file:
|
||
json.dump(game_stats.high_score, file)
|
||
sys.exit()
|
||
|
||
|
||
def fire_bullet(ai_settings, screen, ship, bullets):
|
||
"""
|
||
按照要求发射子弹数量
|
||
"""
|
||
if len(bullets) < ai_settings.bullet_allowed:
|
||
music.bullet_biu() # 发射子弹的声音
|
||
new_bullet = Bullet(ai_settings, screen, ship) # 如果还没有到达限制,就发射一颗子弹
|
||
bullets.add(new_bullet) # 创建一颗子弹,并将其加入到编组bullets中
|
||
|
||
|
||
def start_game(ai_settings, screen, ship, bullets, game_stats, scoreb,
|
||
aliens):
|
||
"""
|
||
P264中动手试一试14-1的练习:让玩家按p开始游戏
|
||
"""
|
||
# 重置游戏统计信息
|
||
game_stats.reset_stats()
|
||
game_stats.game_active = True
|
||
|
||
# 重置记分牌图像
|
||
scoreb.prep_score()
|
||
scoreb.prep_high_score()
|
||
scoreb.prep_level()
|
||
scoreb.prep_ships()
|
||
|
||
# 清空外星人列表和子弹列表
|
||
aliens.empty()
|
||
bullets.empty()
|
||
|
||
# 创建一群新的外星人,并让飞船居中
|
||
create_fleet(ai_settings, screen, ship, aliens)
|
||
ship.center_ship()
|
||
|
||
# 暂停让用户反应一会
|
||
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)
|
||
|
||
|