analoguhr/uhr.py

58 lines
1.7 KiB
Python
Raw Permalink Normal View History

2024-05-22 12:20:38 +02:00
import pygame
import datetime
import math
def draw_number_hand(screen, value, max_value, center, length, thickness, color, rep):
angle = value / max_value * 360
angle_rad = math.radians(angle - 90) # Adjust by 90 degrees to align with clock
# Draw the hand as repeated numbers
for i in range(1, rep): # number of repetitions
offset = length / rep * i
x = center[0] + offset * math.cos(angle_rad)
y = center[1] + offset * math.sin(angle_rad)
rendered_text = font.render(str(value), True, color)
text_rect = rendered_text.get_rect(center=(x, y))
screen.blit(rendered_text, text_rect)
# Definitions
size = 500
center = (size/2, size/2)
hour_hand_length = 200
minute_hand_length = 250
second_hand_length = 270
hand_thickness = 3
# Initialize Pygame and Window
pygame.init()
screen = pygame.display.set_mode([size, size])
pygame.display.set_caption("Analog Clock")
font = pygame.font.Font(None, 36)
# Run until the user asks to quit
running = True
while running:
# background
screen.fill((255, 255, 255))
# Get current time
now = datetime.datetime.now()
# Draw the clock hands
draw_number_hand(screen, now.second, 60, center, second_hand_length, hand_thickness, (0, 0, 255),8)
draw_number_hand(screen, now.minute, 60, center, minute_hand_length, hand_thickness, (0, 255, 0),8)
draw_number_hand(screen, now.hour, 12, center, hour_hand_length, hand_thickness, (255, 0, 0), 6)
# Update the display
pygame.display.flip()
# Check for the quit event
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# 1 FPS
pygame.time.Clock().tick(60)
# Quit
pygame.quit()