-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgrid_ui.py
More file actions
138 lines (106 loc) · 3.57 KB
/
grid_ui.py
File metadata and controls
138 lines (106 loc) · 3.57 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
import pygame
import datetime
import pickle
from dateutil.relativedelta import relativedelta
import sys
import utils
WINDOW_SIZE = [1400, 255]
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
WIDTH = 20
HEIGHT = 20
ROWS = 7
COLS = 53
MARGIN = 5
BTN_BG = (40, 40, 40)
BTN_FG = (200, 200, 200)
pygame.init()
def grid_creator(year, old=False):
grid = []
if old:
try:
with open(f'{year}.pkl', 'rb') as f:
grid = pickle.load(f)
return grid
except FileNotFoundError:
pass
for row in range(COLS):
grid.append([])
for column in range(ROWS):
grid[row].append(False)
fday = datetime.datetime(year, 1, 1).weekday()
lday = datetime.datetime(year, 12, 31).weekday()
if(fday!=6):
for i in range(fday+1):
grid[0][i] = None
if(lday!=5):
lday = -1 if lday == 6 else lday
for i in range(lday+2, 7):
grid[-1][i] = None
return grid
def grid_screen_init(year):
screen = pygame.display.set_mode(WINDOW_SIZE)
done = False
clock = pygame.time.Clock()
grid = grid_creator(year)
old_button = utils.button(BTN_BG, BTN_FG, 400, 195, 70, 40, ' Old ')
save_button = utils.button(BTN_BG, BTN_FG, 570, 195, 220, 40, ' Save And Exit ')
start_button = utils.button(BTN_BG, BTN_FG, 900, 195, 100, 40, ' Start ')
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
if old_button.isOver(pos):
grid = grid_creator(year, old=True)
if save_button.isOver(pos):
with open(f'{year}.pkl', 'wb') as f:
pickle.dump(grid, f)
exit()
if start_button.isOver(pos):
return grid
if pos[0]<(MARGIN + WIDTH) * 53 and pos[1]<(MARGIN + HEIGHT) * 7:
column = pos[0] // (WIDTH + MARGIN)
row = pos[1] // (HEIGHT + MARGIN)
if(grid[column][row]!=None):
grid[column][row] = not grid[column][row]
screen.fill(BLACK)
for row in range(COLS):
for column in range(ROWS):
color = WHITE
if grid[row][column] == True:
color = GREEN
if grid[row][column] == None:
color = BLACK
pygame.draw.rect(screen,
color,
[(MARGIN + WIDTH) * row + MARGIN,
(MARGIN + HEIGHT) * column + MARGIN,
WIDTH,
HEIGHT])
old_button.draw(screen)
save_button.draw(screen)
start_button.draw(screen)
clock.tick(60)
pygame.display.flip()
pygame.quit()
return []
def get_dates(year):
grid = grid_screen_init(year)
if len(grid) == 0:
exit()
fday = datetime.datetime(year, 1, 1).weekday()
fday = -1 if fday == 6 else fday
dates_list=[]
for i in range(COLS):
for j in range(ROWS):
if grid[i][j]==True:
date = datetime.date(year, 1, 1) + relativedelta(weeks=+i, days=+(j-fday-1))
dates_list.append(date)
return dates_list
if __name__=="__main__":
year = 2019
print(get_dates(year))