pygame 外星人入侵 (实现开火)
主框架
"""主文件Alien_invasion创建了一系列游戏都要用到的对象
存储在ai_settings中的设置,存储在screen中的主显示surface以及飞船实例
文件alien_invasion.py还包含游戏的主循环,这是一个调用许多模块的循环"""
import pygame
import sys
from setting import Settings
from ship import Ship
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")
ship = Ship(ai_settings,screen)
bullets = Group()
while True:
gf.check_events(ai_settings,screen,ship,bullets)
ship.update()
gf.update_bullets(bullets)
gf.update_screen(ai_settings,screen,ship,bullets)
run_game()
设置
"""setting包含Setting类,这个类只包含方法_init_,它初始化控制游戏外观,飞船速度,子弹的属性"""
import pygame
from ship import Ship
class Settings():
'''存储《外星人入侵》的所有设置的类'''
def __init__(self):
'''初始化游戏的设置'''
self.screen_width = 1200
self.screen_height = 800
self.bg_color = (200, 100, 225)
self.ship_speed_factor = 1.5
self.bullet_speed_factor = 1
self.bullet_width = 3
self.bullet_height = 15
self.bullet_color = 60, 60, 0
self.bullet_allowed = 3
辅助框架
"""game_functions.py包含一系列函数,游戏的大部分工作都是由他们完成的,函数check_events()检测相关的事件,如按键和松开,并使用辅助函数check_keydown_events()和check_keyup_events()来处理这些事件。
update_screen(),它用于每次执行主循环时都重绘屏幕并将子弹的图像重新绘制。
update_bullets(),更新子弹的位置,并判断删除已消失的子弹
fire_bullet(),对子弹数量限制,并且判断如果没有达到限制就继续发射
"""
import sys
import pygame
from bullet import Bullet
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_SPACE:
fire_bullet(ai_settings,screen,ship,bullets)
def check_keyup_events(event, ship):
'''响应松开,转换到False'''
if event.key == pygame.K_RIGHT:
ship.moving_right = False
elif event.key == pygame.K_LEFT:
ship.moving_left = False
def check_events(ai_settings, screen, ship, 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)
def update_screen(ai_settings, screen, ship, bullets):
'''更新屏幕上的图像,并切换到新屏幕'''
screen.fill(ai_settings.bg_color)
ship.blitme()
for bullet in bullets.sprites():
bullet.draw_bullet()
ship.blitme()
pygame.display.flip()
def update_bullets(bullets):
'''更新子弹的位置,并删除已消失的子弹'''
bullets.update()
for bullet in bullets.copy():
if bullet.rect.bottom <= 0:
bullets.remove(bullet)
print(len(bullets))
def fire_bullet(ai_setting,screen,ship,bullets):
'''如果还没有达到限制,就发射一颗子弹'''
if len(bullets) < ai_setting.bullet_allowed:
new_bullet = Bullet(ai_setting,screen,ship)
bullets.add(new_bullet)
飞船设置
"""ship.py包含Ship类,这个类包含方法_init_(),管理飞船位置的方法update()以及在屏幕上绘制飞船的方法blitme().表示飞船的图像存储在本地文件夹中"""
import pygame
class Ship():
def __init__(self, ai_settings, screen):
"""初始化飞船并设置其初始化的位置"""
self.screen = screen
self.ai_settings = ai_settings
self.image = pygame.image.load('images/me2.png')
'''在游戏中,所有可见的元素都是以 矩形区域 来描述位置的'''
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
self.center = float(self.rect.centerx)
self.rect.bottom = float(self.rect.bottom)
self.moving_right = False
self.moving_left = False
def update(self):
"""根据移动标志调整飞船的位置"""
if self.moving_right and self.rect.right < self.screen_rect.right:
self.center += self.ai_settings.ship_speed_factor
if self.moving_left and self.rect.left > 0:
self.center -= self.ai_settings.ship_speed_factor
self.rect.centerx = self. center
def blitme(self):
'''在指定位置绘制飞船'''
self.screen.blit(self.image, self.rect)
|