-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain2.py
110 lines (99 loc) · 3.61 KB
/
main2.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
# import the pygame module, so you can use it
import pygame
import os
SCREEN_WIDTH = 128
SCREEN_HEIGHT = 64
black = (0,0,0)
white = (255,255,255)
back = 'Back'
def init_screen():
pygame.init()
# create a surface on screen
screen = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))
screen.fill(black)
pygame.draw.rect(screen, white, (0,0,SCREEN_WIDTH,SCREEN_HEIGHT), 1) # screen border
pygame.display.update()
return screen
def get_menu_items(menu, mode):
if menu == 'HOME':
menu_items = ["LOAD SCHEDULE", "NEW SCHEDULE"]
if menu == "LOAD SCHEDULE":
menu_items = os.listdir('schedules')
menu_items.append(back)
if menu == 'DISPLAY SCHEDULE':
menu_items = ["TEMP,RATE,TIME"]
if menu == "NEW SCHEDULE":
pass
return (menu_items, mode)
def process_choice(menu, item):
if menu == 'HOME' and item == 'LOAD SCHEDULE':
new_menu = 'LOAD SCHEDULE'
if menu == "HOME" and item == 'NEW SCHEDULE':
new_menu = 'NEW SCHEDULE'
if menu == 'LOAD SCHEDULE' and item == back:
new_menu = 'HOME'
if menu == 'LOAD SCHEDULE' and item != back:
new_menu = 'DISPLAY SCHEDULE'
return new_menu
def read_schedule(schedule_file):
lines = []
with open(schedule_file, "r") as f:
for line in f:
lines.append(line.rstrip())
return lines
def display_selections(screen, selections, selected):
init_screen()
main_font = pygame.font.SysFont("menlo", 10)
font_height = main_font.get_height()
text_start = [2, 2]
max_lines = (SCREEN_HEIGHT - text_start[1])//font_height
print("max lines: " + str(max_lines))
text = []
for item in selections:
if selected == selections.index(item):
text.append(main_font.render(item, 1, black, white))
else:
text.append(main_font.render(item, 1, white, black))
if (selected + 1) > max_lines:
start_index = (selected + 1) - max_lines
max_index = max_lines
else:
start_index = 0
max_index = len(selections) - 1
if max_index + 1 > max_lines:
max_index = start_index + (max_lines - 1)
for txt in range(start_index,max_index+1):
screen.blit(text[txt], text_start)
text_start = [text_start[0], text_start[1] + font_height]
pygame.display.update()
def main():
running = True
screen = init_screen()
menu = "HOME"
selected = 0
# main loop
while running:
# event handling, gets all event from the eventqueue
for event in pygame.event.get():
# only do something if the event is of type QUIT
#print(mode, event)
(menu_items, mode) = get_menu_items(menu, mode)
if event.type == pygame.KEYDOWN and event.key == pygame.K_DOWN:
if selected < len(menu_items) - 1:
selected = selected + 1
if event.type == pygame.KEYDOWN and event.key == pygame.K_UP:
if selected > 0 :
selected = selected - 1
if event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN:
screen = init_screen()
menu = process_choice(menu, menu_items[selected])
selected = 0
display_selections(screen, menu_items, selected)
if (event.type == pygame.QUIT):
# change the value to False, to exit the main loop
running = False
# run the main function only if this module is executed as the main script
# (if you import this as a module then nothing is executed)
if __name__=="__main__":
# call the main function
main()