# 昨日知识点回顾
操控飞船移动发射子弹,删除屏幕之外的子弹
#今日知识点学习
第13章 外星人
13.1 项目回顾
项目添加新功能前审核既有代码,对混乱或低效的代码进行清理
13.2 创建第一个外星人
13.2.1 创建Alien类
# alien.py
import pygame
from pygame.sprite import Sprite
class Alien(Sprite):
"""表示单个外星人的类"""
def __init__(self, ai_game):
"""初始化未设置其起始位置"""
super().__init__()
self.screen = ai_game.screen
# 加载外星人图像,并设置其rect属性
self.image = pygame.image.load('images/alien.bmp')
self.rect = self.image.get_rect()
# 每个外星人最初都在屏幕左上角附近
self.rect.x = self.rect.width
self.rect.y = self.rect.height
# 存储外星人的准确位置
self.x = float(self.rect.x)
13.2.2 创建Alien实例
# 主程序
import sys
import pygame
from Settings import Settings
from Ship import Ship
from alien import Alien
from bullet import Bullet
class 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().height
pygame.display.set_caption("Alien Invasion")
self.ship = Ship(self)
self.bullets = pygame.sprite.Group()
self.aliens = pygame.sprite.Group()
self._create_fleet()
def _create_fleet(self):
"""创建外星人群"""
# 创建一个外星人
alien = Alien(self)
self.aliens.add(alien)
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 _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 = True
elif event.key == pygame.K_LEFT:
self.ship.moving_left = True
elif 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 = False
elif event.key == pygame.K_LEFT:
self.ship.moving_left = False
def _fire_bullet(self):
"""创建一颗子弹,并将其加入编组bullets中"""
if len(self.bullets) < self.settings.bullets_allowed:
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()
self.alens.draw(self.screen)
# 让最近绘制的屏幕可见
pygame.display.flip()
def _update_bullets(self):
"""更新子弹的位置并消除消失的子弹"""
# 更新子弹的位置
self.bullets.update()
# 删除消失的子弹
for bullet in self.bullets.copy():
if bullet.rect.bottom <= 0:
self.bullets.remove(bullet)
if __name__ == '__main__':
# 创建游戏实例并运行游戏
ai = AlienInvasion()
ai.run_game()
运行结果:(第一个外星人现身)
13.3 创建一群外星人
13.1 确定一行可容纳多少个外星人
可用于放置外星人的水平空间=屏幕宽度-机器人宽度的两倍
available_space_x = settings.screen.width - ( 2 * alien_width)
外星人之间空间宽度设为外星人的宽度,一行容纳外星人个数
number_aliens_x = available_space_x // ( 2* alien_width)
13,2 创建一行外星人
# 主程序
import sys
import pygame
from Settings import Settings
from Ship import Ship
from alien import Alien
from bullet import Bullet
class 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().height
pygame.display.set_caption("Alien Invasion")
self.ship = Ship(self)
self.bullets = pygame.sprite.Group()
self.aliens = pygame.sprite.Group()
self._create_fleet()
def _create_fleet(self):
"""创建外星人群"""
# 创建一个外星人并计算一行可容纳多少个外星人
# 外星人的间距为外星人的宽度
alien = Alien(self)
alien_width = alien.rect.width
available_space_x = self.settings.screen_width - (2 * alien_width)
number_aliens_x = available_space_x // (2 * alien_width)
# 创建第一行外星人
for alien_number in range(number_aliens_x):
# 创建一个外星人并将其加入当前行
alien = Alien(self)
alien.x = alien_width + 2 * alien_width * alien_number
self.rect.x = alien.x
self.aliens.add(alien)
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 _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 = True
elif event.key == pygame.K_LEFT:
self.ship.moving_left = True
elif 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 = False
elif event.key == pygame.K_LEFT:
self.ship.moving_left = False
def _fire_bullet(self):
"""创建一颗子弹,并将其加入编组bullets中"""
if len(self.bullets) < self.settings.bullets_allowed:
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()
self.aliens.draw(self.screen)
# 让最近绘制的屏幕可见
pygame.display.flip()
def _update_bullets(self):
"""更新子弹的位置并消除消失的子弹"""
# 更新子弹的位置
self.bullets.update()
# 删除消失的子弹
for bullet in self.bullets.copy():
if bullet.rect.bottom <= 0:
self.bullets.remove(bullet)
if __name__ == '__main__':
# 创建游戏实例并运行游戏
ai = AlienInvasion()
ai.run_game()
# 运行结果:
# Traceback (most recent call last):
# File "D:\Program Files\JetBrains\jetbra\scripts\test_Pygame.py", line 120, in <module>
# ai = AlienInvasion()
# ^^^^^^^^^^^^^^^
# File "D:\Program Files\JetBrains\jetbra\scripts\test_Pygame.py", line 28, in __init__
# self._create_fleet()
# File "D:\Program Files\JetBrains\jetbra\scripts\test_Pygame.py", line 44, in _create_fleet
# self.rect.x = alien.x
# ^^^^^^^^^
# AttributeError: 'AlienInvasion' object has no attribute 'rect'
---未完待续---