IshaanGarud

Analog Clock in Python using Pygame

Jun 6th, 2022
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.49 KB | None | 0 0
  1. '''AnalogClock-in-Python/main.py
  2. @IshaanGarud
  3. IshaanGarud Main.py
  4. 1 contributor
  5. 117 lines (92 sloc)  5.09 KB'''
  6.  
  7. import pygame, sys, math, datetime, random
  8.  
  9. pygame.init()
  10.  
  11. WIDTH, HEIGHT, FPS = 690, 800, 60
  12. AVG = min(WIDTH, HEIGHT)
  13.  
  14. screen = pygame.display.set_mode((WIDTH, HEIGHT))
  15. screen_rect = screen.get_rect()
  16.  
  17. clock = pygame.time.Clock()
  18.  
  19. COLOURS = {"white": (250, 250, 250),
  20.            "black": (0, 0, 0),
  21.            "gray": (25, 25, 25),
  22.            "l_gray": (150, 150, 150),
  23.            "bg": (25, 25, 75),
  24.            "red": (215, 65, 90),
  25.            "green": (65, 215, 90),
  26.            "d_green": (50, 190, 100),
  27.            "blue": (100, 100, 255)}
  28.  
  29. myfont = pygame.font.SysFont("Arial", 20)
  30. facefont = pygame.font.SysFont("Times New Roman", 35, 1)
  31. datefont = [pygame.font.SysFont("msgothic", 30, 1, 1), pygame.font.SysFont("msgothic", 30, 1, 1)]
  32.  
  33. sec_angle = 0
  34. min_angle = 0
  35. hour_angle = 0
  36. Meridian = None
  37. column = 0
  38.  
  39. heading = random.choice(("Made By Ishaan Garud", "Sigma!",
  40.                          "Deez Nuts!", "Ever Heard of Joe?",
  41.                          "Wanna know what ma balls taste like?",
  42.                          "I don't need bitches, I need python!"))
  43.    
  44.  
  45. class Hands:
  46.     def __init__(self, angle, r, colour):
  47.         self.angle = angle
  48.         self.r = round((AVG*r)/690)   # Multiplying by round((WIDTH*r)/690) adjusts the length of all Hands , no matter the width of the window
  49.         self.colour = colour
  50.         self.width = 3
  51.  
  52.     def move(self):
  53.         self.x = (math.cos(math.radians(self.angle - 90)) * self.r + screen_rect.w // 2)
  54.         self.y = (math.sin(math.radians(self.angle - 90)) * self.r + screen_rect.h // 2)
  55.  
  56.     def draw(self):
  57.         self.hand = pygame.draw.line(screen, self.colour, screen_rect.center, (self.x, self.y), self.width)
  58.  
  59.  
  60. while True:
  61.     screen.fill(COLOURS["bg"])
  62.     frame = pygame.draw.circle(screen, COLOURS["white"], screen_rect.center, round((AVG*350)/690), 3)
  63.  
  64.     for i in range(1, 13):                  # Controls the Numbers on Clock face
  65.         ix = math.cos(math.radians((i*30)-90))*round((AVG*325)/690) + screen_rect.w//2    # Multiplying by round((AVG*325)/690) adjusts the location of numbers , no matter the width of the window
  66.         iy = math.sin(math.radians((i*30)-90))*round((AVG*325)/690) + screen_rect.h//2
  67.         num = facefont.render(str(i), 1, (255, 255, 255))
  68.         screen.blit(num, [ix-10, iy-17])
  69.  
  70.     current_time = datetime.datetime.now()
  71.     sec_angle = current_time.second * (360 / 60)  # 1sec is divided in 60 parts
  72.     min_angle = current_time.minute * (360 / 60)  # 1min is divided in 60 parts
  73.     hour_angle = current_time.hour * (360 / 12)  # 1hr is divided in 12 parts
  74.  
  75.     if (current_time.hour - 12) < 0:    #Decides the Time of Day i.e. AM or PM
  76.         Meridian = "AM"
  77.     else:
  78.         Meridian = "PM"
  79.  
  80.     face_digital = {"weekday": current_time.strftime("%a"),
  81.                     "month": current_time.strftime("%b"),
  82.                     "date": current_time.strftime("%d"),
  83.                     "year": current_time.strftime("%Y")}
  84.  
  85.     # This one's a one-liner
  86.     dateObj = screen.blit(datefont[1].render(face_digital["date"], 1, COLOURS["white"]), [screen_rect.w*(4/5), screen_rect.h//2-datefont[1].size(face_digital["date"])[0]//3])    # Displays the date on the screen (don't question why its so big, I wanted  to fit everything in a line)
  87.     weekdayObj = screen.blit(datefont[0].render(face_digital["weekday"], 1, COLOURS["white"]), [screen_rect.w*(1/5), screen_rect.h//2-datefont[0].size(face_digital["weekday"])[0]])    # Displays the weekday on the screen.
  88.     monthObj = screen.blit(datefont[0].render(face_digital["month"], 1, COLOURS["white"]), [screen_rect.w*(1/5), screen_rect.h//(1.9)])    # Displays the month on the screen.
  89.     yearObj = screen.blit(datefont[1].render(face_digital["year"], 1, COLOURS["white"]), [screen_rect.w//2-datefont[1].size(face_digital["year"])[0]//2, screen_rect.h//2+datefont[1].size(face_digital["year"])[0]*2])   # Displays the year on the screen.
  90.  
  91.     elems = [dateObj, weekdayObj, monthObj, yearObj]
  92.     for elem in elems:
  93.         elem.w += 1
  94.         elem.h += 1
  95.         elem.x -= 3
  96.         elem.y -= 1
  97.         pygame.draw.rect(screen, COLOURS["white"], elem, 2)
  98.  
  99.     sec_hand = Hands(sec_angle, 325, COLOURS["red"])
  100.     min_hand = Hands(min_angle, 300, COLOURS["white"])
  101.     hour_hand = Hands(hour_angle, 200, COLOURS["l_gray"])
  102.     hour_hand.width = 8
  103.  
  104.     sec_hand.move(), sec_hand.draw()
  105.     min_hand.move(), min_hand.draw()
  106.     hour_hand.move(), hour_hand.draw()
  107.  
  108.     time24 = str(current_time.hour) + ": " + str(current_time.minute) + ": " + str(current_time.second)
  109.     time12 = str(abs(current_time.hour - 12)) + ": " + str(current_time.minute) + ": " + str(current_time.second) + Meridian
  110.     digi_time24 = myfont.render(time24, 1, (255, 255, 255))
  111.     digi_time12 = myfont.render(time12, 1, (255, 255, 255))
  112.  
  113.     screen.blit(digi_time24, [0, 0])
  114.     screen.blit(digi_time12, [0, 25])
  115.     pygame.draw.line(screen, COLOURS["white"], (0, 22), (100, 22))
  116.  
  117.     # I hated to use too many lines so I just wrote 3 lines in one :)
  118.     screen.blit(pygame.font.SysFont("Arial", 15).render(heading, 1, COLOURS["white"]), [WIDTH - pygame.font.SysFont("Arial", 15).size(heading)[0], 0])
  119.  
  120.     keys = pygame.key.get_pressed()
  121.     for ev in pygame.event.get():
  122.         if ev.type == pygame.QUIT or keys[pygame.K_ESCAPE]:
  123.             pygame.quit()
  124.             sys.exit()
  125.            
  126.     pygame.display.update()
  127.     clock.tick(FPS)
  128.  
Add Comment
Please, Sign In to add comment