diff --git a/asteroid.py b/asteroid.py index f97b5e5..b213cf2 100644 --- a/asteroid.py +++ b/asteroid.py @@ -1,6 +1,7 @@ import circleshape import pygame import constants +import random class Asteroid(circleshape.CircleShape): @@ -14,3 +15,18 @@ class Asteroid(circleshape.CircleShape): def update(self, dt): self.position += self.velocity * dt + def split(self): + self.kill() + + if self.radius <= constants.ASTEROID_MIN_RADIUS: + return + else: + rand_angle = random.uniform(20, 50) + new_velocity1 = self.velocity.rotate(rand_angle) + new_velocity2 = self.velocity.rotate(-rand_angle) + new_radius = self.radius - constants.ASTEROID_MIN_RADIUS + + asteroid1 = Asteroid(self.position.x, self.position.y, new_radius) + asteroid1.velocity = new_velocity1 * 1.2 + asteroid2 = Asteroid(self.position.x, self.position.y, new_radius) + asteroid2.velocity = new_velocity2 * 1.2 diff --git a/main.py b/main.py index 469c6a5..b302f61 100644 --- a/main.py +++ b/main.py @@ -58,7 +58,7 @@ def main(): for asteroid in asteroids: for shot in shots: if shot.is_colided(asteroid): - asteroid.kill() + asteroid.split() shot.kill() pygame.display.flip() tick = clock.tick(60)