练习 13-5: 侧面射击 2 完成练习 12-6之后,我们给游戏《外星人入侵》添加了很多功能。在本练习中,请尝试让练习12-6中飞船的功能与当前《外星人入侵》中的类似。在屏幕右侧添加一群外星人(或让外星人的位置随机),并让其向飞船移动。另外,编写代码让被子弹击中的外星人消失。
效果图如下:
在往期“作业:侧面射击之排山倒海”的基础上,修改代码,增加一群白展堂并使其向下移动,接触窗口边缘时向左移动一点距离并朝反方向(向上)继续移动,使郭芙蓉发出的“排山倒海”武功接触白展堂时武功和白展堂同时消失。
(1)新建 zhantang.py,代码如下:
import pygame
from pygame.sprite import Sprite
class Zhantang(Sprite):
def __init__(self, ai_game):
"""初始化白展堂并设置其起始位置"""
super().__init__()
self.screen = ai_game.screen
self.settings = ai_game.settings
# 加载白展堂图像并设置其rect属性
self.image = pygame.image.load('images/zhantang_2.bmp')
self.rect = self.image.get_rect()
# 设置位置
self.screen_rect = ai_game.screen.get_rect()
self.rect.topright = self.screen_rect.topright
self.x = float(self.rect.x)
def check_edges(self):
"""如果大侠位于屏幕边缘,就返回True"""
screen_rect = self.screen.get_rect()
if self.rect.bottom >= screen_rect.height or self.rect.top <= -1:
return True
def update(self):
"""向下或向上移动"""
self.y += (self.settings.zhantang_speed * self.settings.daxia_direction)
self.rect.y = self.y
(2)修改 settings.py,在末尾增加白展堂相关设置:
# 白展堂设置
# 移动速度
self.zhantang_speed = 0.1
self.zhantang_width = 88
# 每次触碰边缘后向左移动的距离
self.zhantang_left_speed = 5
# 1为下移,-1为上移
self.daxia_direction = 1
(3)修改 kung_fu.py,增加代码如下:
import sys
import pygame
from settings import Settings
from furong import Furong
from attack import Attack
from zhantang import Zhantang
class Kungfu:
def __init__(self):
pygame.init()
self.settings = Settings()
self.screen = pygame.display.set_mode(
(self.settings.screen_width, self.settings.screen_height))
pygame.display.set_caption('排山倒海')
self.furong = Furong(self)
self.attack = pygame.sprite.Group()
# 创建一个用于存储白展堂大侠的编组
self.zhantangs = pygame.sprite.Group()
self._create_daxia()
def run_game(self):
while True:
self._check_events()
self.furong.update()
self._upadte_attack()
self._update_zhantangs()
self._update_screen()
--snip--
def _create_daxia(self):
"""创建一群大侠"""
# 创建一个大侠
zhantang = Zhantang(self)
zhantang_width = self.settings.zhantang_width
avaliable_space_x = self.settings.screen_width - self.settings.zhantang_width
number_zhantang_x = avaliable_space_x // (zhantang_width)
zhantang_height = zhantang.rect.height
number_rows = self.settings.screen_height // zhantang_height
for row_number in range(number_rows):
for zhantang_number in range(number_zhantang_x):
# 创建一个大侠并将其加入当前行
zhantang = Zhantang(self)
zhantang.x -= zhantang_width * zhantang_number
zhantang.rect.x = zhantang.x
zhantang.y = zhantang.rect.height * row_number
zhantang.rect.y = zhantang.y
self.zhantangs.add(zhantang)
def _update_zhantangs(self):
"""检查是否有白展堂位于屏幕边缘,更新大侠群中所有白展堂的位置"""
self._check_daxia_edges()
self.zhantangs.update()
def _update_screen(self):
self.screen.fill(self.settings.bg_color)
self.furong.blitme()
for attack in self.attack.sprites():
attack.draw_attack()
# 对白展堂编组调用方法 draw()
self.zhantangs.draw(self.screen)
pygame.display.flip()
def _upadte_attack(self):
self.attack.update()
for attack in self.attack.copy():
if attack.rect.left >= self.screen.get_rect().right:
self.attack.remove(attack)
# 检查是否有排山倒海击中了白展堂,如果是,就删除相应的武功和白展堂
collisions = pygame.sprite.groupcollide(self.attack, self.zhantangs, True, True)
def _check_daxia_edges(self):
"""白展堂到达边缘时采取相应的措施"""
for zhantang in self.zhantangs.sprites():
if zhantang.check_edges():
self._change_daxia_direction()
break
def _change_daxia_direction(self):
"""将白展堂下移,并改变他们的方向"""
for zhantang in self.zhantangs.sprites():
zhantang.rect.x -= self.settings.zhantang_left_speed
self.settings.daxia_direction *= -1
if __name__ == '__main__':
ai = Kungfu()
ai.run_game()