-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcolorswirl.py
69 lines (53 loc) · 2.06 KB
/
colorswirl.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
import pygame
import numpy
W, H = 600, 300
FPS = 60
FLAGS = 0 # | pygame.SCALED
class State:
def __init__(self):
self.grid = numpy.random.randint(0, 0xFFFFFF + 1, (W, H), numpy.int32)
def step(self):
shifted = []
for dx in [-1, 0, 1]:
for dy in [-1, 0, 1]:
if (dx, dy) != (0, 0):
shifted.append(numpy.roll(self.grid, (dx, dy), (0, 1)))
rand_vals = numpy.random.randint(0, len(shifted), (W, H), numpy.int16)
masks = []
for i in range(len(shifted)):
masks.append(1 * (rand_vals == i))
self.grid[:] = 0
for i in range(len(shifted)):
self.grid += masks[i] * shifted[i]
def draw(self, screen):
pygame.surfarray.blit_array(screen, self.grid)
if __name__ == "__main__":
pygame.init()
screen = pygame.display.set_mode((W, H), flags=FLAGS)
state = State()
clock = pygame.time.Clock()
running = True
last_update_time = pygame.time.get_ticks()
while running:
current_time = pygame.time.get_ticks()
dt = (current_time - last_update_time) / 1000
for e in pygame.event.get():
if e.type == pygame.QUIT or (e.type == pygame.KEYDOWN and e.key == pygame.K_ESCAPE):
running = False
elif e.type == pygame.KEYDOWN:
if e.key == pygame.K_r:
print("Resetting.")
state = State()
elif e.key == pygame.K_RIGHT:
FPS += 10
print("Increased target FPS to: {}".format(FPS))
elif e.key == pygame.K_LEFT:
FPS = max(10, FPS - 10)
print("Decreased target FPS to: {}".format(FPS))
state.step()
state.draw(screen)
pygame.display.flip()
if current_time // 1000 > last_update_time // 1000:
pygame.display.set_caption("Color Swirl (FPS={:.1f}, TARGET_FPS={}, SIZE={})".format(clock.get_fps(), FPS, (W, H)))
last_update_time = current_time
clock.tick(FPS)