From a5ad10b19f83fcd90cc4133b2011be57005e1cb1 Mon Sep 17 00:00:00 2001 From: Diego Mazzone Date: Wed, 22 Dec 2010 17:19:08 +0000 Subject: New challenge [Cooking] Drag&Drop challenge. Edge control. Put widgets on the litter bin. New asset . Function dispose @Window. New events handlers (mouseup, mousemotion). Fix @Images. Changes on: challenges.py, challenges_creator.py, game.py, kid_window.py, main_window.py, utilities.py, widget.py, window.py, windows_controller.py --- diff --git a/Saludame.activity/assets/challenges/trash.png b/Saludame.activity/assets/challenges/trash.png new file mode 100644 index 0000000..7f93a6a --- /dev/null +++ b/Saludame.activity/assets/challenges/trash.png Binary files differ diff --git a/Saludame.activity/challenges.py b/Saludame.activity/challenges.py index 6379e27..5183d94 100755 --- a/Saludame.activity/challenges.py +++ b/Saludame.activity/challenges.py @@ -27,7 +27,9 @@ 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) + 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) @@ -50,10 +52,7 @@ class MultipleChoice(Window): # 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.buttons += [self.btn_close] - - for b in self.buttons: - self.add_child(b) + self.add_button(self.btn_close) self.wait = 0 @@ -290,3 +289,51 @@ class InfoChallenge(Window): 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() diff --git a/Saludame.activity/challenges_creator.py b/Saludame.activity/challenges_creator.py index 98b0875..a8107b1 100755 --- a/Saludame.activity/challenges_creator.py +++ b/Saludame.activity/challenges_creator.py @@ -18,13 +18,14 @@ class ChallengesCreator: self.game_man = game_man - # Multiple Choice window + # Multiple Choice and Master window self.mc_challenge = challenges.MultipleChoice(self.container, self.rect, self.frame_rate, self.windows_controller, self, "mc_challenge_window", self.bg_color) - self.mc_challenge.set_bg_image("assets/windows/window_1.png") # True or False window self.tf_challenge = challenges.TrueOrFalse(self.container, self.rect, self.frame_rate, self.windows_controller, self, "tf_challenge_window", self.bg_color) - self.tf_challenge.set_bg_image("assets/windows/window_1.png") + + # Cooking window + self.cooking_challenge = challenges.Cooking(self.container, self.rect, self.frame_rate, self.windows_controller, "cooking_challenge_window", self.bg_color) # Tuples of mc_challenges self.mc_challenges = [] diff --git a/Saludame.activity/game.py b/Saludame.activity/game.py index 60344b7..24509bf 100755 --- a/Saludame.activity/game.py +++ b/Saludame.activity/game.py @@ -125,6 +125,10 @@ class Main(): game_man.save_game() elif event.type == pygame.MOUSEBUTTONDOWN: self.windows_controller.handle_mouse_down(pygame.mouse.get_pos()) + elif event.type == pygame.MOUSEBUTTONUP: + self.windows_controller.handle_mouse_up(event.pos) + elif event.type == pygame.MOUSEMOTION: + self.windows_controller.handle_mouse_motion(event.pos) elif event.type == pygame.VIDEOEXPOSE: self.windows_controller.reload_main = True elif event.type == pygame.USEREVENT and event.code == 0: # Music ended diff --git a/Saludame.activity/kid_window.py b/Saludame.activity/kid_window.py index a3b0659..f9cb762 100644 --- a/Saludame.activity/kid_window.py +++ b/Saludame.activity/kid_window.py @@ -144,12 +144,11 @@ class ExternalCharacter(Window): changes += Window.draw(self, screen, frames) changes += self.message_balloon.draw(screen, frames) return changes - else: - + else: screen.blit(self.bg1, self.rect) screen.blit(self.bg2, self.message_balloon.rect) - return [self.rect, self.message_balloon.rect] - + self.dispose() + return [self.rect, self.message_balloon.rect] class MessageBalloon(Window): @@ -188,5 +187,6 @@ class MessageBalloon(Window): return Window.draw(self, screen, frames) else: screen.blit(self.bg, self.rect) + self.dispose() return [self.rect] \ No newline at end of file diff --git a/Saludame.activity/main_window.py b/Saludame.activity/main_window.py index bed3f2c..58bdbdb 100755 --- a/Saludame.activity/main_window.py +++ b/Saludame.activity/main_window.py @@ -34,10 +34,7 @@ class MainWindow(Window): self.add_child(Clock(container, pygame.Rect(0, 528, 1, 1), 1, game_man)) - # Challenges - challenges_button3 = ImageButton(self.rect, pygame.Rect((1120, 300), (60, 60)), 1, "challenges/trophy.png", self._cb_button_click_master_challenge) - challenges_button3.set_tooltip(_("Master challenge")) - self.add_button(challenges_button3) + # Challenges challenges_button = ImageButton(self.rect, pygame.Rect((1120, 400), (60, 60)), 1, "challenges/trophy.png", self._cb_button_click_mc_challenges) challenges_button.set_tooltip(_("Multiple choice")) @@ -47,6 +44,14 @@ class MainWindow(Window): challenges_button2.set_tooltip(_("True or false")) self.add_button(challenges_button2) + challenges_button3 = ImageButton(self.rect, pygame.Rect((1120, 300), (60, 60)), 1, "challenges/trophy.png", self._cb_button_click_master_challenge) + challenges_button3.set_tooltip(_("Master challenge")) + self.add_button(challenges_button3) + + challenges_button4 = ImageButton(self.rect, pygame.Rect((1120, 200), (60, 60)), 1, "challenges/trophy.png", self._cb_button_click_cooking_challenge) + challenges_button4.set_tooltip(_("Cooking")) + self.add_button(challenges_button4) + button_back = pygame.image.load("customization/customization_button.png").convert_alpha() btn_reset = utilities.TextButton2(self.rect, pygame.Rect((1000, 20), (70, 30)), 1, _("Reset"), 30, (255, 255, 255), button_back, self._cb_reset_game) self.add_button(btn_reset) @@ -75,6 +80,8 @@ class MainWindow(Window): self.windows_controller.windows["info_challenge_window"].update_content(u"Super Desafío", u"¡Estas por pasar de nivel! \nPara superarlo tienes que responder \ncorrecto a 3 de las 5 preguntas \nque siguen \n\n¡Suerte!") self.windows_controller.set_active_window("info_challenge_window") + def _cb_button_click_cooking_challenge(self, button): + self.windows_controller.set_active_window("cooking_challenge_window") def _cb_button_click_stop_animation(self, button): self.panel_win.stop_animation() diff --git a/Saludame.activity/utilities.py b/Saludame.activity/utilities.py index f005561..ff2612b 100755 --- a/Saludame.activity/utilities.py +++ b/Saludame.activity/utilities.py @@ -56,8 +56,7 @@ class Image(Widget): self.background = pygame.image.load(image).convert_alpha() else: self.background = image - Widget.__init__(self, container, rect, frame_rate, self.background) - + Widget.__init__(self, container, pygame.Rect((rect.left, rect.top), self.background.get_rect().size), frame_rate, self.background) class Button(Widget): @@ -73,9 +72,6 @@ class Button(Widget): self.over = False self.enable = True - - def contains_point(self, x, y): - return self.rect_absolute.collidepoint(x, y) def set_tooltip(self, text): self.tooltip = text diff --git a/Saludame.activity/widget.py b/Saludame.activity/widget.py index 6a2316b..8751827 100755 --- a/Saludame.activity/widget.py +++ b/Saludame.activity/widget.py @@ -67,4 +67,7 @@ class Widget: rect = pygame.Rect(parents_relative_pos, self.rect_absolute.size) return background.subsurface(rect) return pygame.Surface(self.rect_absolute.size) + + def contains_point(self, x, y): + return self.rect_absolute.collidepoint(x, y) \ No newline at end of file diff --git a/Saludame.activity/window.py b/Saludame.activity/window.py index 066f255..6ae3afa 100755 --- a/Saludame.activity/window.py +++ b/Saludame.activity/window.py @@ -44,6 +44,9 @@ class Window: def set_bg_color(self, color): self.bg_color = color + + def dispose(self): + self.windows_controller.unregister_window(self) # Abstract function. def pre_draw(self, screen): @@ -155,7 +158,15 @@ class Window: button.showing_tooltip = False button.over = False button.on_mouse_out() + + # It will be overridden by cooking challenge or other D&D challenge + def handle_mouse_motion(self, (x, y)): + pass + # It will be overridden by cooking challenge or other D&D challenge + def handle_mouse_up(self, pos): + pass + def move(self, (x, y)): """ Moves the window the given offset, notifying all its subitems """ self.rect.move_ip(x, y) diff --git a/Saludame.activity/windows_controller.py b/Saludame.activity/windows_controller.py index 6bc7628..7d5e6c1 100755 --- a/Saludame.activity/windows_controller.py +++ b/Saludame.activity/windows_controller.py @@ -89,6 +89,9 @@ class WindowsController: def register_new_window(self, id, window): self.windows[id] = window + def unregister_window(self, window): + del self.windows[window.register_id] + def show_window_hierarchy(self, window): sys.stdout.write(window.get_register_id()) W = [] @@ -151,10 +154,17 @@ class WindowsController: def handle_mouse_down(self, (x, y)): x, y = self.scaled_game.scale_coordinates((x, y)) self.windows_stack[-1].handle_mouse_down((x, y)) + + def handle_mouse_up(self, pos): + self.windows_stack[-1].handle_mouse_up(pos) def handle_mouse_over(self, (x, y)): x, y = self.scaled_game.scale_coordinates((x, y)) self.windows_stack[-1].handle_mouse_over((x, y)) + + def handle_mouse_motion(self, (x, y)): + x, y = self.scaled_game.scale_coordinates((x, y)) + self.windows_stack[-1].handle_mouse_motion((x, y)) ########################## #### Tooltips ##### -- cgit v0.9.1