存储项目的大部分函数
This commit is contained in:
parent
93a9099e5e
commit
e771eca2ba
|
@ -3,6 +3,8 @@ 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:
|
||||
|
@ -17,6 +19,8 @@ def check_keydown_events(event, ai_settings, screen, ship, bullets):
|
|||
fire_bullet(ai_settings, screen, ship, bullets)
|
||||
elif event.key == pygame.K_q:
|
||||
sys.exit()
|
||||
|
||||
|
||||
def fire_bullet(ai_settings, screen, ship, bullets):
|
||||
"""如果还没有到达限制,就发射一颗子弹"""
|
||||
# 创建一颗子弹,并将其加入到编组bullets中
|
||||
|
@ -27,15 +31,21 @@ def fire_bullet(ai_settings, screen, ship, bullets):
|
|||
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:
|
||||
|
@ -46,6 +56,8 @@ def check_keyup_events(event, ship):
|
|||
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():
|
||||
|
@ -59,6 +71,8 @@ def check_events(ai_settings, screen, stats, sb, play_button, ship, aliens, bull
|
|||
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()
|
||||
|
@ -88,6 +102,8 @@ def update_screen(ai_settings, screen, stats, sb, ship, aliens, bossalien, bulle
|
|||
|
||||
# 让最近绘制的屏幕可见
|
||||
pygame.display.flip()
|
||||
|
||||
|
||||
def update_bullets(ai_settings, screen, stats, sb, ship, aliens, bullets, bossalien, bossbullet):
|
||||
"""更新子弹的位置,并删除已消失的子弹"""
|
||||
# 更新子弹的位置
|
||||
|
@ -102,6 +118,8 @@ def update_bullets(ai_settings, screen, stats, sb, ship, aliens, bullets, bossal
|
|||
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关才启用
|
||||
|
@ -159,6 +177,8 @@ def check_bullet_collisions(ai_settings, screen, stats, sb, ship, aliens, bossal
|
|||
|
||||
if pygame.Rect.colliderect(bossbullet.rect, ship.rect):
|
||||
ship_hit(ai_settings, screen, stats, sb, ship, aliens, bullets, bossalien, bossbullet)
|
||||
|
||||
|
||||
def create_fleet(ai_settings, screen, ship, aliens):
|
||||
"""创建外星人群"""
|
||||
# 创建一个外星人,并计算一行可容纳多少个外星人
|
||||
|
@ -176,4 +196,21 @@ def get_number_aliens_x(ai_settings, alien_width):
|
|||
"""计算每行可容纳多少个外星人"""
|
||||
available_space_x = ai_settings.screen_width - 2 * alien_width
|
||||
number_aliens_x = int(available_space_x / (2 * alien_width))
|
||||
return number_aliens_x
|
||||
return number_aliens_x
|
||||
|
||||
|
||||
def create_alien(ai_settings, screen, aliens, alien_number, row_number):
|
||||
"""创建一个外星人并将其放在当前行"""
|
||||
alien = Alien(ai_settings, screen)
|
||||
alien_width = alien.rect.width
|
||||
alien.x = alien_width + 2 * alien_width * alien_number
|
||||
alien.rect.x = alien.x
|
||||
alien.rect.y = alien.rect.height + 2 * alien.rect.height * row_number
|
||||
aliens.add(alien)
|
||||
|
||||
|
||||
def get_number_rows(ai_settings, ship_height, alien_height):
|
||||
"""计算屏幕可容纳多少行外星人"""
|
||||
available_space_y = (ai_settings.screen_height - (3 * alien_height) - ship_height)
|
||||
number_rows = int(available_space_y / (2 * alien_height))
|
||||
return number_rows
|
||||
|
|
Loading…
Reference in New Issue