1
0
Fork 0
menu/screensavers.py

38 lines
1.2 KiB
Python

#!/usr/bin/env python3
from time import strftime
from typing import Tuple
import pygame
from iboard import IBoard
class Screensaver(IBoard):
def handle_event(self, _):
pass
class ClockScreensaver(Screensaver):
def __init__(self):
self.font = pygame.freetype.SysFont(name="Sans", size=40)
self.dx = 1
self.dy = 1
self.text_xy: Tuple[int, int] = (0, 0)
def draw(self, screen: pygame.Surface):
text = strftime("%H:%M:%S")
screen.fill("black")
pygame.Rect(0, 0, screen.get_width(), screen.get_height())
text_rect = self.font.get_rect(text)
if ((self.text_xy[0] + self.dx + text_rect.width) > screen.get_width()) or \
((self.text_xy[0] + self.dx) < 0):
self.dx *= -1
if ((self.text_xy[1] + self.dy + text_rect.height) > screen.get_height()) or \
((self.text_xy[1] + self.dy) < 0):
self.dy *= -1
self.text_xy = (self.text_xy[0] + self.dx, self.text_xy[1] + self.dy)
text_pos = pygame.Rect(self.text_xy[0], self.text_xy[1],
text_rect.width, text_rect.height)
self.font.render_to(screen, text_pos, text, fgcolor="pink")