snakeware

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

commit 985f41356c3f4b1e436f859cc83e45916a88dd9c
parent cb7cdf122bf9b2a5e92a57282547224bda3b7ddb
Author: Josh Moore <jxm5210@g.rit.edu>
Date:   Sat, 13 Jun 2020 17:38:33 -0400

Merge pull request #119 from mdoege/berlin

Add Berlin clock
Diffstat:
Asnakewm/apps/fun/berlin/README.md | 17+++++++++++++++++
Asnakewm/apps/fun/berlin/__init__.py | 14++++++++++++++
Asnakewm/apps/fun/berlin/berlin.py | 132+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 163 insertions(+), 0 deletions(-)

diff --git a/snakewm/apps/fun/berlin/README.md b/snakewm/apps/fun/berlin/README.md @@ -0,0 +1,17 @@ +# Berlin + +The is a PyGame version of the [Berlin clock](https://en.wikipedia.org/wiki/Berlin_Clock). + +How to read: + +* hour = 5 * lights in first row + lights in second row +* minute = 5 * lights in third row + lights in fourth row +* Seconds are even when top light is lit. + +# Author + +Martin C. Doege + ++ github: https://github.com/mdoege + ++ date: 13 Jun 2020 diff --git a/snakewm/apps/fun/berlin/__init__.py b/snakewm/apps/fun/berlin/__init__.py @@ -0,0 +1,14 @@ +from .berlin import Berlin + + +def load(manager, params): + """ + Create and launch a new instance of Berlin. + """ + # default position + pos = (100, 100) + + if params is not None and len(params) > 0: + pos = params[0] + + Berlin(pos, manager) diff --git a/snakewm/apps/fun/berlin/berlin.py b/snakewm/apps/fun/berlin/berlin.py @@ -0,0 +1,132 @@ +#!/usr/bin/env python + +# Displays the Berlin clock +# 2020-06-13 + +import pygame +import pygame_gui +from pygame_gui.elements.ui_image import UIImage + +import sys, time, datetime + +ORANGE = 204, 100, 0 +YELLOW = 249, 244, 46 +BACKGROUND = 33, 41, 46 +GRAY = 76, 80, 82 +BORDER = 1 +RES = 220, 250 +inter = 0.1 + + +def mainprog(win, res): + box4, box11, boxy = res[0] // 4, res[0] // 11, res[1] // 5 + + now = datetime.datetime.now() + for x in range(4): + h1, h2 = divmod(now.hour, 5) + mm = now.minute % 5 + if x < h1: + c1 = ORANGE + else: + c1 = GRAY + if x < h2: + c2 = ORANGE + else: + c2 = GRAY + if x < mm: + c3 = YELLOW + else: + c3 = GRAY + pygame.draw.rect( + win, + c1, + [x * box4 + BORDER, boxy + BORDER, box4 - 2 * BORDER, boxy - 2 * BORDER], + ) + pygame.draw.rect( + win, + c2, + [ + x * box4 + BORDER, + 2 * boxy + BORDER, + box4 - 2 * BORDER, + boxy - 2 * BORDER, + ], + ) + pygame.draw.rect( + win, + c3, + [ + x * box4 + BORDER, + 4 * boxy + BORDER, + box4 - 2 * BORDER, + boxy - 2 * BORDER, + ], + ) + for x in range(11): + m5 = now.minute // 5 + if x < m5: + if (x + 1) % 3 == 0: + c = ORANGE + else: + c = YELLOW + else: + c = GRAY + pygame.draw.rect( + win, + c, + [ + x * box11 + BORDER, + 3 * boxy + BORDER, + box11 - 2 * BORDER, + boxy - 2 * BORDER, + ], + ) + if now.second % 2 == 0: + c = YELLOW + else: + c = GRAY + pygame.draw.circle(win, c, (2 * box4, boxy // 2), boxy // 2 - BORDER) + + +class Berlin(pygame_gui.elements.UIWindow): + res = RES + + def __init__(self, pos, manager): + super().__init__( + pygame.Rect(pos, (self.res[0] + 32, self.res[1] + 60)), + manager=manager, + window_display_title="berlin", + object_id="#berlin", + resizable=True, + ) + + self.dsurf = UIImage( + pygame.Rect((0, 0), self.res), + pygame.Surface(self.res).convert(), + manager=manager, + container=self, + parent_element=self, + ) + self.screen = pygame.Surface(self.res) + self.screen.fill(BACKGROUND) + self.manager = manager + self.last = 0 + + def process_event(self, event): + super().process_event(event) + r = super().get_abs_rect() + if event.type == pygame.MOUSEBUTTONUP and ( + r.w != self.res[0] + 32 or r.h != self.res[1] + 60 + ): + self.res = r.w - 32, r.h - 60 + super().kill() + self.__init__((r.left, r.top), self.manager) + + def update(self, delta): + super().update(delta) + if time.time() - self.last < inter: + return + self.last = time.time() + self.screen.fill(BACKGROUND) + mainprog(self.screen, self.res) + self.dsurf.image.blit(self.screen, (0, 0))