Files
bootdev_asteroids/asteroid.py

37 lines
1020 B
Python

from circleshape import CircleShape
import pygame
import random
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, (255,255,255), (self.position.x, self.position.y), self.radius)
def update(self, dt):
self.position += self.velocity * dt
def split(self):
self.kill()
if self.radius <= ASTEROID_MIN_RADIUS:
return
else:
angle = random.uniform(20, 50)
rand_angle1 = self.position.rotate(angle)
rand_angle2 = self.position.rotate(-angle)
new_radius = self.radius - ASTEROID_MIN_RADIUS
asteroid1 = Asteroid(self.position.x, self.position.y, new_radius)
asteroid1.velocity = rand_angle1 * 1.2
asteroid2 = Asteroid(self.position.x, self.position.y, new_radius)
asteroid2.velocity = rand_angle2 * 1.2