Created
June 13, 2021 10:02
-
-
Save PatrickKalkman/8daafdc5f1acb2eccfbb1f30d506ea16 to your computer and use it in GitHub Desktop.
The enemy space ship shoots a rocket towards the player
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def enemy_shoots(self): | |
nr_of_enemies = len(self.all_enemies) | |
if nr_of_enemies > 0: | |
enemy_index = random.randint(0, nr_of_enemies - 1) | |
start_rocket = None | |
for index, enemy in enumerate(self.all_enemies): | |
if index == enemy_index: | |
start_rocket = enemy.rect.center | |
if start_rocket[1] < 400: | |
ySpeed = 7 | |
dx = self.player.rect.centerx - start_rocket[0] | |
dy = self.player.rect.centery - start_rocket[1] | |
number_of_steps = dy / ySpeed | |
xSpeed = dx / number_of_steps | |
rocket = Rocket(self.sprites, xSpeed, ySpeed) | |
rocket.rect.centerx = start_rocket[0] | |
rocket.rect.centery = start_rocket[1] | |
self.enemy_rockets.add(rocket) | |
self.all_sprites.add(rocket) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment