Files
pysteroids/asteroid.py
2025-03-10 13:00:07 +01:00

32 lines
986 B
Python

import random
import pygame
from circleshape import CircleShape
from constants import ASTEROID_MIN_RADIUS
class Asteroid(CircleShape):
def __init__(self, x, y, radius):
super().__init__(x, y, radius)
def draw(self, screen):
pygame.draw.circle(screen, "white", self.position, self.radius, 2)
def update(self, dt):
self.position += self.velocity * dt
def split(self):
self.kill()
if self.radius <= ASTEROID_MIN_RADIUS:
return
new_angle = random.uniform(20,50)
new_velocity_1 = self.velocity.rotate(new_angle)
new_velocity_2 = self.velocity.rotate(-new_angle)
new_radius = self.radius - ASTEROID_MIN_RADIUS
new_asteroid_1 = Asteroid(self.position[0], self.position[1], new_radius)
new_asteroid_2 = Asteroid(self.position[0], self.position[1], new_radius)
new_asteroid_1.velocity = new_velocity_1 * 1.2
new_asteroid_2.velocity = new_velocity_2 * 1.2