Last active
February 13, 2024 10:26
-
-
Save fabiogaluppo/9f1d0b2c1a31bc9f66eb58f391ca48dc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Fabio Galuppo - http://member.acm.org/~fabiogaluppo - [email protected] | |
#1st version - Mar 2021 | |
#2nd version - Feb 2024 | |
import pygame | |
import os | |
import math | |
from datetime import datetime | |
def wallclock(): | |
def rotate(point, angle, center): | |
""" compute rotation in degree """ | |
angle = math.radians(angle) | |
s, c = math.sin(angle), math.cos(angle) | |
cx, cy = center | |
px, py = point | |
px, py = px - cx, py - cy | |
x = c * px - s * py | |
y = s * px + c * py | |
return int(x + cx), int(y + cy) | |
screen_size = width, height = 300, 300 | |
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (width, height) | |
pygame.init() | |
screen = pygame.display.set_mode(screen_size) | |
pygame.display.set_caption('Clock') | |
clock = pygame.time.Clock() | |
black = (0, 0, 0) | |
red = (255, 0, 0) | |
yellow = (255, 255, 0) | |
green = (0, 255, 0) | |
purple = (128, 0, 128) | |
while 1: | |
if pygame.event.peek(): | |
ev = pygame.event.wait() | |
if ev.type == pygame.QUIT: | |
break | |
if ev.type == pygame.KEYDOWN and ev.key == pygame.K_ESCAPE: | |
break | |
now = datetime.now() | |
h, m, s, ms = now.hour, now.minute, now.second, now.microsecond / 1000 | |
pygame.display.set_caption('Clock {:2d}:{:02d}:{:02d}'.format(h % 12, m, s)) | |
#start-point | |
center = (150, 150) | |
#end-points | |
hourHand, minuteHand, secondHand = (150, 85), (150, 50), (150, 50) | |
#compute hand angles | |
#smooth | |
hourHand = rotate(hourHand, ((h % 12) + m * 0.016667) * 30, center) | |
minuteHand = rotate(minuteHand, (m + s * 0.016667) * 6, center) | |
secondHand = rotate(secondHand, (s + ms * 0.001) * 6, center) | |
#jump | |
#hourHand = rotate(hourHand, (h % 12) * 30, center) | |
#minuteHand = rotate(minuteHand, m * 6, center) | |
#secondHand = rotate(secondHand, s * 6, center) | |
#clear screen | |
screen.fill(black) | |
#drawing order is important! | |
pygame.draw.line(screen, red, center, minuteHand, 7) | |
pygame.draw.line(screen, green, center, hourHand, 7) | |
pygame.draw.line(screen, yellow, center, secondHand, 3) | |
pygame.draw.circle(screen, purple, center, 104, 11) | |
pygame.draw.circle(screen, purple, center, 7) | |
pygame.display.flip() | |
clock.tick(15) | |
pygame.quit() | |
if __name__ == "__main__": | |
wallclock() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment