-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
126 lines (96 loc) · 3.97 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# TODO: Make window resizeable
# TODO: Adjust lamps' size and position to window's size
import sys
from datetime import datetime
import pygame
from pygame.locals import *
pygame.init()
clock = pygame.time.Clock()
WINDOW_SIZE = (500, 500)
FILL_COLOR_ACTIVE_HOUR = (255, 0, 0)
FILL_COLOR_ACTIVE_MINUTE = (0, 255, 0)
FILL_COLOR_ACTIVE_SECOND = (0, 0, 255)
FILL_COLOR_INACTIVE = (45, 45, 45)
DIGITAL_TIME_COLOR = (150, 150, 150)
OUTLINE_COLOR = (0, 0, 0)
CLOCK_LAMP_SIZE = 20
CLOCK_LAMP_OUTLINE_THICKNESS = 3
CLOCK_START_POS = (50, 0)
display = pygame.display.set_mode(WINDOW_SIZE)
width = display.get_width()
height = display.get_height()
def convertinttobinlist(part, length=0):
result = ""
if part == 0:
result = "0"
while part > 0:
result = str(part % 2) + result
part = part // 2
result = ("0"*length + result)[-length:]
return [int(char) for char in result]
def convertimetobinary(time):
binary_clock = []
for i, section in enumerate(time.strftime("%H:%M:%S").split(":")):
binary_clock_section = []
for j, part in enumerate(section):
# convert part to binary
# append part to our list
bin_part = convertinttobinlist(int(part), 5)
binary_clock.append(bin_part)
return binary_clock
def drawbinaryclock(display, showdigitalclock):
# draw filled circle then draw a circle outline OVER of the same size
now = datetime.now()
now_in_binary = convertimetobinary(now)
time_digit_list = now.strftime("%H%M%S")
font = pygame.font.SysFont(None, 48)
for x in range(6):
if showdigitalclock:
time_digit = font.render(time_digit_list[x], True, DIGITAL_TIME_COLOR)
display.blit(time_digit, ((CLOCK_START_POS[0] + x * CLOCK_LAMP_SIZE * 3) - time_digit.get_width()/2, (CLOCK_START_POS[1] + 5 * CLOCK_LAMP_SIZE * 3) - time_digit.get_height()/2))
if x < 2:
fill_color = FILL_COLOR_ACTIVE_HOUR
elif x < 4:
fill_color = FILL_COLOR_ACTIVE_MINUTE
else:
fill_color = FILL_COLOR_ACTIVE_SECOND
for y in range(4, 0, -1):
if now_in_binary[x][y]:
pygame.draw.circle(display, fill_color, (
CLOCK_START_POS[0] + x * CLOCK_LAMP_SIZE * 3, CLOCK_START_POS[1] + y * CLOCK_LAMP_SIZE * 3),
CLOCK_LAMP_SIZE)
else:
pygame.draw.circle(display, FILL_COLOR_INACTIVE, (
CLOCK_START_POS[0] + x * CLOCK_LAMP_SIZE * 3, CLOCK_START_POS[1] + y * CLOCK_LAMP_SIZE * 3),
CLOCK_LAMP_SIZE)
pygame.draw.circle(display, OUTLINE_COLOR, (
CLOCK_START_POS[0] + x * CLOCK_LAMP_SIZE * 3, CLOCK_START_POS[1] + y * CLOCK_LAMP_SIZE * 3),
CLOCK_LAMP_SIZE, CLOCK_LAMP_OUTLINE_THICKNESS)
# break first hour after two "lamps"
if x == 0 and y == 3:
break
if __name__ == "__main__":
font = pygame.font.SysFont("Arial", 20)
text = font.render("Show digital clock", True, DIGITAL_TIME_COLOR)
showdigitalclock = False
while True:
# event loop
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
if event.type == MOUSEBUTTONDOWN:
if 0 <= mouse[0] <= text.get_width() + 4 and 0 <= mouse[1] <= text.get_height() + 4:
showdigitalclock = not showdigitalclock
display.fill((90, 90, 90))
mouse = pygame.mouse.get_pos()
# draw show digital clock button
pygame.draw.rect(display, DIGITAL_TIME_COLOR, (0, 0, text.get_width() + 4, text.get_height() + 4), 1)
display.blit(text, (2, 2))
drawbinaryclock(display, showdigitalclock)
pygame.display.flip()
clock.tick(2)