added asteroid splitting

This commit is contained in:
specCon18 2025-01-21 04:21:39 -05:00
parent 82882bab74
commit e36416de3a
2 changed files with 17 additions and 1 deletions

View file

@ -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

View file

@ -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)