splitting asteroids, game done
This commit is contained in:
23
asteroid.py
23
asteroid.py
@@ -1,5 +1,8 @@
|
||||
from circleshape import CircleShape
|
||||
import pygame
|
||||
import random
|
||||
|
||||
from constants import ASTEROID_MIN_RADIUS
|
||||
|
||||
class Asteroid(CircleShape):
|
||||
def __init__(self, x, y, radius):
|
||||
@@ -11,3 +14,23 @@ class Asteroid(CircleShape):
|
||||
|
||||
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
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user