-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.py
More file actions
161 lines (134 loc) · 6.32 KB
/
helpers.py
File metadata and controls
161 lines (134 loc) · 6.32 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# -*- coding: utf-8 -*-
"""
Created on Fri Apr 17 12:23:22 2020
@author: Patrick
"""
#HELPERS
import xlrd
import sys
import os
import pygame
pygame.init()
main = os.getcwd()
_image_library = {}
def get_image(path,transformx=None,transformy=None,reload=None):
global _image_library
try:
image,image_rect = _image_library.get(path)
except TypeError:
image,image_rect = None,None
if image == None or reload == True:
canonicalized_path = path.replace('/', os.sep).replace('\\', os.sep)
image = pygame.image.load(canonicalized_path)
if (transformx,transformy) != (None,None):
image = pygame.transform.scale(image,(transformx,transformy))
image_rect = image.get_rect()
image.set_colorkey((255,255,255))
_image_library[path] = (image,image_rect)
return image,image_rect
def get_vocab():
d = []
fname = main + '\\vocab\\germanvocab.xlsx'
xl_workbook = xlrd.open_workbook(fname)
# grab the first sheet by index
# (sheets are zero-indexed)
xl_sheet = xl_workbook.sheet_by_index(0)
#iterating through rows and columns
num_cols = xl_sheet.ncols # Number of columns
for row_idx in range(1, xl_sheet.nrows): # Iterate through rows, 0th is title
col=0
row_info = {'English':None,'German':None,'Type':None,'Difficulty':None}
for col_idx in range(0, num_cols): # Iterate through columns
cell_obj = xl_sheet.cell(row_idx, col_idx) # Get cell object by row, col
if col_idx == 0: row_info['German'] = cell_obj.value
if col_idx == 1: row_info['English'] = cell_obj.value
if col_idx == 2: row_info['Type'] = cell_obj.value
if col_idx == 3: row_info['Difficulty'] = cell_obj.value
#Fix entry syntax
row_info = {k.strip(): v.strip() for k, v in row_info.items()}
row_info = {k: v.split(',') for k, v in row_info.items()}
row_info = {k.strip(): [v[i].strip() for i in range(len(v))] for k, v in row_info.items()}
d.append(row_info)
return d
def get_gender_vocab():
d = []
fname = main + '\\vocab\\germangenders.xlsx'
xl_workbook = xlrd.open_workbook(fname)
# grab the first sheet by index
# (sheets are zero-indexed)
xl_sheet = xl_workbook.sheet_by_index(0)
#iterating through rows and columns
num_cols = xl_sheet.ncols # Number of columns
for row_idx in range(1, xl_sheet.nrows): # Iterate through rows, 0th is title
col=0
row_info = {'GermanNoun':None,'Gender':None}
for col_idx in range(0, num_cols): # Iterate through columns
cell_obj = xl_sheet.cell(row_idx, col_idx) # Get cell object by row, col
if col_idx == 0: row_info['GermanNoun'] = cell_obj.value
if col_idx == 1: row_info['Gender'] = cell_obj.value
#Fix entry syntax
row_info = {k.strip(): v.strip() for k, v in row_info.items()}
row_info = {k: v.split(',') for k, v in row_info.items()}
row_info = {k.strip(): [v[i].strip() for i in range(len(v))] for k, v in row_info.items()}
d.append(row_info)
return d
def get_plural_vocab():
d = []
fname = main + '\\vocab\\germanplurals.xlsx'
xl_workbook = xlrd.open_workbook(fname)
# grab the first sheet by index
# (sheets are zero-indexed)
xl_sheet = xl_workbook.sheet_by_index(0)
#iterating through rows and columns
num_cols = xl_sheet.ncols # Number of columns
for row_idx in range(1, xl_sheet.nrows): # Iterate through rows, 0th is title
col=0
row_info = {'GermanNoun':None,'Plural':None}
for col_idx in range(0, num_cols): # Iterate through columns
cell_obj = xl_sheet.cell(row_idx, col_idx) # Get cell object by row, col
if col_idx == 0: row_info['GermanNoun'] = cell_obj.value
if col_idx == 1: row_info['Plural'] = cell_obj.value
#Fix entry syntax
row_info = {k.strip(): v.strip() for k, v in row_info.items()}
row_info = {k: v.split(',') for k, v in row_info.items()}
row_info = {k.strip(): [v[i].strip() for i in range(len(v))] for k, v in row_info.items()}
d.append(row_info)
return d
def save_diff(diff):
with open(main + '\\states\\difficulty.txt','w') as f:
for k,v in diff.items():
if v:
f.write(k)
def save_word_choice(choice):
with open(main + '\\states\\word_choice.txt','w') as f:
for k,v in choice.items():
if v:
f.write(k)
def save_player(choice):
with open(main + '\\states\\user.txt','w') as f:
for k,v in choice.items():
if v:
f.write(k)
def dborder_state_definer(border_state,sprite):
border_state = dict.fromkeys(border_state, 0)
if sprite.path == main+'\\sprites\\easy_arrow.png': border_state['E'] = 1
elif sprite.path == main+'\\sprites\\medium_arrow.png': border_state['M'] = 1
elif sprite.path == main+'\\sprites\\hard_arrow.png': border_state['H'] = 1
save_diff(border_state)
return border_state
def wborder_state_definer(border_state,sprite):
border_state = dict.fromkeys(border_state, 0)
if sprite.path == main+'\\sprites\\verbs_arrow.png': border_state['V'] = 1
elif sprite.path == main+'\\sprites\\adjectives_arrow.png':border_state['A'] = 1
elif sprite.path == main+'\\sprites\\nouns_arrow.png': border_state['N'] = 1
elif sprite.path == main+'\\sprites\\plurals_arrow.png': border_state['P'] = 1
elif sprite.path == main+'\\sprites\\genders_arrow.png': border_state['G'] = 1
elif sprite.path == main+'\\sprites\\mixed_arrow.png': border_state['M'] = 1
save_word_choice(border_state)
return border_state
def uborder_state_definer(border_state,sprite):
border_state = dict.fromkeys(border_state, 0)
if sprite.path == main+'\\sprites\\player1.png': border_state['player1'] = 1
elif sprite.path == main+'\\sprites\\player2.png': border_state['player2'] = 1
save_player(border_state)
return border_state