|
import colorsys |
|
import contextlib |
|
import math |
|
import os |
|
import signal |
|
import sys |
|
import time |
|
|
|
|
|
with contextlib.redirect_stdout(None): |
|
import pygame |
|
|
|
|
|
print("""Display HAT Mini: Pygame Demo""") |
|
|
|
|
|
class PygameDemo: |
|
def __init__(self): |
|
os.putenv('SDL_FBDEV', '/dev/fb0') |
|
os.putenv('SDL_AUDIODRIVER', 'dsp') |
|
|
|
self.hue_to_rgb = tuple(map( |
|
lambda hue: colorsys.hsv_to_rgb(hue / 255.0, 1, 1), |
|
range(0, 255), |
|
)) |
|
|
|
pygame.display.init() |
|
pygame.mouse.set_visible(False) |
|
self.screen = pygame.display.set_mode((320, 240)) |
|
|
|
self.screen.fill((0, 0, 0)) |
|
pygame.display.update() |
|
|
|
self._running = False |
|
|
|
def _exit(self, sig, frame): |
|
self._running = False |
|
print("\nExiting!...\n") |
|
|
|
def __del__(self): |
|
"Destructor to make sure pygame shuts down, etc." |
|
|
|
def run(self): |
|
self._running = True |
|
signal.signal(signal.SIGINT, self._exit) |
|
|
|
while self._running: |
|
for event in pygame.event.get(): |
|
if any(( |
|
event.type == pygame.QUIT, |
|
event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE, |
|
)): |
|
self._running = False |
|
break |
|
|
|
self.screen.fill((0, 0, 0)) |
|
|
|
t = int(time.time() * 40) |
|
|
|
for x in range(21): |
|
for y in range(15): |
|
r, g, b = self.tunnel(x, y, t) |
|
r = min(255, int(r)) |
|
g = min(255, int(g)) |
|
b = min(255, int(b)) |
|
pygame.draw.circle( |
|
self.screen, |
|
(r, g, b), |
|
( |
|
(x * 15) + 7, |
|
(y * 15) + 6 + 7, |
|
), |
|
7, |
|
) |
|
|
|
pygame.display.update() |
|
|
|
pygame.quit() |
|
sys.exit(0) |
|
|
|
def tunnel(self, x, y, step): |
|
u_width = 32 |
|
u_height = 32 |
|
speed = step / 100.0 |
|
x -= (u_width / 2) |
|
y -= (u_height / 2) |
|
xo = math.sin(step / 27.0) * 2 |
|
yo = math.cos(step / 18.0) * 2 |
|
x += xo |
|
y += yo |
|
|
|
if y == 0: |
|
if x < 0: |
|
angle = -(math.pi / 2) |
|
else: |
|
angle = (math.pi / 2) |
|
else: |
|
angle = math.atan(x / y) |
|
|
|
if y > 0: |
|
angle += math.pi |
|
|
|
angle /= 2 * math.pi # convert angle to 0...1 range |
|
|
|
hyp = math.sqrt(math.pow(x, 2) + math.pow(y, 2)) |
|
|
|
shade = hyp / 2.1 |
|
shade = 1 if shade > 1 else shade |
|
|
|
angle += speed |
|
|
|
depth = speed + (hyp / 10) |
|
|
|
col1 = self.hue_to_rgb[step % 255] |
|
col1 = (col1[0] * 0.8, col1[1] * 0.8, col1[2] * 0.8) |
|
col2 = self.hue_to_rgb[step % 255] |
|
col2 = (col2[0] * 0.3, col2[1] * 0.3, col2[2] * 0.3) |
|
col = col1 if int(abs(angle * 6.0)) % 2 == 0 else col2 |
|
|
|
td = .3 if int(abs(depth * 3.0)) % 2 == 0 else 0 |
|
|
|
col = (col[0] + td, col[1] + td, col[2] + td) |
|
col = (col[0] * shade, col[1] * shade, col[2] * shade) |
|
|
|
return (col[0] * 255, col[1] * 255, col[2] * 255) |
|
|
|
PygameDemo().run() |