Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/Saludame.activity/challenges.py
blob: 2cf1dba87a29fca77cd580676bf6f5149094ee5d (plain)
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# -*- coding: utf-8 -*-

"""
Challenges module
"""

import pygame
import os
import utilities
from window import *
from gettext import gettext as _

S_CORRECT_PATH = os.path.normpath("assets/sound/correct.ogg")
S_OVER_PATH = os.path.normpath("assets/sound/over.ogg")
S_INCORRECT_PATH = os.path.normpath("assets/sound/incorrect.ogg")

N_TF = 4

FIN_MC = False # Toma el valor True cuando finaliza el juego de multiple choice

TITLE_FONT_SIZE = 24
TEXT_FONT_SIZE = 18

ANSWER_COLOR = pygame.Color("blue")
MOUSE_OVER_COLOR = pygame.Color("green")

class MultipleChoice(Window):
    
    def __init__(self, container, rect, frame_rate, windows_controller, challenges_creator, register_id, bg_color=(0, 0, 0)):
        Window.__init__(self, container, rect, frame_rate, windows_controller, register_id, bg_color)
        
        self.set_bg_image("assets/windows/window_1.png")     
        
        ###### Sounds ######
        self.s_correct = pygame.mixer.Sound(S_CORRECT_PATH)
        self.s_over = pygame.mixer.Sound(S_OVER_PATH)
        self.s_incorrect = pygame.mixer.Sound(S_INCORRECT_PATH)
        ####################
        
        self.choices = []
        self.correct = 0
        
        self.opportinities = 1
        
        self.challenges_creator = challenges_creator
        
        # If a question is setted, we have to "erase" the old challenge
        self.question = None
        
        self.win_points = 0
        self.lose_points = 0
        
        # Close Button
        self.btn_close = TextButton(self.rect, pygame.Rect((910, 0), (30, 30)), 1, "X", 32, (0, 0, 0), self._cb_button_click_close)
        self.add_button(self.btn_close)     
        
        self.wait = 0
        
    ####### Set attributes #######
    def set_question(self, question):
        if (self.question):
            self.erase()
                
        self.question = TextBlock(self.rect, 30, 90, 1, question, TITLE_FONT_SIZE, (0, 0, 0), False)
        self.add_child(self.question)
        
    def set_correct_answer(self, a):
        self.correct = a
        
    def set_answers(self, answers):
        x = 30 
        y = self.question.rect_absolute.top  

        y += 40 * len(self.question.lines)
        
        for ans in answers:
            y += 30
            b = TextButton(self.rect, pygame.Rect((x, y), (1, 1)), 1, ans, TEXT_FONT_SIZE, ANSWER_COLOR, self._cb_button_click_choice, self._cb_button_over_choice, self._cb_button_out_choice)
            self.choices.append(b)
            self.buttons.append(b)
            self.add_child(b)        
    
    def set_image(self, image):
        if (not isinstance(image, pygame.Surface)):
            image = pygame.image.load(image)
        image = Image(self.rect, pygame.Rect(500, 40, 20, 20), 1, image)
        self.add_child(image)
    
    def set_win_points(self, points):
        self.win_points = points
    
    def set_lose_points(self, points):
        self.lose_points = points
        
    ######## Callbacks buttons ########
    
    def _cb_button_click_choice(self, button):
        if(button == self.choices[self.correct]):
            self.s_correct.play()
            self.windows_controller.game_man.add_points(self.win_points)                  
            
            self.windows_controller.close_active_window()
            self.windows_controller.windows["info_challenge_window"].update_content(u"¡Respuesta correcta!", u"Muy bien, \nganaste %s puntos para tu barra %s" % (self.win_points, self.challenges_creator.game_man.get_lowest_bar().label))
            self.windows_controller.set_active_window("info_challenge_window") 
            self.opportinities = 1               
        else:
            self.windows_controller.game_man.add_points(-self.lose_points)
            self.s_incorrect.play()
            self.windows_controller.close_active_window()
                
            if (self.opportinities == 0):
                self.windows_controller.windows["info_challenge_window"].update_content(u"Perdiste", u"Qué lástima, no era correcta, \nperdiste %s puntos en tu barra de %s. \nLee la biblioteca o pregunta al maestro/a." % (self.lose_points, self.challenges_creator.game_man.get_lowest_bar().label))
                self.windows_controller.set_active_window("info_challenge_window")
                self.opportinities = 1
            else:                                            
                self.opportinities -= 1
                self.windows_controller.windows["info_challenge_window"].update_content(u"Respuseta incorrecta", u"La respuesta no esta correcta, intenta otra vez")
                self.windows_controller.set_active_window("mc_challenge_window")
                self.windows_controller.set_active_window("info_challenge_window")

        
    def _cb_button_over_choice(self, button):
        if not FIN_MC:
            button.switch_color_text(MOUSE_OVER_COLOR)
            button.force_update()
            self.s_over.play()        
            
    def _cb_button_out_choice(self, button):
        if not FIN_MC:
            button.switch_color_text(ANSWER_COLOR)
            button.force_update()      

    def _cb_button_click_close(self, button):
        self.windows_controller.close_active_window()
    
    ######## Others ########
    
    def erase(self):
        """
        Delete question and answers and repaint. Set FIN_MC false
        """
        self.choices = []
        self.widgets = [self.btn_close] #, self.btn_view_answer]
        self.buttons = [self.btn_close] #, self.btn_view_answer]
        self.repaint = True
        self.wait = 0
        global FIN_MC
        FIN_MC = False
        
