Alien_Invasion.py
import pygame
from settings import Settings
from airplane import Airplane
import game_functions as gf
from pygame.sprite import Group
def run_game():
pygame.init()
ai_settings = Settings()
screen = pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height))
pygame.display.set_caption("Alien Invasion")
airplane = Airplane(ai_settings,screen)
bullets = pygame.sprite.Group()
while True:
gf.check_events(ai_settings,screen,airplane,bullets)
airplane.update()
#bullets.update()
gf.update_bullets(bullets)
gf.update_screen(ai_settings,screen,airplane,bullets)
#让最近绘制的屏幕可见
pygame.display.flip()
run_game()
-------------------------------------------------------------------------------------------------------------------------------
airplane.py
import pygame
class Airplane():
def __init__(self,ai_settings,screen):
"""初始化飞机并设置其初始化位置"""
self.screen = screen
self.ai_settings = ai_settings
self.image = pygame.image.load('image/airplane.bmp')
self.rect = self.image.get_rect()
self.screen_rect = screen.get_rect()
self.rect.centerx = self.screen_rect.centerx
self.rect.bottom = self.screen_rect.bottom
# 在飞船的属性center中存储小数值
self.center = float(self.rect.centerx)
# 移动标志
self.moving_right = False
self.moving_left = False
self.ai_settings.airplane_speed_factor = 1.5
def update(self):
"""根据移动标志调整飞船的位置"""
#更新飞船的center值,而不是rect
if self.moving_right and self.rect.right < self.screen_rect.right:
self.center += self.ai_settings.airplane_speed_factor
if self.moving_left and self.rect.left>0:
self.center -= self.ai_settings.airplane_speed_factor
self.rect.centerx = self.center
def blitme(self):
"""指定位置绘制飞船"""
self.screen.blit(self.image,self.rect)
______________________________________________________________________________
game_functions.py
import sys
import pygame
from settings import Settings
from airplane import Airplane
from bullet import Bullet
def check_keydown_events(event,ai_settings,screen,airplane,bullets):
"""响应按键"""
if event.key == pygame.K_RIGHT:
airplane.moving_right = True
# airplane.update()
elif event.key == pygame.K_LEFT:
airplane.moving_left = True
# airplane.update()
elif event.key == pygame.K_SPACE:
fire_bullet(ai_settings,screen,airplane,bullets)
def check_keyup_events(event,airplane):
"""响应松开"""
if event.key == pygame.K_RIGHT:
airplane.moving_right = False
elif event.key == pygame.K_LEFT:
airplane.moving_left = False
# 开火
def fire_bullet(ai_settings,screen,airplane,bullets):
if len(bullets) < ai_settings.bullets_allowed:
new_bullet = Bullet(ai_settings,screen,airplane)
bullets.add(new_bullet)
def check_events(ai_settings,screen,airplane,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,airplane,bullets)
elif event.type == pygame.KEYUP:
check_keyup_events(event,airplane)
def update_screen(ai_settings,screen,airplane,bullets):
screen.fill(ai_settings.bg_color)
for bullet in bullets.sprites():
bullet.draw_bullet()
airplane.blitme()
def update_bullets(bullets):
"""更新子弹的位置,并删除已消失的子弹"""
#更新子弹的位置
bullets.update()
# 删除已消失的子弹
for bullet in bullets.copy():
if bullet.rect.bottom <= 0:
bullets.remove(bullet)
_____________________________________________________________________________
settings.py
class Settings():
"""存储《外星人入侵》的所有设置的类"""
def __init__(self):
"""初始化游戏的设置"""
# 屏幕设置
self.screen_width = 1200
self.screen_height = 800
self.bg_color = (255,255,255)
#子弹设置
self.bullet_speed_factor = 1
self.bullet_width = 3
self.bullet_height = 15
self.bullet_color =60,60,60
self.bullets_allowed = 3
---------------------------------------------------------------------------------------------------------------------------------
bullet.py
import pygame
from pygame.sprite import Sprite
class Bullet(Sprite):
"""一个对飞船发射的子弹进行管理的类"""
def __init__(self,ai_settings,screen,airplane):
"""在飞船所处的位置创建一个子弹对象"""
super(Bullet,self).__init__()
self.screen = screen
self.ai_settings = ai_settings
#在(0,0)处创建一个表示子弹的矩形,再设置正确的位置
self.rect = pygame.Rect(0,0,ai_settings.bullet_width,ai_settings.bullet_height)
self.rect.centerx = airplane.rect.centerx
self.rect.top = airplane.rect.top
#存储用小数表示的子弹位置
self.y = float(self.rect.y)
self.color = ai_settings.bullet_color
self.speed_factor = ai_settings.bullet_speed_factor
def update(self):
"""向上移动子弹"""
#更新表示子弹位置的小数值
self.y -= self.speed_factor
#更新表示子弹的rect的位置
self.rect.y = self.y
def draw_bullet(self):
"""在屏幕上绘制子弹"""
pygame.draw.rect(self.screen,self.color,self.rect)