How to Create Analog Clock || Analog Clock Using Python
import pygame
from math import pi, cos, sin
import datetime
WIDTH, HEIGHT = 1120, 800
center = (WIDTH / 2, HEIGHT / 2)
clock_radius = 400
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Analog Clock")
clock = pygame.time.Clock()
FPS = 60
BLUE = (0, 0, 255)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
YELLOW = (255, 255, 0)
def numbers(number, size, position):
font = pygame.font.SysFont("Arial", size, True, False)
text = font.render(number, True, BLACK)
text_rect = text.get_rect(center=(position))
screen.blit(text, text_rect)
def polar_to_cartesian(r, theta):
x = r * sin(pi * theta / 180)
y = r * cos(pi * theta / 180)
return x + WIDTH / 2, -(y - HEIGHT / 2)
def main():
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
current_time = datetime.datetime.now()
second = current_time.second
minute = current_time.minute
hour = current_time.hour
name = 'Analog Clock With Date'
name1 = 'Sub My Channel'
name2 = 'Technical Rafi'
day = current_time.day
month = current_time.month
year = current_time.year
weekday = current_time.today().isoweekday()
calendar = current_time.today().isocalendar()
weekdays_abbr = {
1: "Mo",
2: "Tu",
3: "We",
4: "Th",
5: "Fr",
6: "Sa",
7: "Su"
}
weekday_abbr = weekdays_abbr.get(weekday)
months_abbr = {
1: "JAN",
2: "FEB",
3: "MAR",
4: "APR",
5: "MAY",
6: "JUN",
7: "JUL",
8: "AUG",
9: "SEP",
10: "OCT",
11: "NOV",
12: "DEC"
}
month_abbr = months_abbr.get(month)
if day < 10:
day = "0" + str(day)
screen.fill(WHITE)
pygame.draw.circle(screen, GREEN, center, clock_radius - 13, 13)
pygame.draw.circle(screen, RED, center, clock_radius - 10, 10)
pygame.draw.circle(screen, BLACK, center, 18)
pygame.draw.circle(screen, RED, center, 12)
pygame.draw.rect(screen, GREEN,
[WIDTH / 2 - 260, HEIGHT / 2 - 100, 80, 60], 5)
pygame.draw.rect(screen, GREEN,
[WIDTH / 2 - 180, HEIGHT / 2 - 100, 80, 60], 5)
pygame.draw.rect(screen, GREEN,
[WIDTH / 2 + 100, HEIGHT / 2 - 100, 80, 60], 5)
pygame.draw.rect(screen, GREEN,
[WIDTH / 2 + 180, HEIGHT / 2 - 100, 80, 60], 5)
pygame.draw.rect(screen, GREEN,
[WIDTH / 2 - 55, HEIGHT / 2 - 260 + 160, 110, 60], 5)
pygame.draw.rect(screen, YELLOW,
[WIDTH / 2 - 185, HEIGHT / 2 - 70 + 160, 370, 120], 5)
pygame.draw.rect(screen, RED,
[WIDTH / 2 - 270, HEIGHT / 2 - 130 + 160, 540, 60], 5)
pygame.draw.rect(screen, RED,
[WIDTH / 2 - 270, HEIGHT / 2 - 270 + 160, 540, 80], 5)
pygame.draw.rect(screen, YELLOW,
[WIDTH / 2 - 265, HEIGHT / 2 - 265 + 157, 530, 75], 5)
numbers(str(weekday_abbr), 40, (WIDTH / 2 - 220, HEIGHT / 2 - 70))
numbers(str(calendar[1]), 40, (WIDTH / 2 - 140, HEIGHT / 2 - 70))
numbers(str(month_abbr), 40, (WIDTH / 2 + 140, HEIGHT / 2 - 70))
numbers(str(day), 40, (WIDTH / 2 + 220, HEIGHT / 2 - 70))
numbers(str(year), 40, (WIDTH / 2, HEIGHT / 2 - 230 + 160))
numbers(str(name), 40, (WIDTH / 2, HEIGHT / 2 + 60))
numbers(str(name1), 40, (WIDTH / 2, HEIGHT / 2 + 120))
numbers(str(name2), 40, (WIDTH / 2, HEIGHT / 2 + 170))
for number in range(1, 13):
numbers(str(number), 80,
polar_to_cartesian(clock_radius - 80, number * 30))
for number in range(0, 360, 6):
if number % 5:
pygame.draw.line(screen, BLACK,
polar_to_cartesian(clock_radius - 15, number),
polar_to_cartesian(clock_radius - 30, number), 4)
else:
pygame.draw.line(screen, BLUE,
polar_to_cartesian(clock_radius - 15, number),
polar_to_cartesian(clock_radius - 35, number), 8)
# Hour
r = 250
theta = (hour + minute / 60 + second / 3600) * (360 / 12)
pygame.draw.line(screen, BLUE, center, polar_to_cartesian(r, theta), 14)
# Minute
r = 300
theta = (minute + second / 60) * (360 / 60)
pygame.draw.line(screen, BLACK, center, polar_to_cartesian(r, theta), 10)
# Second
r = 340
theta = second * (360 / 60)
pygame.draw.line(screen, RED, center, polar_to_cartesian(r, theta), 6)
pygame.display.update()
clock.tick(FPS)
pygame.quit()
main()
0 Comments