class TrueOrFalse(MultipleChoice):
    def __init__(self, container, rect, frame_rate, windows_controller, challenges_creator, register_id, bg_color=(0, 0, 0)):
        
        MultipleChoice.__init__(self, container, rect, frame_rate, windows_controller, challenges_creator, register_id, bg_color)
        
        self.n_tf = N_TF 
        self.question_number = 0
        self.answers = ["waiting", "waiting", "waiting", "waiting", "waiting"]
        
        self.kind = "normal"    # normal or master
                                # master challenge is like a TrueOrFalse challenge with mutlitple choice questions 
                                
        self.limit = 3
        self.perdio = False
        
    def pre_draw(self, screen):
        if self.answers[0] == "waiting":
            q0 = pygame.draw.circle(screen, pygame.Color("grey"), (1020, 550), 10)
        elif self.answers[0] == "incorrect":
            q0 = pygame.draw.circle(screen, pygame.Color("red"), (1020, 550), 10)
        elif self.answers[0] == "correct":
            q0 = pygame.draw.circle(screen, pygame.Color("green"), (1020, 550), 10)
            
        if self.answers[1] == "waiting":
            q1 = pygame.draw.circle(screen, pygame.Color("grey"), (1050, 550), 10)
        elif self.answers[1] == "incorrect":
            q1 = pygame.draw.circle(screen, pygame.Color("red"), (1050, 550), 10)
        elif self.answers[1] == "correct":
            q1 = pygame.draw.circle(screen, pygame.Color("green"), (1050, 550), 10)
        
        
        if self.answers[2] == "waiting":
            q2 = pygame.draw.circle(screen, pygame.Color("grey"), (1080, 550), 10)
        elif self.answers[2] == "incorrect":
            q2 = pygame.draw.circle(screen, pygame.Color("red"), (1080, 550), 10)
        elif self.answers[2] == "correct":
            q2 = pygame.draw.circle(screen, pygame.Color("green"), (1080, 550), 10)
        
        
        if self.answers[3] == "waiting":
            q3 = pygame.draw.circle(screen, pygame.Color("grey"), (1110, 550), 10)
        elif self.answers[3] == "incorrect":
            q3 = pygame.draw.circle(screen, pygame.Color("red"), (1110, 550), 10)
        elif self.answers[3] == "correct":
            q3 = pygame.draw.circle(screen, pygame.Color("green"), (1110, 550), 10)
        
        
        if self.answers[4] == "waiting":
            q4 = pygame.draw.circle(screen, pygame.Color("grey"), (1140, 550), 10)
        elif self.answers[4] == "incorrect":
            q4 = pygame.draw.circle(screen, pygame.Color("red"), (1140, 550), 10)
        elif self.answers[4] == "correct":
            q4 = pygame.draw.circle(screen, pygame.Color("green"), (1140, 550), 10)
        
        
        return [q0, q1, q2, q3, q4]
        
    def _cb_button_click_choice(self, button):
        if(button == self.choices[self.correct]):
            self.s_correct.play()
            self.windows_controller.game_man.add_points(self.win_points)
            if self.n_tf:
                # Correct answer
                self.answers[self.question_number] = "correct"
                self.n_tf -= 1
                if self.kind == "master":                    
                    self.challenges_creator.get_challenge("master")
                elif self.kind == "normal":
                    self.challenges_creator.get_challenge("tf")
                self.question_number += 1
            else:
                self.windows_controller.close_active_window()
                self.result_and_reset()
        else:
            self.windows_controller.game_man.add_points(-self.lose_points)
            self.s_incorrect.play()
            if self.n_tf and not self.perdio:
                # Incorrect answer
                self.answers[self.question_number] = "incorrect"
                self.n_tf -= 1
                                
                if self.kind == "master":
                    if self.answers.count("incorrect") == 5 - self.limit:
                        self.perdio = True  
                    self.challenges_creator.get_challenge("master")
                    
                if self.kind == "normal":
                    self.challenges_creator.get_challenge("tf")
                self.question_number += 1
            else:
                self.windows_controller.close_active_window()
                self.result_and_reset()

    def result_and_reset(self):
        if self.kind == "normal":
            self.windows_controller.windows["info_challenge_window"].update_content(u"%s Respuestas correctas" % (self.answers.count("correct") + 1), u"Ganaste %s puntos para tu \nbarra %s" % ("[ver puntos]", self.challenges_creator.game_man.get_lowest_bar().label))
        if self.kind == "master":
            if self.perdio:
                self.windows_controller.windows["info_challenge_window"].update_content(u"Perdiste", u"Quedaste en este nivel. \n¡Hay que aprender más!")
            else:
                self.windows_controller.windows["info_challenge_window"].update_content(u"Ganaste", u"¡Pasaste de nivel! \nBusca las nuevas acciones en tu menú")
        self.windows_controller.set_active_window("info_challenge_window")
        
        self.n_tf = N_TF
        self.question_number = 0
        self.answers = ["waiting", "waiting", "waiting", "waiting", "waiting"]
        self.perdio = False
        
    def _cb_button_click_close(self, button):
        self.windows_controller.close_active_window()   
        self.n_tf = N_TF
        self.question_number = 0
        self.answers = ["waiting", "waiting", "waiting", "waiting", "waiting"]
        self.perdio = False
    
