游戏相应按键

This commit is contained in:
18530439 2021-06-15 19:30:11 +08:00
parent 58fe0ed266
commit e09aab0640
1 changed files with 49 additions and 0 deletions

49
game_functions.py Normal file
View File

@ -0,0 +1,49 @@
# 《外星人入侵》运行的所有函数
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()