snakeware

Python-based Linux distro for x86-64 and RPi
git clone git://squid-tech.com/snakeware.git
Log | Files | Refs | README

commit 84fa4b981688187bd953781ff5c5d5be7f5ba2ab
parent 805e533092eb3ad747a7671f2e4ecbd0f83813fb
Author: Josh Moore <jxm5210@g.rit.edu>
Date:   Sun,  7 Jun 2020 16:23:13 -0400

Merge pull request #86 from mdoege/dazzler

Add kaleidoscope
Diffstat:
Asnakewm/apps/fun/dazzler/README.md | 12++++++++++++
Asnakewm/apps/fun/dazzler/__init__.py | 14++++++++++++++
Asnakewm/apps/fun/dazzler/dazzler.py | 96+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 122 insertions(+), 0 deletions(-)

diff --git a/snakewm/apps/fun/dazzler/README.md b/snakewm/apps/fun/dazzler/README.md @@ -0,0 +1,12 @@ +# Snazzler +Snazzler is a kaleidoscope similar to the one for the Cromemco Dazzler (1976). + +The window is resizable, press Space to pause, "." to advance one frame. + +# Author + +Martin C. Doege + ++ github: https://github.com/mdoege + ++ date: 7 Jun 2020 diff --git a/snakewm/apps/fun/dazzler/__init__.py b/snakewm/apps/fun/dazzler/__init__.py @@ -0,0 +1,14 @@ +from .dazzler import Snazzler + + +def load(manager, params): + """ + Create and launch a new instance of Snazzler. + """ + # default position + pos = (100, 100) + + if params is not None and len(params) > 0: + pos = params[0] + + Snazzler(pos, manager) diff --git a/snakewm/apps/fun/dazzler/dazzler.py b/snakewm/apps/fun/dazzler/dazzler.py @@ -0,0 +1,96 @@ +# The Dazzler + +import pygame +import pygame_gui +from pygame_gui.elements.ui_image import UIImage + +from math import sin, cos, pi +import random, time + +RES = 64 +RES2 = RES / 2 +SRES = 640 +FPS = 15 + + +class Snazzler(pygame_gui.elements.UIWindow): + DIMS = SRES, int(0.75 * SRES) + + def __init__(self, pos, manager): + super().__init__( + pygame.Rect(pos, (self.DIMS[0] + 32, self.DIMS[1] + 60)), + manager=manager, + window_display_title="dazzler", + object_id="#dazzler", + resizable=True, + ) + + self.dsurf = UIImage( + pygame.Rect((0, 0), self.DIMS), + pygame.Surface(self.DIMS).convert(), + manager=manager, + container=self, + parent_element=self, + ) + self.dazz = pygame.Surface((RES, RES)) + self.pow = 1 + self.lastpow = 0 + self.lastframe = 0 + self.paused = False + self.step = False + self.manager = manager + + def process_event(self, event): + super().process_event(event) + r = super().get_abs_rect() + if event.type == pygame.MOUSEBUTTONUP and ( + r.w != self.DIMS[0] + 32 or r.h != self.DIMS[1] + 60 + ): + self.DIMS = r.w - 32, r.h - 60 + super().kill() + self.__init__((r.left, r.top), self.manager) + if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE: + self.paused = not self.paused + if event.type == pygame.KEYDOWN and event.key == pygame.K_PERIOD: + self.step = True + + def draw(self, x, y, c): + pygame.draw.line(self.dazz, c, (RES2 + x, RES2 + y), (RES2 + x, RES2 + y)) + pygame.draw.line(self.dazz, c, (RES2 - x, RES2 + y), (RES2 - x, RES2 + y)) + pygame.draw.line(self.dazz, c, (RES2 + x, RES2 - y), (RES2 + x, RES2 - y)) + pygame.draw.line(self.dazz, c, (RES2 - x, RES2 - y), (RES2 - x, RES2 - y)) + + def update(self, delta): + super().update(delta) + if (self.paused and not self.step) or (time.time() - self.lastframe < 1 / FPS): + return + self.step = False + self.lastframe = time.time() + c = ( + 85 * random.randint(0, 3), + 85 * random.randint(0, 3), + 85 * random.randint(0, 3), + ) + if random.random() < random.uniform(0.2, 1): + c = 0, 0, 0 + if random.random() < random.uniform(0.01, 0.03): + c = 255, 255, 255 + amp = random.gauss(0, 0.1) + time.time() % 29 + phi = random.gauss(0, 0.1) + time.time() % 11 + off = random.gauss(0, 0.3) * sin(time.time() / 23) + if time.time() - self.lastpow > 19: + self.pow = random.randint(1, 3) + self.lastpow = time.time() + for t in range(0, 360, random.randint(1, 3)): + x = amp * (sin(phi * t * pi / 180) + off) ** self.pow + y = amp * (cos(phi * t * pi / 180) + off) ** self.pow + self.draw(x, y, c) + x = 2 * x + y = 2 * y + self.draw(x, y, c) + x = 1.5 * x + y = 1.5 * y + self.draw(x, y, c) + + out = pygame.transform.scale(self.dazz, (self.DIMS)) + self.dsurf.image.blit(out, (0, 0))