some changes in strikethrough lines
[tibetan_culture:learn_tibetan.git] / src / learn_tibetan.py
1 #    Copyright (C) 2015 mUniKeS
2 #
3 #    This program is free software: you can redistribute it and/or modify
4 #    it under the terms of the GNU General Public License as published by
5 #    the Free Software Foundation, either version 3 of the License, or
6 #    (at your option) any later version.
7 #
8 #    This program is distributed in the hope that it will be useful,
9 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
10 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 #    GNU General Public License for more details.
12 #
13 #    You should have received a copy of the GNU General Public License
14 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 import os
17 from random import shuffle
18 from unicodedata import lookup
19 from Tkinter import Tk, Button, PhotoImage, Label, Frame
20 import pygame
21
22 TITLE = "Learn Tibetan"
23 WINDOW_DIMENSIONS = "185x260+100+80"
24 SOUNDS_PATH = os.path.join(os.path.dirname(__file__), "../sounds/")
25 IMAGES_PATH = os.path.join(os.path.dirname(__file__), "../images/")
26
27 LETTERS = {lookup("tibetan letter ka"):("ka", SOUNDS_PATH + "ka.ogg"),
28           lookup("tibetan letter kha"):("k'a", SOUNDS_PATH + "kha.ogg"),
29           lookup("tibetan letter ga"):("k_'a", SOUNDS_PATH + "kh_a.ogg"),
30           lookup("tibetan letter nga"):("nga", SOUNDS_PATH + "nga.ogg")}
31
32 NUM_LETTERS = 4
33
34
35 def shuffle_dictionary (dictionary):
36     """
37     Return random keys of dictionary
38     """
39     # Python 3
40     # keys =  list(dictionary.keys())
41     # Python 2
42     keys = dictionary.keys()
43     shuffle(keys)
44     return keys
45
46
47 def generate_letters(list_letters, NUM_LETTERS):
48     """
49     Return a right letter and a list of shuffle letters
50     """
51     right_letter = list_letters[0]
52     letters = list_letters[0:NUM_LETTERS]
53     shuffle(letters)
54     return right_letter, letters
55
56
57 def play_ogg(list_oggs):
58     """
59     Play a list of oggs
60     """
61     i=0
62     SONG_END = pygame.USEREVENT + 1
63     pygame.mixer.music.set_endevent(SONG_END)
64     pygame.mixer.music.load(list_oggs[i])
65     pygame.mixer.music.play()
66
67     running = True
68     while running:
69         for event in pygame.event.get():
70             if  i >= len(list_oggs) - 1:
71                 running = False
72             elif event.type == SONG_END:
73                 pygame.mixer.music.load(list_oggs[i+1])
74                 pygame.mixer.music.play()
75             i+=1
76
77
78 class Application(Frame):
79
80     def __init__(self, parent):
81
82         Frame.__init__(self, parent)
83         self.parent = parent
84
85         # generate the options of buttons
86         keys = shuffle_dictionary(LETTERS)
87         right_answer, answers = generate_letters(keys, NUM_LETTERS)
88         # create window
89         self.create_window()
90         self.create_letter(right_answer)
91         #self.create_button()
92         self.create_play_button(answers)
93         self.create_option_buttons(right_answer, answers)
94
95     def create_window (self):
96
97         self.parent.geometry(WINDOW_DIMENSIONS)
98         self.parent.title(TITLE)
99
100     def create_letter(self, right_answer):
101
102         self.label = Label(self.parent, text=right_answer, font=("",80))
103         self.label.pack()
104
105     def create_button(self):
106
107         self.button = Button().place(x=5,y=102)
108
109     def create_play_button(self, answers):
110
111         # create list of oggs
112         list_oggs = []
113         i= 0
114         for i in range(0, NUM_LETTERS):
115             list_oggs.append(LETTERS[answers[i]][1])
116             i+=1
117         self.play_image = PhotoImage(file=IMAGES_PATH + 'play.png')
118         # create the button
119         self.play_button = Button(self.parent,width=40,height=30,fg='black',
120             image=self.play_image, command=lambda: play_ogg(list_oggs)).place(x=5,y=102)
121
122     def right_button(self, button):
123         """
124         Button right green and sound
125         """
126         button.config(background = "green")
127         button.config(activebackground="green")
128         pygame.mixer.music.load(SOUNDS_PATH + 'right.ogg')
129         pygame.mixer.music.play()
130
131     def wrong_button(self, button):
132         """
133         Button wrong red and sound
134         """
135         button.config(background = "red")
136         button.config(activebackground="red")
137         pygame.mixer.music.load(SOUNDS_PATH + 'wrong.ogg')
138         pygame.mixer.music.play()
139
140     def create_option_buttons(self, right_answer, answers):
141
142         # create buttons
143         self.option_1 = Button(self.parent,width=7,height=3,fg='black',
144                   text=LETTERS[answers[0]][0])
145         self.option_1.place(x=5,y=140)
146         self.option_2 = Button(self.parent,width=7,height=3,fg='black',
147                   text=LETTERS[answers[1]][0])
148         self.option_2.place(x=5,y=200) 
149         self.option_3 = Button(self.parent,width=7,height=3,fg='black',
150                   text=LETTERS[answers[2]][0])
151         self.option_3.place(x=95,y=140)
152         self.option_4 = Button(self.parent,width=7,height=3,fg='black',
153                   text=LETTERS[answers[3]][0])
154         self.option_4.place(x=95,y=200)
155         self.option_1.config(command=lambda:
156                              self.right_button(self.option_1) if answers[0] == right_answer
157                              else self.wrong_button(self.option_1))
158         self.option_2.config(command=lambda:
159                              self.parent.right_button(self.option_2) if answers[1] == right_answer
160                              else self.wrong_button(self.option_2))
161         self.option_3.config(command=lambda:
162                              self.right_button(self.option_3) if answers[2] == right_answer
163                              else self.wrong_button(self.option_3))
164         self.option_4.config(command=lambda:
165                              self.right_button(self.option_4) if answers[3] == right_answer
166                              else self.wrong_button(self.option_4))
167
168
169 def main():
170
171     pygame.init()
172     root = Tk()
173     app = Application(root)
174     root.mainloop()
175     pygame.quit()
176
177
178 if __name__ == '__main__':
179     main()