This repository has been archived by the owner on Dec 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HyperDrive.py
136 lines (94 loc) · 3.51 KB
/
HyperDrive.py
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import pygame
import random
import time
import math
from common import BaseSprite, FrameCounter
max_mass = 2500
screen_size = (800, 600)
class Star(BaseSprite):
brightness_range = [63, 255]
containers = []
def __init__(self, position, angle, speed, size=3, twinkle=False):
BaseSprite.__init__(self, self.containers)
self.position = position
self.angle = angle
self.speed = speed
self.twinkle = twinkle
self.rect = pygame.Rect(position, (size, size))
self.image = pygame.Surface((size, size))
self.size = size
self.image.fill((255, 255, 255))
def set_size(self, size):
self.size = size
self.rect.height = self.rect.width = size
self.image = pygame.Surface((size, size))
self.image.fill((255, 255, 255))
def update_dt(self, dt):
if dt == 0:
return
self.dirty = 1
# collision = pygame.sprite.spritecollideany(self, self.containers[0])
collision = False
if collision:
reflection_plane = (self.angle + collision.angle) / 2
self.angle = reflection_plane - (self.angle - reflection_plane)
collision.angle = reflection_plane - (collision.angle - reflection_plane)
m_self = self.speed*self.size**2
m_col = collision.speed * collision.size**2
m_avg = (m_self + m_col)/2
self.speed = m_avg / self.size**2
collision.speed = m_avg / collision.size**2
# combine if small enough
if collision.size + 2 <= self.size:
collision.kill()
self.set_size(math.sqrt(self.size**2 + collision.size**2))
x, y = self.position
x += self.speed * dt * math.cos(math.radians(self.angle))
y += self.speed * dt * math.sin(math.radians(self.angle))
self.rect.center = self.position = (x, y)
if self.twinkle:
b = random.randint(self.brightness_range[0], self.brightness_range[1])
self.image.fill((b, b, b))
if (x < -self.size / 2 or x > screen_size[0] + self.size / 2) or \
(y < -self.size / 2 or y > screen_size[1] + self.size / 2):
self.kill()
# print(self.rect.center)
class Stars(pygame.sprite.RenderUpdates):
def get_total_mass(self):
total_mass = 0
for s in self.sprites():
total_mass += s.size**2
return total_mass
def make_star():
x = random.randint(0, screen_size[0])
y = random.randint(0, screen_size[1])
# angle = random.randint(0, 360)
angle = math.degrees(math.atan2(y - screen_size[1]/2, x - screen_size[0]/2))
size = random.randint(1, 7)
speed = 20*size
Star((x, y), angle, speed, size=size, twinkle=False)
def main():
pygame.init()
screen = pygame.display.set_mode(screen_size)
stars = Stars()
Star.containers.append(stars)
counter = FrameCounter()
while stars.get_total_mass() < max_mass:
make_star()
try:
while not pygame.event.peek(pygame.QUIT):
pygame.event.poll()
while stars.get_total_mass() < max_mass:
make_star()
screen.fill((0, 0, 0))
stars.update()
stars.draw(screen)
counter.update()
# counter.image.blit(screen, counter.rect)
screen.blit(counter.image, counter.rect)
pygame.display.flip()
except KeyboardInterrupt:
pass
print("Quitting...")
if __name__ == "__main__":
main()