Skip to content

Commit

Permalink
Minor tweaks to starfield example
Browse files Browse the repository at this point in the history
  • Loading branch information
lordmauve committed Apr 16, 2019
1 parent f94e44c commit 258946c
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions examples/basic/stars.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
HEIGHT = 1000 * 9 // 16
ACCEL = 1.0 # Warp factor per second
DRAG = 0.71 # Fraction of speed per second
TRAIL_LENGTH = 3
MIN_WARP_FACTOR = 0.1
BOUNDS = Rect(0, 0, WIDTH, HEIGHT)

Expand All @@ -17,12 +18,17 @@


class Star:
__slots__ = (
'pos', 'vel', 'brightness',
'speed', 'prev_pos'
)

def __init__(self, pos, vel):
self.pos = pos
self.vel = vel
self.brightness = 10
self.speed = math.hypot(*vel)
self.prev_pos = deque(maxlen=3)
self.prev_pos = deque(maxlen=TRAIL_LENGTH)
self.prev_pos.append(self.pos)

def set_pos(self, pos):
Expand Down Expand Up @@ -60,13 +66,13 @@ def update(dt):
(warp_factor - MIN_WARP_FACTOR) * DRAG ** dt
)

for _ in range(int(dt * 200)):
while len(stars) < 300:
angle = random.uniform(-math.pi, math.pi)
dx = math.cos(angle)
dy = math.sin(angle)
speed = 255 * random.uniform(0.3, 1.0) ** 2

d = random.uniform(0, 100)
d = random.uniform(25, 100)
pos = centerx + dx * d, centery + dy * d

v = speed * dx, speed * dy
Expand All @@ -85,7 +91,7 @@ def update(dt):


# Jump-start the star field
for _ in range(10):
for _ in range(30):
update(0.5)
for _ in range(5):
update(1 / 60)

0 comments on commit 258946c

Please sign in to comment.