get.py
`import sys
import pygame
from setting import setting
from ship import ship
import game_functions as gf
import pygame
from pygame.sprite import Group
from alien import Alien
def run_game():
pygame.init()
ai_setting=setting()`
`screen=pygame.display.set_mode((ai_setting.screen_width,ai_setting.screen_height))
pygame.display.set_caption("play")
#创建飞船,子弹和外星人
alienss = Group()
#创建一个飞船,一个子弹编组和外星人编组
ships=ship(screen)
bullets=Group()
# aliens=Alien(ai_setting,screen)
gf.create_fleet(ai_setting,ships,screen,alienss)
#开始主循环
while True:
gf.check_event(ai_setting,ships,screen,bullets)
ships.update_ship()
# bullets.update()
gf.update_bullets(bullets)
gf.update_screen(screen,ai_setting,ships,bullets,alienss,bullets)
run_game()`
alien.py
`import pygame
from pygame.sprite import Sprite
class Alien(Sprite):
"""表示单个外星人的类"""
def __init__(self, ai_settings, screen):
"""初始化外星人并设置其起始位置"""
super(Alien, self).__init__()
self.screen = screen
self.ai_settings = ai_settings
# 加载外星人图像,并设置其rect属性
self.image = pygame.image.load('image/ships.jpg')
self.rect = self.image.get_rect()
# 每个外星人最初都在屏幕左上角附近
self.rect.x = self.rect.width
self.rect.y = self.rect.height
# 存储外星人的准确位置
self.x = float(self.rect.x)
def blitme(self):
"""在指定位置绘制外星人"""
self.screen.blit(self.image,self.rect)
`
game_function.py
`import sys
import pygame
from bullet import bullet
from alien import Alien
def key_down(event,ai_settings,screen,ships,bullets):#控制飞船移动
if event.key == pygame.K_a:
ships.move_left = True
elif event.key == pygame.K_d:
ships.move_right = True
elif event.key == pygame.K_SPACE:#创造一颗子弹
first_bullet(ai_settings,bullets,screen,ships)
elif event.key == pygame.K_q:
sys.exit()
def first_bullet(ai_settings,bullets,screen,ships):#创造一个过程,控制子弹熟练(用途:简化)没啥用
if len(bullets)<=ai_settings.Limit_bullets:
new_bullet=bullet(ai_setting=ai_settings,screen=screen,ship=ships)
bullets.add(new_bullet)
# print(len(bullets))
def key_up(event,ships):#判断按键是否抬起,阻止飞船不受控制
if event.key == pygame.K_a:
ships.move_left = False
elif event.key == pygame.K_d:
ships.move_right = False
# ships.update_ship()
def check_event(ai_settings,ships,screen,bullets):#控制鼠标和键盘
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
key_down(event=event,ai_settings=ai_settings,screen=screen,ships=ships,bullets=bullets)
elif event.type == pygame.KEYUP:
key_up(event, ships)
def update_screen(screen,ai_setting,ships,bullets,alien,bulletss):#绘制以及控制子弹
screen.fill(ai_setting.bg_color)
for bullet in bullets.sprites():
bullet.draw()
alien.draw(screen)
ships.show()
# alien.blitme()
pygame.display.flip()
def update_bullets(bullets):
"""更新子弹位置,并在子弹超过屏幕时删除子弹"""
bullets.update()#移动子弹
for bullet in bullets.copy():#删除子弹设置
if bullet.rect.bottom <= 0:
bullets.remove(bullet)
def get_numer_aliens_x(ai_setting,alien_width):
"""计算每行可以容纳多少外星人"""
available_space_x = ai_setting.screen_width - 2 * alien_width
number_aliens_x = int(available_space_x / (2 * alien_width))
return number_aliens_x
def create_alien(ai_setting,screen,aliens,alien_number,row_number):
"""创建一个外星人并将其放在当前行"""
alien = Alien(ai_setting, 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_setting,ship_height,alien_height):
"""计算屏幕中最多容纳多少外星人"""
available_space_y = (ai_setting.screen_height -
(3*alien_height) - ship_height)
number_rows = int(available_space_y / (2*alien_height))
return number_rows
def create_fleet(ai_setting,ship,screen,aliens):
"""创建一群外星人"""
#创建一个外星人,并计算每行可以容纳几个外星人
alien = Alien(ai_setting, screen)
number_aliens_x=get_numer_aliens_x(ai_setting,alien.rect.width)
row_number=get_number_rows(ai_setting,ship.rect.height,alien.rect.height)
#创建第一行外星人
for alien_number in range(row_number):
create_alien(ai_setting,screen,aliens,alien_number,row_number)`
ship.py
`import pygame
from setting import setting
class ship():
"""初始化飞船"""
def __init__(self,screen):
self.screen=screen
self.image=pygame.image.load("image/ship.jpg")
self.ai_setting=setting()
self.rect=self.image.get_rect()
self.screen_rect=self.screen.get_rect()
self.rect.centerx=self.screen_rect.centerx
self.rect.bottom=self.screen_rect.bottom
self.move_right = False
self.move_left = False
self.rects=float(self.rect.centerx)
def update_ship(self):
if self.move_right and self.rect.right < self.screen_rect.right:
if self.move_right:
self.rects+=self.ai_setting.ship_speend
if self.move_left and self.rect.left > 0:
if self.move_left:
self.rects-=self.ai_setting.ship_speend
self.rect.centerx=(self.rects)
def show(self):
self.screen.blit(self.image,self.rect)
`
setting.py
`class setting():
def __init__(self):
"""初始化游戏"""
#设置游戏默认值
self.ship_speend=1.5
self.screen_width=1200
self.screen_height=800
self.bg_color=230,230,230
#设置子弹属性
self.bullet_speed_factor=1
self.bullet_width=3
self.bullet_height=15
self.bullet_color=127,255,0
self.Limit_bullets=3`