一、运行效果
二、实现步骤
1、创建Python项目ShipFireBullets
2、创建images目录,拷贝飞船图片ship.png
3、创建游戏设置文件settings.py
class Settings():
# 初始化游戏设置
def __init__(self):
# 屏幕设置
self.screen_width = 1000
self.screen_height = 600
self.bg_color = (230, 230, 230)
# 飞船设置
self.ship_speed_factor = 1.5
# 子弹设置
self.bullet_speed_factor = 1
self.bullet_width = 15
self.bullet_height = 1
self.bullet_color = 60, 60, 60
self.bullets_allowed = 3
4、创建飞船类文件ship.py
import pygame
class Ship():
# 初始化飞船
def __init__(self, ai_settings, screen):
self.screen = screen
self.ai_settings = ai_settings
# 加载船图像,获取其矩形
self.image = pygame.image.load('images/ship.bmp')
self.rect = self.image.get_rect()
self.screen_rect = screen.get_rect()
# 移动标记
self.moving_up = False
self.moving_down = False
# 每艘飞船出现在屏幕左边中央
self.rect.left = 0
self.rect.centery = self.screen_rect.centery
# 设置飞船垂直中心位置的浮点值
self.center = float(self.rect.centery)
def update(self):
# 向下移动,不能移出下边界
if self.moving_down and self.rect.bottom < self.screen_rect.bottom:
self.center += self.ai_settings.ship_speed_factor
# 向上移动,不能移出上边界
if self.moving_up and self.rect.top > 0:
self.center -= self.ai_settings.ship_speed_factor
# 设置飞船垂直中心位置的整数值
self.rect.centery = self.center
def blitme(self):
# 在当前位置绘制飞船
self.screen.blit(self.image, self.rect)
5、创建子弹类文件bullet.py
import pygame
from pygame.sprite import Sprite
# 子弹类
class Bullet(Sprite):
# 初始化子弹
def __init__(self, ai_settings, screen, ship):
"""在飞船当前位置创建一个子弹对象"""
super().__init__()
self.screen = screen
# 在(0, 0)处创建子弹矩形,然后设置正确位置
self.rect = pygame.Rect(0, 0, ai_settings.bullet_width, ai_settings.bullet_height)
self.rect.centery = ship.rect.centery
self.rect.right = ship.rect.right
# 将子弹位置保存为十进制小数值
self.x = float(self.rect.x)
# 设置子弹颜色
self.color = ai_settings.bullet_color
# 设置子弹速度
self.speed_factor = ai_settings.bullet_speed_factor
# 更新子弹位置
def update(self):
# 更新子弹的十进制小数位置
self.x += self.speed_factor
# 更新子弹的矩形位置
self.rect.x = self.x
# 绘制子弹
def draw_bullet(self):
pygame.draw.rect(self.screen, self.color, self.rect)
6、创建函数文件game_functions.py
import sys
import pygame
from bullet import Bullet
# 检查按键按下事件
def check_keydown_events(event, ai_settings, screen, ship, bullets):
if event.key == pygame.K_DOWN:
ship.moving_down = True
elif event.key == pygame.K_UP:
ship.moving_up = True
elif event.key == pygame.K_SPACE:
fire_bullet(ai_settings, screen, ship, bullets)
# 发射子弹
def fire_bullet(ai_settings, screen, ship, bullets):
# 子弹数未超过极限时,创建一个新子弹,添加到子弹组里
if len(bullets) < ai_settings.bullets_allowed:
new_bullet = Bullet(ai_settings, screen, ship)
bullets.add(new_bullet)
# 检查按键弹起事件
def check_keyup_events(event, ship):
if event.key == pygame.K_DOWN:
ship.moving_down = False
elif event.key == pygame.K_UP:
ship.moving_up = 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()
# 局部更新屏幕最近所绘
pygame.display.flip()
# 更新子弹组
def update_bulletes(screen, bullets):
# 更新子弹组
bullets.update()
# 删除已经消失的子弹
for bullet in bullets.copy():
if bullet.rect.left > screen.get_rect().right:
bullets.remove(bullet)
# 打印子弹数量
print(len(bullets), end=' ')
7、创建游戏主程序文件game_main.py
import pygame
import game_functions as gf
from settings import Settings
from ship import Ship
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("飞船发射子弹")
# 创建一艘飞船
ship = Ship(ai_settings, screen)
# 创建一个组用于存放子弹
bullets = Group()
# 启动游戏主循环
while True:
# 监听事件
gf.check_events(ai_settings, screen, ship, bullets)
# 更新飞船
ship.update()
# 更新子弹组
gf.update_bulletes(screen, bullets)
# 更新屏幕
gf.update_screen(ai_settings, screen, ship, bullets)
# 启动游戏
run_game()
运行game_main程序,结果如下:
按上下方向键可以让飞船上下移动,但是不能移出边界。按空格键,可以向右发射子弹。