-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasteroid.py
More file actions
43 lines (36 loc) · 1.41 KB
/
Copy pathasteroid.py
File metadata and controls
43 lines (36 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import pygame
from circleshape import CircleShape
from constants import ASTEROID_MIN_RADIUS
import random
class Asteroid(CircleShape):
def __init__(self, x, y, radius):
pygame.sprite.Sprite.__init__(self, self.containers)
CircleShape.__init__(self, x, y, radius)
self.position = pygame.Vector2(x, y)
self.velocity = pygame.Vector2(0, 0)
def draw(self, screen):
pygame.draw.circle(screen,"White",(self.position.x, self.position.y),self.radius, 2)
def update(self, dt):
self.position += self.velocity * dt
self.x = self.position.x
self.y = self.position.y
def split(self):
self.kill()
if self.radius <= ASTEROID_MIN_RADIUS:
return
random_angle = random.uniform(20, 50)
new_velocity1 = self.velocity.rotate(random_angle)
new_velocity2 = self.velocity.rotate(-random_angle)
new_radius = self.radius - ASTEROID_MIN_RADIUS
asteroid1 = Asteroid(self.rect.centerx, self.rect.centery, new_radius)
asteroid1.velocity = new_velocity1 * 1.2
asteroid2 = Asteroid(self.rect.centerx, self.rect.centery, new_radius)
asteroid2.velocity = new_velocity2 * 1.2
@property
def rect(self):
return pygame.Rect(
self.position.x - self.radius,
self.position.y - self.radius,
self.radius * 2,
self.radius * 2
)