class InfoChallenge(Window):
    def __init__(self, container, rect, frame_rate, windows_controller, challenges_creator, text_intro, text_result_good, text_result_bad, bg_color=(0, 0, 0)):
        Window.__init__(self, container, rect, frame_rate, windows_controller, "info_challenge_window", bg_color)
             
        self.set_bg_image("assets/windows/window_2.png")
        self.challenges_creator = challenges_creator
        
        self.btn_continue = utilities.get_accept_button(self.rect, pygame.Rect((400, 500), (1, 1)), _("Continue"), self._cb_button_click_continue)
        self.add_button(self.btn_continue)
        
        # Texts
        self.title = utilities.Text(rect, 40, 40, 1, "Verdadero o Falso", 45, pygame.Color("blue"))
        self.text = utilities.TextBlock(rect, 40, 120, 1, "Texto de prueba", 35, pygame.Color("black"))
        self.image = utilities.Image(rect, pygame.Rect(640, 240, 80, 80), 1, "challenges/ninio_normal.png")
    
        self.add_child(self.title)
        self.add_child(self.text)
        self.add_child(self.image)
        
    def update_content(self, title="Verdadero o Falso", text="Texto de prueba", image="challenges/ninio_normal.png"):
        self.title.text = title
        self.title.refresh()
        self.text.parse_lines(text)
        self.image = image
        
    def _cb_button_click_continue(self, button):
        self.windows_controller.close_active_window()   

class Cooking(Window):
    def __init__(self, container, rect, frame_rate, windows_controller, register_id, bg_color=(0, 0, 0)):
        Window.__init__(self, container, rect, frame_rate, windows_controller, register_id, bg_color)
        
        self.set_bg_image("assets/windows/window_1.png") 
        
        # Close Button
        self.btn_close = TextButton(self.rect, pygame.Rect((910, 0), (30, 30)), 1, "X", 32, (0, 0, 0), self._cb_button_click_close)
        self.add_button(self.btn_close) 
        
        # Some widgets to test DnD
        self.dnd = []
        self.dnd.append(Image(rect, pygame.Rect(50, 100, 100, 100), 1, "assets/events/ill.jpg"))
        self.dnd.append(Image(rect, pygame.Rect(50, 250, 100, 100), 1, "assets/events/caries.jpg"))
        self.dnd.append(Image(rect, pygame.Rect(50, 400, 100, 100), 1, "assets/events/unkown.png"))
        
        self.trash = Image(rect, pygame.Rect(500, 200, 200, 200), 1, "assets/challenges/trash.png")

        for w in self.dnd:
            self.add_child(w)
        self.add_child(self.trash)  
        
        # Mouse mode (1 - left button pressed)
        self.mouse_mode = 0  
        
    def handle_mouse_down(self, (x, y)):
        Window.handle_mouse_down(self, (x, y))                
        self.mouse_mode = 1    
        
    def handle_mouse_up(self, pos):  
        Window.handle_mouse_up(self, pos) 
        for widget in self.dnd:
            if self.trash.contains_point(widget.rect_absolute.centerx, widget.rect_absolute.centery):
                self.remove_child(widget)                            
        self.mouse_mode = 0
    
    def handle_mouse_motion(self, pos):
        if pos[0] < self.rect.right - 70 and pos[0] > self.rect.left + 70 and pos[1] < self.rect.bottom - 70 and pos[1] > self.rect.top + 120:
            if self.mouse_mode == 1:
                for widget in self.dnd:
                    if widget.contains_point(pos[0], pos[1]):
                        widget.rect_absolute.centerx = pos[0]
                        widget.rect_absolute.centery = pos[1]     
                        self.repaint = True               
        
    def _cb_button_click_close(self, button):
        self.windows_controller.close_active_window()