当前位置:   article > 正文

Python实战(1)——外星人入侵,实现空格长按发射子弹_pygame实现长按功能

pygame实现长按功能

#经过对Python从入门到实践的学习,完成了外星人入侵的代码复现

#于是考虑能否在这个基础上进行一些小小的改进

问题说明

        我发现在射击的部分还存在一定的缺憾,想要快速地射击外星人,依靠每次按下发射一颗子弹有些费手,于是考虑实现长按空格键进行射击,网上搜索了一下没有这么实现的代码,而利用pygame.key.set_repeat() 也无法准确实现,反而影响正常射击。

        于是考虑与移动相似的方法设定一个flag来判断目前的射击状态

目标实现

首先在ship.py中添加了一个flag:self.shooting, 用以判断射击状态。

  1. # Movement flags.
  2. self.moving_right = False
  3. self.moving_left = False
  4. self.shooting = False

接着对game_function.py进行修改。

  1. ##原代码##
  2. def check_keydown_events(event, ai_settings, screen, ship, bullets):
  3. """Respond to keypresses."""
  4. if event.key == pygame.K_RIGHT:
  5. ship.moving_right = True
  6. elif event.key == pygame.K_LEFT:
  7. ship.moving_left = True
  8. elif event.key == pygame.K_SPACE:
  9. fire_bullet(ai_settings, screen, ship, bullets)
  10. elif event.key == pygame.K_q:
  11. sys.exit()
  12. def check_keyup_events(event, ship):
  13. """Respond to key releases."""
  14. if event.key == pygame.K_RIGHT:
  15. ship.moving_right = False
  16. elif event.key == pygame.K_LEFT:
  17. ship.moving_left = False

将flag加入其中:

  1. def check_keydown_events(event, ai_settings, screen, ship, bullets):
  2. """Respond to keypresses."""
  3. if event.key == pygame.K_RIGHT:
  4. ship.moving_right = True
  5. elif event.key == pygame.K_LEFT:
  6. ship.moving_left = True
  7. elif event.key == pygame.K_SPACE:
  8. ship.shooting = True
  9. # fire_bullet(ai_settings, screen, ship, bullets)
  10. elif event.key == pygame.K_q:
  11. sys.exit()
  12. def check_keyup_events(event, ship):
  13. """Respond to key releases."""
  14. if event.key == pygame.K_RIGHT:
  15. ship.moving_right = False
  16. elif event.key == pygame.K_LEFT:
  17. ship.moving_left = False
  18. elif event.key == pygame.K_SPACE:
  19. ship.shooting = False
  1. if ship.shooting:
  2. gf.fire_bullet(ai_settings, screen, ship, bullets)

定义后就可以在主函数中进行调用了,即 可以实现连续射击

  1. while True:
  2. clock.tick(250)
  3. gf.check_events(ai_settings, screen, stats, sb, play_button, ship,
  4. aliens, bullets)
  5. if stats.game_active:
  6. if ship.shooting:
  7. gf.fire_bullet(ai_settings, screen, ship, bullets)
  8. ship.update()
  9. gf.update_bullets(ai_settings, screen, stats, sb, ship, aliens,
  10. bullets)
  11. gf.update_aliens(ai_settings, screen, stats, sb, ship, aliens,
  12. bullets)
  13. --snip--

但是这样在主代码中显得有些臃肿,于是再进行修改。将if shooting放入fire_bullet()中。

  1. def fire_bullet(ai_settings, screen, ship, bullets):
  2. """Fire a bullet, if limit not reached yet."""
  3. # Create a new bullet, add to bullets group.
  4. if len(bullets) < ai_settings.bullets_allowed and ship.shooting:
  5. new_bullet = Bullet(ai_settings, screen, ship)
  6. bullets.add(new_bullet)

 这样主代码变为:

  1. --snip--
  2. if stats.game_active:
  3. gf.fire_bullet(ai_settings, screen, ship, bullets)
  4. ship.update()
  5. --snip--

目前基本实现了预期目的,感觉还可以再精进一下.......

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/123228
推荐阅读
相关标签
  

闽ICP备14008679号