-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstargfx.py
91 lines (69 loc) · 2.37 KB
/
stargfx.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
import sys, math, pygame, random
from pygame.locals import *
def gaussian_decay(A, sigma, x, y):
""" Return the Z value along a gaussian distribution given x,y
coordinates"""
return A * math.exp(-((pow(x-16.,2.)/(2*pow(sigma,2)) + \
(pow(y-16.,2.)/(2*pow(sigma,2))))))
class stargfx:
def __init__(self):
self.stars = []
def generate_star(self, size, color):
s = pygame.Surface((64,64))
s.set_colorkey((0,0,0))
s.set_alpha(150)
r = color[0]
g = color[1]
b = color[2]
for i in range(0, 32):
for j in range(0, 32):
r_decay = gaussian_decay(r, size, i, j)
g_decay = gaussian_decay(g, size, i, j)
b_decay = gaussian_decay(b, size, i, j)
s.set_at((i,j), (r_decay, g_decay, b_decay))
return s
def generate_row(self, color):
cache = []
for i in [x * 0.05 for x in range(1, 72)] :
cache.append(self.generate_star(i,color))
return cache
def blit_row(self, stars, row):
i = 0
for star in stars:
self.screen.blit(star, (i*32,row*32))
i+=1
def run_test(self):
""" Used to visually validate star creation """
pygame.init()
self.screen = pygame.display.set_mode((640, 640))
self.clock = pygame.time.Clock()
self.screen.fill((0,0,0))
# Red
stars = self.generate_row((255,0,0))
self.blit_row(stars,0)
# yellow
stars = self.generate_row((255,255,0))
self.blit_row(stars,1)
# white
stars = self.generate_row((255,255,255))
self.blit_row(stars,2)
# blue
stars = self.generate_row((0,0,255))
self.blit_row(stars,3)
# white/blue
stars = self.generate_row((200,200,255))
self.blit_row(stars,4)
# white/yellow
stars = self.generate_row((255,255,200))
self.blit_row(stars,5)
# white/red
stars = self.generate_row((255,200,200))
self.blit_row(stars,6)
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
self.clock.tick(50)
pygame.display.flip()
if __name__ == "__main__":
stargfx().run_test()