-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhd-puff-1.py
140 lines (101 loc) · 3.84 KB
/
hd-puff-1.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
137
138
139
140
# Experiment with high resolution
import pygame
import math
import time
import random
# 2D Guass funxction from https://en.wikipedia.org/wiki/Gaussian_function#Two-dimensional_Gaussian_function
# A - Amplitude
# xo, yo - the center
# sigx, sigy - x and y spreads of the blob
def my_gauss2d(x, y, A, xo, yo, sigx, sigy):
x_num = math.pow((x - xo), 2)
y_num = math.pow((y - yo), 2)
x_den = 2 * math.pow(sigx, 2)
y_den = 2 * math.pow(sigy, 2)
return A * math.exp(- ( (x_num / x_den) + (y_num / y_den) ) )
def create_puff(width, height):
puff = pygame.Surface((width, height))
xo = width / 2
yo = height / 2
sigx = width / 6
sigy = height / 6
for x in range(0, width):
for y in range(0, height):
opacity = my_gauss2d(x, y, 1, xo, yo, sigx, sigy)
alpha = (255*opacity)
puff.set_at((x, y), (255, 255, 255, alpha))
return puff
class Puff:
"""A Gaussian Puff"""
def __init__(self, width, height, surface):
self.w = width
self.h = height
self.xinc = random.choice([ -2, -1, -1, 1, 1, 2])
self.yinc = random.choice([ -2, -1, -1, 1, 1, 2])
self.surface = surface
self.puff = create_puff(self.w, self.h)
self.init_pos()
def init_pos(self):
self.x = int(self.surface.get_width () * random.random())
self.y = int(self.surface.get_height() * random.random())
def move_pos(self):
if self.x >= (self.surface.get_width() - self.w):
self.xinc *= -1
elif self.x <= 0:
self.xinc *= -1
if self.y >= (self.surface.get_height() - self.h):
self.yinc *= -1
elif self.y <= 0:
self.yinc *= -1
self.x += self.xinc
self.y += self.yinc
#print ("%d %d" % (self.x, self.y))
def my_gauss(a, b, c, x):
return a * math.exp(- (math.pow((x-b), 2) / (2 * math.pow(c, 2))))
def create_background(width, height):
start_color = pygame.Color(0, 0, 0)
end_color = pygame.Color(255, 0, 0)
background = pygame.Surface((width, height))
for y in range(0, height):
for x in range(0, width):
plot_color = start_color.lerp(end_color, x/width)
background.set_at((x, y), plot_color)
return background
def is_trying_to_quit(event):
pressed_keys = pygame.key.get_pressed()
alt_pressed = pressed_keys[pygame.K_LALT] or pressed_keys[pygame.K_RALT]
x_button = event.type == pygame.QUIT
altF4 = alt_pressed and event.type == pygame.KEYDOWN and event.key == pygame.K_F4
escape = event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE
return x_button or altF4 or escape
def do_gauss_demo(surface, puff):
# Draw a rectangle
surface.blit(puff.puff, (puff.x, puff.y))
def run_demos(width, height, fps):
pygame.init()
screen = pygame.display.set_mode((width, height))
background = create_background(width, height)
clock = pygame.time.Clock()
the_world_is_a_happy_place = 0
puffs = []
locs = []
for i in range(0,100):
size = int(random.random()*200)
puffs.append(Puff(size, size, screen))
for i in range(0,100):
size = int(random.random()*20)
puffs.append(Puff(size, size, screen))
while True:
the_world_is_a_happy_place += 1
for event in pygame.event.get():
if is_trying_to_quit(event):
return
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
demos = demos[1:]
screen.blit(background, (0, 0))
for puff in puffs:
do_gauss_demo(screen, puff)
puff.move_pos()
pygame.display.flip()
clock.tick(fps)
run_demos(1200, 768, 60)