# 昨日知识点回顾
方法重构、驾驶飞船左右移动、全屏显示
飞船不移动解决,问题出在移动变量x更新
# Ship.pysnipdef update(self):"""根据移动标志调整飞船的位置"""# 更新飞船而不是rect对象的x值# 如果飞船右移的标志和飞船外接矩形右边缘的x坐标 小于 屏幕右边缘x坐标,开始右移if self.moving_right and self.rect.right < self.screen_rect.right:self.x += self.settings.ship_speed# self.rect.x += self.settings.ship_speed# 如果飞船左移的标志和飞船外接矩形左边缘的x坐标 大于于 0,开始左移if self.moving_left and self.rect.left > 0:self.x -= self.settings.ship_speed# self.rect.x -= self.settings.ship_speed# 根据self.x 更新rect对象self.rect.x = self.x
# 今日知识点学习
12.8 射击
12.8.1 添加子弹设置
# Settings.py
class Settings():"""存储《外星人入侵》的所有设置的类"""def __init__(self):"""初始化游戏的设置"""# 屏幕设置self.screen_width = int(1200)self.screen_height = int(800)self.bg_color = (230, 230, 230)# 飞船设置self.ship_speed = 1.5# 子弹设置self.bullet_speed = 1.0self.bullet_width = 3self.bullet_height = 15self.bullet_color = (60, 60, 60)
12.8.2 创建Bullet类
#bullet.py
import pygame
from pygame.sprite import Spriteclass Bullet(Sprite):"""管理飞船所发射的子弹"""def __init__(self, ai_game):"""在飞船当前位置创建一个子弹对象"""super().__init__()self.screen = ai_game.screenself.settings = ai_game.settingsself.color = self.settings.bullet_color# 在(0,0)处创建一个表示子弹的矩形,再设置正确的位置self.rect = pygame.Rect(0, 0, self.settings.bullet_width, self.settings.bullet_height)self.rect.midtop = ai_game.ship.rect.midtop# 存储用小数表示的子单位置self.y = float(self.rect.y)def update(self):"""向上移动子弹"""# 更新表示子弹位置的小数值self.y -= self.settings.bullet_speed# 更新表示子弹的rect位置self.rect.y = self.ydef draw_bullet(self):"""在屏幕上绘制子弹"""pygame.draw.rect(self.screen, self.color, self.rect)
12.8.3 将子弹存储在编组中
#主程序
import sys
import pygame
from Settings import Settings
from Ship import Shipclass AlienInvasion:"""管理游戏资源和行为的类"""def __init__(self):"""初始化游戏并创建游戏资源"""pygame.init()self.settings = Settings()# 非全屏运行self.screen = pygame.display.set_mode((self.settings.screen_width, self.settings.screen_height))# 全屏运行# self.screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)# self.settings.screen_width = self.screen.get_rect().width# self.settings.screen_height = self.screen.get_rect().heightpygame.display.set_caption("Alien Invasion")self.ship = Ship(self)# 创建用于存储子弹的编组self.bullets = pygame.sprite.Group()def run_game(self):"""开始游戏的主循环"""while True:self._check_events()self.ship.update()# 更新子弹的位置self.bullets.update()self._update_screen()
12.8.4 开火
import sys
import pygame
from Settings import Settings
from Ship import Ship
from bullet import Bulletclass AlienInvasion:"""管理游戏资源和行为的类"""def __init__(self):"""初始化游戏并创建游戏资源"""pygame.init()self.settings = Settings()# 非全屏运行self.screen = pygame.display.set_mode((self.settings.screen_width, self.settings.screen_height))# 全屏运行# self.screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)# self.settings.screen_width = self.screen.get_rect().width# self.settings.screen_height = self.screen.get_rect().heightpygame.display.set_caption("Alien Invasion")self.ship = Ship(self)self.bullets = pygame.sprite.Group()def run_game(self):"""开始游戏的主循环"""while True:self._check_events()self.ship.update()self.bullets.update()self._update_screen()def _check_events(self):"""响应按键和鼠标事件"""for event in pygame.event.get():if event.type == pygame.QUIT:sys.exit()elif event.type == pygame.KEYDOWN:self._check_keydown_events(event)elif event.type == pygame.KEYUP:self._check_keyup_events(event)def _check_keydown_events(self, event):"""响应按键"""if event.key == pygame.K_RIGHT:self.ship.moving_right = Trueelif event.key == pygame.K_LEFT:self.ship.moving_left = Trueelif event.key == pygame.K_q:sys.exit()elif event.key == pygame.K_SPACE:self._fire_bullet()def _check_keyup_events(self, event):"""响应松开"""if event.key == pygame.K_RIGHT:self.ship.moving_right = Falseelif event.key == pygame.K_LEFT:self.ship.moving_left = Falsedef _fire_bullet(self):"""创建一颗子弹,并将其加入编组bullets中"""new_bullet = Bullet(self)self.bullets.add(new_bullet)def _update_screen(self):"""更新屏幕上的图像,并切换到新屏幕。"""self.screen.fill(self.settings.bg_color)self.ship.blitme()for bullet in self.bullets.sprites():bullet.draw_bullet()# 让最近绘制的屏幕可见pygame.display.flip()if __name__ == '__main__':# 创建游戏实例并运行游戏ai = AlienInvasion()ai.run_game()
12.8.5 删除消失的子弹
def run_game(self):"""开始游戏的主循环"""while True:self._check_events()self.ship.update()self.bullets.update()# 删除消失的子弹for bullet in self.bullets.copy():if bullet.rect.bottom <= 0:self.bullets.remove(bullet)print(len(self.bullets))self._update_screen()
12.8.6 限制子弹的数量
#Settings.py
# snip
# 子弹设置self.bullet_speed = 1.0self.bullet_width = 3self.bullet_height = 15self.bullet_color = (60, 60, 60)self.bullets_allowed = 3#主程序
# snipdef _fire_bullet(self):"""创建一颗子弹,并将其加入编组bullets中"""if len(self.bullets) < self.settings.bullets_allowed:new_bullet = Bullet(self)self.bullets.add(new_bullet)
12.8.7 创建方法_update_bullets()
def run_game(self):"""开始游戏的主循环"""while True:self._check_events()self.ship.update()self.bullets.update()self._update_bullets()# # 删除消失的子弹# for bullet in self.bullets.copy():# if bullet.rect.bottom <= 0:# self.bullets.remove(bullet)# print(len(self.bullets))self._update_screen()def _update_bullets(self):"""更新子弹的位置并消除消失的子弹"""# 更新子弹的位置self.bullets.update()# 删除消失的子弹for bullet in self.bullets.copy():if bullet.rect.bottom <= 0:self.bullets.remove(bullet)
运行结果: