@websec007
2020-06-16T22:14:23.000000Z
字数 1453
阅读 986
2020.Python从入门到实践
(子弹存放)
& (子弹绘制)
(子弹更新)
引入精灵模块为子弹的存放、子弹更新以及子弹的逐个绘制做准备。
new_bullet = Bullet()
bullets.add(new_bullet)
bullets.update()
for bullet in bullets.spriters():
bullet.draw_bullet()
for bullet in bullets.copy():
if bullet.top < 0
bullets.remove(bullet)
fire_bullet(bullets, game_settings, screen, ship)
update_bullets(bullets)
draw_bullets(bullets)
+++++++++++++++++++fire_bullets(bullets)++++++++++++++++++++++
def fire_bullet(bullets, game_settings, screen, ship):
# 创建新的子弹,并加入编组(在设定的子弹限制范围数量内,);
if len(bullets) < game_settings.bullets_allowed:
# 创建新的子弹
new_bullet = Bullet(screen, game_settings, ship)
# 加入新编组中
bullets.add(new_bullet)
++++++++++++++++++update_bullets(bullets)+++++++++++++++++++++
def update_bullets(bullets):
# 更新被已创建子弹位置,并删除已经消失的子弹
# 更新编组中的每一个子弹
bullets.update()
# 删除已经消失的子弹
for bullet in bullets.copy():
if bullet.rect.bottom <= 0:
bullets.remove(bullet)
+++++++++++++++++draw_bullets(bullets)+++++++++++++++++++++
def draw_bullets(bullets):
# 绘制编组中每一个子弹
for bullet in bullets.sprites():
bullet.draw_bullet()
通过代码重构的回顾,突然好像明白了整个子弹游戏内容的实现就3个核心方法与1个子弹类;
3个核心方法
1个子弹类