-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasteroidshape.py
More file actions
38 lines (31 loc) · 1.27 KB
/
Copy pathasteroidshape.py
File metadata and controls
38 lines (31 loc) · 1.27 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
import pygame
from constants import *
import random
import math
class AsteroidShape(pygame.sprite.Sprite):
def __init__(self, x, y, radius):
if hasattr(self, "containers"):
super().__init__(self.containers)
else:
super().__init__()
self.position = pygame.Vector2(x, y)
self.velocity = pygame.Vector2(0, 0)
self.radius = radius
self.kind = random.randint(1, ASTEROID_KINDS)
self.points = self.generate_asteroid_points(self.radius)
def generate_asteroid_points(self, radius):
points = []
for i in range(10):
angle = i * math.pi * 2 / 10
x = radius * math.cos(angle) + random.uniform(-radius/5, radius/5)
y = radius * math.sin(angle) + random.uniform(-radius/5, radius/5)
points.append((x, y))
return points
def draw(self, screen):
color = ("green", "blue", "red")[self.kind - 1]
offset_points = [(self.position.x + x, self.position.y + y) for x, y in self.points]
pygame.draw.polygon(screen, color, offset_points, 1)
def update(self, dt):
self.position += self.velocity * dt
def is_colliding(self, other):
return self.position.distance_to(other.position) <= self.radius + other.radius