206530219/game_functions.py

137 lines
5.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import sys
import pygame
from bullet import *
from alien import *
from time import sleep
def check_keydown_events(event, ai_settings, screen, ship, bullets):
"""响应按键"""
if event.key == pygame.K_RIGHT:
ship.moving_right = True
elif event.key == pygame.K_LEFT:
ship.moving_left = True
elif event.key == pygame.K_UP:
ship.moving_up = True
elif event.key == pygame.K_DOWN:
ship.moving_down = True
elif event.key == pygame.K_SPACE:
fire_bullet(ai_settings, screen, ship, bullets)
elif event.key == pygame.K_q:
sys.exit()
def fire_bullet(ai_settings, screen, ship, bullets):
"""如果还没有到达限制,就发射一颗子弹"""
# 创建一颗子弹并将其加入到编组bullets中
if len(bullets) < ai_settings.bullets_allowed:
new_bullet = Bullet(ai_settings, screen, ship)
bullets.add(new_bullet)
# 增加:添加发射音效
pygame.mixer.init()
sound = pygame.mixer.Sound('sounds/firebullets.wav')
sound.play()
def bossalien_fire_bullet(ai_settings, screen, bossalien, bossbullet, ship):
"""增加BOSS发射子弹"""
# 创建一颗子弹
bossbullet = BossBullet(ai_settings, screen, bossalien)
def check_bossbullet_bottom(ai_settings, screen, stats, sb, ship, aliens, bossalien, bullets, bossbullet):
"""当子弹飞出屏幕底部时重置BOSS子弹位置"""
screen_rect = screen.get_rect()
if bossbullet.y >= screen_rect.bottom:
bossbullet.reset_position(bossalien)
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
elif event.key == pygame.K_UP:
ship.moving_up = False
elif event.key == pygame.K_DOWN:
ship.moving_down = False
def check_events(ai_settings, screen, stats, sb, 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:
mouse_x, mouse_y = pygame.mouse.get_pos()
check_play_button(ai_settings, screen, stats, sb, play_button, ship, aliens, bullets, mouse_x, mouse_y)
def update_screen(ai_settings, screen, stats, sb, ship, aliens, bossalien, bullets, bossbullet, play_button):
screen.fill(ai_settings.bg_color)
ship.blitme()
# 在飞船和外星人后面重绘所有子弹
for bullet in bullets.sprites():
bullet.draw_bullet()
# 增加如果进入BOSS关则绘制BOSS外星人其他关卡绘制普通外星人
if stats.level == stats.bosslevel:
bossalien.blitme()
bossalien.draw_health_bar(screen)
bossbullet.draw_bullet()
bossalien_fire_bullet(ai_settings, screen, bossalien, bossbullet, ship)
check_bossbullet_bottom(ai_settings, screen, stats, sb, ship, aliens, bossalien, bullets, bossbullet)
else:
aliens.draw(screen)
# 显示得分
sb.show_score()
# 如果游戏处于非活动状态就绘制Play按钮
if not stats.game_active:
play_button.draw_button()
# 让最近绘制的屏幕可见
pygame.display.flip()
def update_bullets(ai_settings, screen, stats, sb, ship, aliens, bullets, bossalien, bossbullet):
"""更新子弹的位置,并删除已消失的子弹"""
# 更新子弹的位置
bullets.update()
if stats.level == stats.bosslevel:
bossbullet.update()
# 删除已消失的子弹
for bullet in bullets.copy():
if bullet.rect.bottom <= 0:
bullets.remove(bullet)
check_bullet_collisions(ai_settings, screen, stats, sb, ship, aliens, bossalien, bullets, bossbullet)
def check_bullet_collisions(ai_settings, screen, stats, sb, ship, aliens, bossalien, bullets, bossbullet):
"""响应子弹和外星人的碰撞,以及外星人子弹和船的碰撞"""
# 增加只有在非BOSS关才启用
if stats.level != stats.bosslevel:
# 删除发生碰撞的子弹和外星人
collisions = pygame.sprite.groupcollide(bullets, aliens, True, True)
if collisions:
for aliens in collisions.values():
stats.score += ai_settings.alien_points * len(aliens)
sb.prep_score()
check_high_score(stats, sb)
if len(aliens) == 0:
# 如果整群外星人都被消灭,就提高一个等级
bullets.empty()
ai_settings.increase_speed()
# 增加:播放音效
pygame.mixer.init()
sound = pygame.mixer.Sound('sounds/levelup.wav')
sound.play()
# 提高等级
stats.level += 1
sb.prep_level()
# 如果当前关卡下一关是BOSS关就不要再生成舰队
if stats.level != stats.bosslevel - 1:
create_fleet(ai_settings, screen, ship, aliens)
else:
pass