First version 0.1
[tibetan_culture:learn_tibetan.git] / src / main.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 from random import shuffle
17 from unicodedata import lookup
18 from Tkinter import Tk, Button, PhotoImage, Label
19 import pygame 
20
21 letters = {lookup("tibetan letter ka"):("ka", "../sounds/kha.ogg"), 
22           lookup("tibetan letter kha"):("k'a", "../sounds/kha.ogg"),
23           lookup("tibetan letter ga"):("k_'a", "../sounds/kha.ogg"),
24           lookup("tibetan letter nga"):("nga", "../sounds/kha.ogg")}
25 num_letters = 4
26
27 def shuffle_dictionary (dictionary):
28     """
29     Return random keys of dictionary
30     """
31     # Python 3 
32     # keys =  list(dictionary.keys())
33     # Python 2 
34     keys = dictionary.keys()
35     shuffle(keys)
36     return keys
37         
38 def generate_letters(list_letters, num_letters):
39     """
40     Return a right letter and a list of shuffle letters
41     """
42     right_letter = list_letters[0]
43     letters = list_letters[0:num_letters]
44     shuffle(letters)
45     return right_letter, letters
46
47 def play_ogg(list_oggs):
48     """
49     Play a list of oggs
50     """
51     i=0
52     SONG_END = pygame.USEREVENT + 1
53     pygame.mixer.music.set_endevent(SONG_END)
54     pygame.mixer.music.load(list_oggs[i])
55     pygame.mixer.music.play()
56   
57     running = True
58     while running:
59         for event in pygame.event.get():
60             if  i >= len(list_oggs) - 1:
61                 running = False
62             elif event.type == SONG_END:
63                 pygame.mixer.music.load(list_oggs[i+1])
64                 pygame.mixer.music.play()
65             i+=1     
66  
67 def right_button(button):
68     """
69     Right answer, button right green and sound
70     """
71     button.config(background = "green")
72     button.config(activebackground="green")
73     pygame.mixer.music.load('../sounds/right.ogg')
74     pygame.mixer.music.play()
75     
76 def wrong_button(button):
77     """
78     Wrong answer, button right green and sound
79     """
80     button.config(background = "red")
81     button.config(activebackground="red")
82     pygame.mixer.music.load('../sounds/wrong.ogg')
83     pygame.mixer.music.play()
84     
85 # random the letters
86 keys = shuffle_dictionary(letters)
87 right_answer, answers = generate_letters(keys, num_letters)
88 # lista de sonidos
89 list_oggs = []
90 i= 0
91 for i in range(0, num_letters):
92     list_oggs.append(letters[answers[i]][1])
93     i+=1
94
95 pygame.init()
96 # Tkinter
97 ventana = Tk()
98 play = PhotoImage(file='../images/play.png')
99 ventana.geometry("185x260+100+80")
100 label = Label(ventana, text = right_answer, font=("", 80))
101 label.pack()
102  
103 play_sound=Button(ventana,width=40,height=30,fg='black',
104             image=play, command=lambda: play_ogg(list_oggs)).place(x=5,y=102)
105 option_1 = Button(ventana,width=7,height=3,fg='black', 
106                   text=letters[answers[0]][0])
107 option_1.place(x=5,y=140)
108 option_2 = Button(ventana,width=7,height=3,fg='black', 
109                   text=letters[answers[1]][0])
110 option_2.place(x=5,y=200) 
111 option_3 = Button(ventana,width=7,height=3,fg='black', 
112                   text=letters[answers[2]][0])
113 option_3.place(x=95,y=140)
114 option_4 = Button(ventana,width=7,height=3,fg='black', 
115                   text=letters[answers[3]][0])
116 option_4.place(x=95,y=200)
117 option_1.config(command=lambda: right_button(option_1) if answers[0] == right_answer else wrong_button(option_1) )
118 option_2.config(command=lambda: right_button(option_2) if answers[1] == right_answer else wrong_button(option_2))
119 option_3.config(command=lambda: right_button(option_3) if answers[2] == right_answer else wrong_button(option_3))
120 option_4.config(command=lambda: right_button(option_4) if answers[3] == right_answer else wrong_button(option_4))
121  
122 ventana.mainloop()
123 pygame.quit()