Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xSaludame.activity/animation.py29
-rwxr-xr-xSaludame.activity/challenges.py148
-rwxr-xr-xSaludame.activity/customization.py17
-rwxr-xr-xSaludame.activity/game.py25
-rwxr-xr-xSaludame.activity/menu.py2
-rwxr-xr-xSaludame.activity/menu_creator.py2
-rwxr-xr-xSaludame.activity/utilities.py134
-rwxr-xr-xSaludame.activity/widget.py29
-rwxr-xr-xSaludame.activity/window.py284
-rwxr-xr-xSaludame.activity/windows_controller.py (renamed from Saludame.activity/windowsController.py)16
-rw-r--r--resources/uml.zargobin0 -> 10838 bytes
11 files changed, 338 insertions, 348 deletions
diff --git a/Saludame.activity/animation.py b/Saludame.activity/animation.py
index 8126f82..2a25ae3 100755
--- a/Saludame.activity/animation.py
+++ b/Saludame.activity/animation.py
@@ -3,6 +3,7 @@
import pygame
import os
import utilities
+from window import *
KID_PATH = os.path.normpath("assets/kid")
KID_PREFIX, KID_SUFIX = "kid", ".png"
@@ -15,17 +16,18 @@ COLORS_EYES_NEW = [("#008000",), ("#2222FF",), ("#000000",)]
GRAY = pygame.Color("gray")
BLACK = pygame.Color("black")
+BLUE = pygame.Color("blue")
-class Kid:
+class Kid(Window):
- def __init__(self, rect, background, frame_rate):
+ def __init__(self, container, rect, frame_rate, background, screen, windows_controller):
+ Window.__init__(self, container, rect, frame_rate, background, screen, windows_controller)
+
self.index = 1
- self.rect = rect
- self.frame_rate = frame_rate
self.color_index = 0
- self.background = background
- def draw(self, screen):
+ def pre_draw(self, screen):
+
file_nro = str(self.index)
if len(file_nro) == 1:
file_nro = "0" + file_nro
@@ -40,10 +42,7 @@ class Kid:
self.index = (self.index % 11) + 1
if self.index == 1:
- self.color_index = (self.color_index + 1) % 3
-
- return [self.rect]
-
+ self.color_index = (self.color_index + 1) % 3
def change_color(self, old, new):
# No funciona en pygame 1.8.0
@@ -77,11 +76,11 @@ class Food:
self.blip = pygame.mixer.Sound(BLIP_PATH)
- def draw(self, screen):
+ def draw(self, screen, frames):
file = self.file_list[self.index]
self.sprite = pygame.image.load(file).convert_alpha()
- screen.fill(BLACK, self.rect)
+ screen.fill(BLUE, self.rect)
screen.blit(self.sprite, self.rect)
self.index = (self.index + 1) % len(self.file_list)
@@ -95,16 +94,16 @@ class Apple(Food):
class FPS:
- def __init__(self, rect, frame_rate, clock):
+ def __init__(self, container, rect, frame_rate, clock):
self.rect = rect
self.frame_rate = frame_rate
self.clock = clock
self.font = pygame.font.Font(None, 16)
- def draw(self, screen):
+ def draw(self, screen, frames):
screen.fill(BLACK, self.rect)
text = str(round(self.clock.get_fps()))
- text_surf = self.font.render(text, False, (255,255,255))
+ text_surf = self.font.render(text, False, (255, 255, 255))
screen.blit(text_surf, self.rect)
return [self.rect]
diff --git a/Saludame.activity/challenges.py b/Saludame.activity/challenges.py
index 8d460a5..27f8bf7 100755
--- a/Saludame.activity/challenges.py
+++ b/Saludame.activity/challenges.py
@@ -5,6 +5,7 @@
import pygame
import os
from utilities import *
+from window import *
S_CORRECT_PATH = os.path.normpath("assets/sound/correct.ogg")
S_OVER_PATH = os.path.normpath("assets/sound/over.ogg")
@@ -13,106 +14,77 @@ I_FRANCIA_PATH = os.path.normpath("assets/challenges/francia.jpg")
FIN_MC = False # Toma el valor True cuando finaliza el juego de multiple choice
-class MultipleChoice:
+class MultipleChoice(Window):
- def __init__(self, rect, frame_rate):
- self.rect = rect
- self.frame_rate = frame_rate
- self.background = pygame.image.load(I_FRANCIA_PATH).convert()
+ def __init__(self, container, rect, frame_rate, background, screen, windows_controller):
+ Window.__init__(self, container, rect, frame_rate, background, screen, windows_controller)
- self.question = Text(self.rect.left + 10, self.rect.top + 5, "Cual es la capital de Francia?", 20)
+ ###### Images ######
+ self.image = pygame.image.load(I_FRANCIA_PATH).convert()
+ ####################
- # Boton cerrar
- self.btn_close = CloseButton(self.rect, 770, 5, 30, 30, "X")
-
- self.choices = []
- self.choices += [Choice(self.rect, 20, 40, 200, 20, "Buenos Aires")]
- self.choices += [Choice(self.rect, 20, 70, 200, 20, "Oslo")]
- self.choices += [Choice(self.rect, 20, 100, 200, 20, "Roma")]
- self.choices += [Choice(self.rect, 20, 130, 200, 20, "Paris")]
- self.choices += [Choice(self.rect, 20, 160, 200, 20, "Moscu")]
+ ###### 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.btn_view_answer = ViewAnswer(self.rect, 20, 200, 200, 20, "Me doy por vencido! :(...", self.choices[3])
- self.choices += [self.btn_view_answer]
-
- def draw(self, screen):
- screen.fill((150, 150, 255), self.rect)
- screen.blit(self.background, (self.rect.right - 230, self.rect.top + 30))
- self.question.draw(screen)
- self.btn_close.draw(screen)
- self.btn_view_answer.draw(screen)
- for button in self.choices:
- button.draw(screen)
- return [self.rect]
-
- def handle_mouse_down(self, (x, y), windows_controller):
- global FIN_MC
+ # Question
+ self.question = Text(self.rect, 5, 5, 1, "Cual es la capital de Francia?", 40, (0, 255, 0))
+ self.add_child(self.question)
+
+ # Close Button
+ self.btn_close = TextButton(self.rect, pygame.Rect((770, 5), (30, 30)), 1, "X", 30, (0, 0, 0), self._cb_button_click_close)
+ self.buttons += [self.btn_close]
- if (self.btn_close.contains_point(x, y)):
- self.btn_close.on_mouse_click(windows_controller)
+ ###### Choices ######
+ self.buttons += [TextButton(self.rect, pygame.Rect((20, 40), (200, 20)), 1, "Buenos Aires", 30, (255, 255, 255), self._cb_button_click_choice, self._cb_button_over_choice, self._cb_button_out_choice)]
+ self.buttons += [TextButton(self.rect, pygame.Rect((20, 70), (200, 20)), 1, "Oslo", 30, (255, 255, 255), self._cb_button_click_choice, self._cb_button_over_choice, self._cb_button_out_choice)]
+ self.buttons += [TextButton(self.rect, pygame.Rect((20, 100), (200, 20)), 1, "Roma", 30, (255, 255, 255), self._cb_button_click_choice, self._cb_button_over_choice, self._cb_button_out_choice)]
+ self.buttons += [TextButton(self.rect, pygame.Rect((20, 130), (200, 20)), 1, "Paris", 30, (255, 255, 255), self._cb_button_click_choice, self._cb_button_over_choice, self._cb_button_out_choice)]
+ self.buttons += [TextButton(self.rect, pygame.Rect((20, 160), (200, 20)), 1, "Moscu", 30, (255, 255, 255), self._cb_button_click_choice, self._cb_button_over_choice, self._cb_button_out_choice)]
- else:
- for choice in self.choices:
- if (choice.contains_point(x, y) and not FIN_MC):
- fin = choice.on_mouse_click() # Fin representa el usuario ya contesto bien o se dio por vencido
- if(fin):
- FIN_MC = fin
- self.choices = [self.choices[3], self.choices[5]]
- break # No tiene sentido seguir iterando sobre los botones si ya sabemos cual apreto
-
- def handle_mouse_over(self, (x, y)):
- for choice in self.choices:
- if (choice.contains_point(x, y)):
- if(not choice.over):
- choice.on_mouse_over()
- choice.over = True
- else:
- choice.over = False
- choice.on_mouse_out()
-
- def get_windows(self):
- return [self]
+ # Answer Button
+ self.btn_view_answer = TextButton(self.rect, pygame.Rect(20, 200, 20, 20), 1, "Me doy por vencido! :(...", 30, (255, 20, 20), self._cb_button_click_answer, self._cb_button_over_answer, self._cb_button_out_answer)
+ self.buttons += [self.btn_view_answer]
-class Choice(Button):
- def __init__(self, rect, x, y, w, h, text):
- Button.__init__(self, rect, x, y, w, h, text)
- 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.y = y
- """
- Temporal para simular una respuesta correcta basandonos en la coordenada
- 'y' del rectangulo que la contiene.
- """
+ france_image = Image(self.rect, pygame.Rect(500, 40, 20, 20), 1, self.image)
+ self.add_child(france_image)
- def on_mouse_click(self):
- if(self.y == 130):
+ for b in self.buttons:
+ self.add_child(b)
+
+
+ ######## Callbacks buttons ########
+
+ def _cb_button_click_choice(self, button):
+ global FIN_MC
+ if(button.rect.top == 130):
self.s_correct.play()
- return True # Damos por finalizada la pregunta
+ FIN_MC = True # Damos por finalizada la pregunta
else:
self.s_incorrect.play()
- def on_mouse_over(self):
- self.s_over.play()
- self.set_background_color((0, 255, 0))
-
- def on_mouse_out(self):
- self.set_background_color((255, 0, 0))
-
-class ViewAnswer(Button):
- def __init__(self, rect, x, y, w, h, text, resp):
- Button.__init__(self, rect, x, y, w, h, text)
- self.s_over = pygame.mixer.Sound(S_OVER_PATH)
- self.resp = resp
- self.set_background_color((20, 100, 45))
-
- def on_mouse_click(self):
- self.resp.set_background_color((0, 255, 255))
- return True # Damos por finalizada la pregunta
+ def _cb_button_over_choice(self, button):
+ button.switch_color_text((255, 0, 0))
+ button.force_update(self.screen)
+ self.s_over.play()
+
+ def _cb_button_out_choice(self, button):
+ button.switch_color_text((255, 255, 255))
+ button.force_update(self.screen)
+
+ def _cb_button_click_answer(self, button):
+ global FIN_MC
+ FIN_MC = True
- def on_mouse_over(self):
+ def _cb_button_over_answer(self, button):
self.s_over.play()
- self.set_background_color((45, 255, 100))
+
+ def _cb_button_out_answer(self, button):
+ pass
+
+ def _cb_button_click_close(self, button):
+ self.windows_controller.close_active_window()
- def on_mouse_out(self):
- self.set_background_color((20, 100, 45))
+ ########################################
diff --git a/Saludame.activity/customization.py b/Saludame.activity/customization.py
index 2569969..af60968 100755
--- a/Saludame.activity/customization.py
+++ b/Saludame.activity/customization.py
@@ -8,12 +8,13 @@ from gettext import gettext as _
class CustomizationWindow(window.Window):
- def __init__(self, rect, frame_rate):
- window.Window.__init__(self, rect, frame_rate, pygame.Color("Gray"))
+ def __init__(self, rect, frame_rate, background, screen):
+ window.Window.__init__(self, rect, frame_rate, background, screen)
+ self.screen = screen
self.windows = []
kid_rect = self.rect.move(50, 40)
- self.kid = CustomizatedKid(kid_rect, 1)
+ self.kid = CustomizatedKid(kid_rect, 1, pygame.Color("Black"), screen)
self.windows.append(self.kid)
self.btn_close = utilities.CloseButton(self.rect, 770, 5, 30, 30, "X")
@@ -22,7 +23,7 @@ class CustomizationWindow(window.Window):
self.color_list = [pygame.Color(color) for color in ["Brown", "Black", "Green", "Gray", "Skyblue", "Blue"]]
self.eyes_color_index = 0
- def draw(self, screen):
+ def draw(self, screen, frames):
screen.fill(self.background_color)
self.btn_close.draw(screen)
self.btn_eyes.draw(screen)
@@ -57,11 +58,11 @@ class CustomizatedKid(window.Window):
"skin": pygame.Color("#803300")
}
- def __init__(self, rect, frame_rate):
- window.Window.__init__(self, rect, frame_rate, pygame.Color("Black"))
+ def __init__(self, rect, frame_rate, background, screen):
+ window.Window.__init__(self, rect, frame_rate, background, screen)
- self.mappings = CustomizatedKid.COLOR_MAP.copy()
- self.set_gender("male")
+ self.mappings = CustomizatedKid.COLOR_MAP.copy()
+ self.set_gender("male")
def draw(self, screen):
screen.blit(self.sprite, self.rect)
diff --git a/Saludame.activity/game.py b/Saludame.activity/game.py
index d01913d..6c21bbc 100755
--- a/Saludame.activity/game.py
+++ b/Saludame.activity/game.py
@@ -3,8 +3,10 @@
import pygame
import logging
from gettext import gettext as _
+import animation
+from utilities import *
-from windowsController import *
+from windows_controller import *
import window
import challenges
import customization
@@ -51,26 +53,33 @@ class Main():
# This clock is used to keep the game at the desired FPS.
clock = pygame.time.Clock()
+
# Challenges Window
- challenges_window = challenges.MultipleChoice(pygame.Rect((200, 150), (800, 400)), 1)
+ challenges_window = challenges.MultipleChoice(screen.get_rect(), pygame.Rect((200, 150), (800, 400)), 1, (100,45,255), screen, self.windows_controller)
self.windows_controller.add_new_window(challenges_window, "challenges")
-
+
+ """
# Customization Window
- customization_window = customization.CustomizationWindow(pygame.Rect((200, 100), (800, 500)), 1)
+ customization_window = customization.CustomizationWindow(pygame.Rect((200, 100), (800, 500)), 1, pygame.Color("Gray"), screen)
self.windows_controller.add_new_window(customization_window, "customization")
+ """
# Main Window
- main_window = (window.MainWindow(clock))
- self.windows_controller.add_new_window(main_window, "main")
+ main_window = (window.MainWindow(screen.get_rect(), screen.get_rect(), 1, clock, screen, self.windows_controller))
+ self.windows_controller.add_new_window(main_window, "main")
+
+ #Probando ActionWindow
+ main_window.action_win.play_animation('eat_apple')
# Activamos ventana principal
- self.windows_controller.set_active_window("main")
-
+ self.windows_controller.set_active_window("main")
+
frames = 0
# Main loop
update = True # The first time the screen need to be updated
running = True
+
while running:
if from_sugar:
diff --git a/Saludame.activity/menu.py b/Saludame.activity/menu.py
index c0157b9..d376e7b 100755
--- a/Saludame.activity/menu.py
+++ b/Saludame.activity/menu.py
@@ -33,7 +33,7 @@ class Menu:
self.calculate()
"""
- def draw(self, screen):
+ def draw(self, screen, frames):
"""
draw menu items
"""
diff --git a/Saludame.activity/menu_creator.py b/Saludame.activity/menu_creator.py
index 428a9db..d305fb7 100755
--- a/Saludame.activity/menu_creator.py
+++ b/Saludame.activity/menu_creator.py
@@ -11,7 +11,7 @@ example = [
("sport", "assets/icons/icon.png", "Do sports...", [
("run", "assets/icons/icon.png", "Run", None),
("jump rope", "assets/icons/icon.png", "Jump the rope", None),
- ("footbal", "assets/icons/icon.png", "Play footbal", None)
+ ("football", "assets/icons/icon.png", "Play footbal", None)
]),
("sleep", "assets/icons/icon.png", "Go to sleep", None),
("talk", "assets/icons/icon3.png", "talk with a friend", None),
diff --git a/Saludame.activity/utilities.py b/Saludame.activity/utilities.py
index 3d57765..294e173 100755
--- a/Saludame.activity/utilities.py
+++ b/Saludame.activity/utilities.py
@@ -1,106 +1,98 @@
# -*- coding: utf-8 -*-
-# Utilitarios
+# Utilitarios: Text, Button (abstract), ImageButton, TextButton
-import pygame
+from widget import *
-class Text:
- def __init__(self, x, y, text, size):
+class Text(Widget):
+ def __init__(self, container, x, y, frame_rate, text, size, color):
self.font = pygame.font.SysFont(None, size)
- self.ren = self.font.render(text, 1, (0, 0, 100))
- self.x = x
- self.y = y
+ self.surface = self.font.render(text, True, color)
+
+ self.text = text
+ Widget.__init__(self, container, self.surface.get_rect(topleft=(x, y)), frame_rate, self.surface, None)
+
+ def switch_color_text(self, color):
+ self.surface = self.font.render(self.text, True, color)
+ return (self)
- def draw(self, screen):
- screen.blit(self.ren, (self.x, self.y))
+class Image(Widget):
+ def __init__(self, container, rect, frame_rate, image):
+
+ if not isinstance(image, pygame.Surface):
+ self.surface = pygame.image.load(image).convert_alpha()
+ else:
+ self.surface = image
+ Widget.__init__(self, container, rect, frame_rate, self.surface)
+
-class Button:
+class Button(Widget):
# Clase abstracta que representa un boton
- def __init__(self, rect, x, y, w, h, text):
- # Agregamos los botones con coordenadas "relativas" a la ventana que los cotiene
- self.rect = pygame.Rect(rect.left + x, rect.top + y, w, h)
- self.text = text
+ def __init__(self, container, rect, frame_rate, surface, cb_click=None, cb_over=None, cb_out=None):
+
+ Widget.__init__(self, container, rect, frame_rate, surface)
- self.font = pygame.font.SysFont(None, 16)
+ self.function_on_mouse_click = cb_click
+ self.function_on_mouse_over = cb_over
+ self.function_on_mouse_out = cb_out
- self.background_color = (255,0,0)
self.over = False
-
+
def contains_point(self, x, y):
- return self.rect.collidepoint(x, y)
-
- def draw(self, surface):
- ren = self.font.render(self.text, 1, (0, 0, 100))
- surface.fill(self.background_color, self.rect)
- surface.blit(ren, (self.rect.left + 5, self.rect.top + 5))
-
- def set_background_color(self, color):
- self.background_color = color
+ return self.rect_in_container.collidepoint(x, y)
- # Eventos sobre el boton... seran sobreescritos por los hijos
-
def on_mouse_click(self):
- None
+ if self.function_on_mouse_click: # if there's a callback setted makes the call
+ self.function_on_mouse_click(self)
def on_mouse_over(self):
- None
+ if self.function_on_mouse_over: # if there's a callback setted makes the call
+ self.function_on_mouse_over(self)
def on_mouse_out(self):
- None
+ if self.function_on_mouse_out: # if there's a callback setted makes the call
+ self.function_on_mouse_out(self)
+
+ def set_on_mouse_click(self, fn):
+ self.function_on_mouse_click = fn
+
+ def set_on_mouse_over(self, fn):
+ self.function_on_mouse_over = fn
-class ImageButton:
+ def set_on_mouse_out(self, fn):
+ self.function_on_mouse_out = fn
- # Clase abstracta que representa un boton con una imagen de fondo
+
+class ImageButton(Button):
- def __init__(self, rect, x, y, image):
+ def __init__(self, container, rect, frame_rate, image, cb_click=None, cb_over=None, cb_out=None):
+
+ self.image = image
if not isinstance(image, pygame.Surface):
+ self.image = pygame.image.load(image).convert_alpha()
+
+ Button.__init__(self, container, rect, frame_rate, self.image, cb_click, cb_over, cb_out)
+
+ def switch_image_background(self, image):
+ if not isinstance(image, pygame.Surface):
image = pygame.image.load(image).convert_alpha()
+ self.surface = image
- self.image = image
-
- # Agregamos los botones con coordenadas "relativas" a la ventana que los cotiene
- self.rect = pygame.Rect(rect.left + x, rect.top + y, image.get_width(), image.get_height())
-
- self.background_color = (0, 0, 0)
- self.over = False
+class TextButton(ImageButton):
+ def __init__(self, container, rect, frame_rate, text, size, color, cb_click=None, cb_over=None, cb_out=None):
+ self.text = Text(rect, 5, 5, frame_rate, text, size, color)
+ ImageButton.__init__(self, container, rect, frame_rate, self.text.surface, cb_click, cb_over, cb_out)
- def contains_point(self, x, y):
- return self.rect.collidepoint(x, y)
-
- def draw(self, surface):
- surface.blit(self.image, self.rect.topleft)
- return [self.rect]
-
- def set_background_color(self, color):
- self.background_color = color
+ def switch_color_text(self, color):
+ self.surface = self.text.switch_color_text(color).surface
- # Eventos sobre el boton... seran sobreescritos por los hijos
- def on_mouse_click(self):
- None
-
- def on_mouse_over(self):
- None
-
- def on_mouse_out(self):
- None
-
-class CloseButton(Button):
- def __init__(self, rect, x, y, w, h, text):
- Button.__init__(self, rect, x, y, w, h, text)
- self.font = pygame.font.SysFont(None, 30) # Modificamos atributo heredado
- self.background_color = (150, 150, 255) # Modificamos atributo heredado
-
- def on_mouse_click(self, windows_controller):
- windows_controller.close_active_window()
-
def change_color(surface, old_color, new_color):
# No funciona en pygame 1.8.0
#image_pixel_array = pygame.PixelArray(self.sprite)
#image_pixel_array.replace(old_color, new_color)
mapped_int = surface.map_rgb(old_color)
- surface.set_palette_at(mapped_int, new_color[0:3])
- \ No newline at end of file
+ surface.set_palette_at(mapped_int, new_color[0:3])
diff --git a/Saludame.activity/widget.py b/Saludame.activity/widget.py
new file mode 100755
index 0000000..3a542b3
--- /dev/null
+++ b/Saludame.activity/widget.py
@@ -0,0 +1,29 @@
+# -*- coding: utf-8 -*-
+
+import pygame
+
+class Widget:
+
+ # Un widget representa cualquier cosa "pintable"
+
+ def __init__(self, container, rect, frame_rate, surface, tooltip=None):
+ self.container = container # Ventana (Rect) que "contiene" al widget
+ self.rect = rect # Rect del widget (relativo al container)
+ self.rect_in_container = pygame.Rect((self.container.left + self.rect.left,
+ self.container.top + self.rect.top),
+ (self.rect.size)) # Rect del widget (absoluto al screen)
+ self.frame_rate = frame_rate
+ self.surface = surface
+
+ # El widget puede (opcionalmente) tener un tooltip
+ self.tooltip = tooltip
+
+ def draw(self, screen):
+ screen.blit(self.surface, self.rect_in_container)
+ return self.rect_in_container
+
+ def force_update(self, screen): # Forzamos la actualizacion del widget independientemente del frame_rate
+ screen.blit(self.surface, self.rect_in_container)
+ pygame.display.update(self.rect_in_container)
+
+
diff --git a/Saludame.activity/window.py b/Saludame.activity/window.py
index 757949f..299a304 100755
--- a/Saludame.activity/window.py
+++ b/Saludame.activity/window.py
@@ -4,193 +4,181 @@ import pygame
import os
import menu_creator
import animation
-import utilities
import status_bars
+from utilities import *
+
BLACK = pygame.Color("black")
BACKGROUND_PATH = os.path.normpath("assets/background/background.png")
class Window:
- def __init__(self, rect, frame_rate, background_color):
- self.rect = rect
+ # Una ventana contiene 'n' widgets
+
+ def __init__(self, container, rect, frame_rate, background, screen, windows_controller):
+ self.container = container
+ self.rect = pygame.Rect(container.left + rect.left, container.top + rect.top, rect.width, rect.height) # Relativo al container
self.frame_rate = frame_rate
+ self.background = background
self.surface = pygame.Surface((rect.width, rect.height))
- self.background_color = background_color
- self.surface.fill(self.background_color)
-
- def draw(self, screen):
- self.surface.fill(self.background_color)
- screen.blit(self.surface, self.rect)
- return [self.rect]
-
-
-class BlinkWindow(Window):
-
- def __init__(self, rect, frame_rate, background_color):
- Window.__init__(self, rect, frame_rate, background_color)
- self.par = True
+ self.screen = screen
+ self.windows_controller = windows_controller
- def draw(self, screen):
- self.par = not self.par
+ self.widgets = [] # Lista de widgets que contiene la ventana
+ self.windows = [] # Lista de ventanas que contiene la ventana
+ self.buttons = [] # Lista de botones que contiene la ventana
- if self.par:
- self.surface.fill(self.background_color)
- else:
- self.surface.fill(BLACK)
-
- screen.blit(self.surface, self.rect)
-
- return [self.rect]
-
-
-class StatusWindow(Window):
+ self.repaint = True
- def __init__(self, rect, frame_rate, background_color):
- self.rect = rect
- self.frame_rate = frame_rate
- self.surface = pygame.Surface(rect.size)
- self.background_color = background_color
-
- self.surface.fill(self.background_color)
-
- self.bars = []
- self.bars.append(IdleStatusBar(pygame.Rect(20, 15, 460, 30), pygame.Color("green")))
- self.bars.append(IdleStatusBar(pygame.Rect(20, 55, 460, 30), pygame.Color("blue"), 35))
- self.bars.append(IdleStatusBar(pygame.Rect(20, 95, 460, 30), pygame.Color("yellow"), 65))
-
- def draw(self, screen):
- self.surface.fill(self.background_color)
-
- for bar in self.bars:
- bar.draw(self.surface)
+ # Abstract function.
+ def pre_draw(self, screen):
+ pass
+
+ # Logica de pintado de cualquier ventana
+ def draw(self, screen, frames):
+
+ self.pre_draw(screen)
+
+ if self.repaint: # Solo actualizamos el fondo de la ventana cuando hagamos un 'repaint'
+ # De otra forma solo actualizamos los widgets y subventanas
+
+ if (isinstance(self.background, tuple) or isinstance(self.background, pygame.Color)):
+ self.surface.fill(self.background) # En este caso background se corresponde con un color
+ else:
+ if (not isinstance(self.background, pygame.Surface)):
+ # Si entramos aca es porque es una imagen que tenemos que convertir
+ self.surface = pygame.image.load(self.background).convert_alpha()
+ else:
+ self.surface = self.background
+
+ screen.blit(self.surface, self.rect) # Pintamos el "fondo" de la ventana
+
+ self.repaint = False
+
+ for widget in self.widgets:
+ if (frames % widget.frame_rate == 0):
+ widget.draw(screen) # Pintamos los widgets que "contiene" la ventana
- screen.blit(self.surface, self.rect)
+ for win in self.windows:
+ if (frames % win.frame_rate == 0):
+ win.draw(screen, frames) # Le decimos a cada ventana que se pinte
- return [self.rect]
-
-
-class StatusBar:
-
- def __init__(self, rect, color, value=0):
- self.rect = rect
- self.color = color
- self.surface = pygame.Surface(rect.size)
- self.value = value
-
-
-class IdleStatusBar(StatusBar):
+ return self.rect
- def __init__(self, rect, color, value=0):
- StatusBar.__init__(self, rect, color, value)
+ def add_child(self, widget):
+ self.widgets.append(widget)
- def draw(self, screen):
- self.value = (self.value + 1) % 101
- factor = self.value / 100.0
+ def add_window(self, window):
+ self.windows.append(window)
+
+ def handle_mouse_down(self, (x, y)):
+ for button in self.buttons:
+ if button.contains_point(x, y):
+ button.on_mouse_click()
- rect = pygame.Rect((0, 0), self.rect.size)
- rect.width *= factor
+ def handle_mouse_over(self, (x, y)):
+ for button in self.buttons:
+ if button.contains_point(x, y):
+ if(not button.over):
+ button.on_mouse_over()
+ button.over = True
+ else:
+ # Ineficiente! Por ahora lo dejo asi para PROBAR
+ # Esta todo el tiempo haciendo esto! Cambiar
+ button.over = False
+ button.on_mouse_out()
+
+class ActionWindow(Window):
+ """
+ Ventana de acciones
+ """
+ def __init__(self, container, rect, frame_rate, background, screen, windows_controller, actions_dictionary):
+
+ self.timing = 1 # la idea de timing es llevar una cuenta adentro, de los frames que fueron pasando
+
+ Window.__init__(self, container, rect, frame_rate, background, screen, windows_controller)
+
+ self.actions_dictionary = actions_dictionary
+ self.background.fill(pygame.Color("blue"))
+ self.on_animation = False
+ self.actual_animation = None
+
+ def play_animation(self, id):
+ self.actual_animation = (self.actions_dictionary[id][0], self.actions_dictionary[id][1])
+ self.on_animation = True
+
+ def pre_draw(self, screen):
+ self.background.fill((0, 0, 255))
+
+ self.timing += 3
+ changes = []
+ if(self.on_animation and self.actual_animation != None):
+ if(self.timing > 12):
+ self.timing = 1
+
+ font = pygame.font.Font(None, 20 + self.timing)
+ self.background.blit(font.render(self.actual_animation[1], 1, (255, 255, 255)), (5, 5 + self.timing))
+ changes += self.actual_animation[0].draw(self.background, self.timing)
- self.surface.fill(BLACK)
- self.surface.fill(self.color, rect)
- screen.blit(self.surface, self.rect)
+ changes += [self.rect]
+ screen.blit(self.background, self.rect)
- return [self.rect]
class KidWindow(Window):
- def __init__(self, rect, frame_rate):
- Window.__init__(self, rect, frame_rate, BLACK)
-
- self.first = True
+ def __init__(self, container, rect, frame_rate, screen, windows_controller):
self.background = pygame.image.load(BACKGROUND_PATH).convert()
+ Window.__init__(self, container, rect, frame_rate, self.background, screen, windows_controller)
- kid_rect = pygame.Rect((100, 20), (350, 480))
- kid_background = self.background.subsurface(kid_rect)
+ kid_rect = pygame.Rect((80, 20), (350, 480))
+ kid_background = self.background.subsurface(kid_rect)
- self.windows = []
- self.windows.append(animation.Kid(kid_rect, kid_background, 1))
-
- def draw(self, screen):
- """
- if self.first:
- # First time blits the entire background
- self.first = False
- screen.blit(self.background, self.rect)
- return [self.rect]
- else:
- # Next blits only the changed areas
- changes = []
- for win in self.windows:
- changes.extend(win.draw(screen))
-
- return changes #[rect.move(self.rect.left, self.rect.top) for rect in changes]
- """
-
- # Temporal para que se vea bien el menu principal
- screen.blit(self.background, self.rect)
- changes = [self.rect]
- for win in self.windows:
- changes.extend(win.draw(screen))
- return changes
+ self.add_window(animation.Kid(rect, kid_rect, 1, kid_background, screen, windows_controller))
-class MainWindow():
+class MainWindow(Window):
- class ChallengesButton(utilities.ImageButton):
-
- def __init__(self, rect, x, y):
- utilities.ImageButton.__init__(self, rect, x, y, "challenges/trophy.png")
- self.frame_rate = 1
-
- def on_mouse_click(self, windows_controller):
- windows_controller.set_active_window("challenges")
-
- class CustomizationButton(utilities.ImageButton):
+ def __init__(self, container, rect, frame_rate, clock, screen, windows_controller):
+ Window.__init__(self, container, rect, frame_rate, (0, 0, 0), screen, windows_controller)
- def __init__(self, rect, x, y):
- utilities.ImageButton.__init__(self, rect, x, y, "customization/palette.png")
- self.frame_rate = 1
-
- def on_mouse_click(self, windows_controller):
- windows_controller.set_active_window("customization")
-
- def __init__(self, clock):
self.name = "main"
self.clock = clock
- self.rect = pygame.Rect(0, 0, 1200, 780)
+
self.windows = [] # Lista de ventanas que 'componen' la ventana principal
- #self.windows.append(BlinkWindow(pygame.Rect((700, 0), (500, 140)), 5, pygame.Color("red")))
- #self.windows.append(BlinkWindow(pygame.Rect((700, 150), (500, 140)), 5, pygame.Color("blue")))
- #self.windows.append(StatusWindow(pygame.Rect((700, 300), (500, 140)), 2, pygame.Color("gray")))
- self.windows.append(KidWindow(pygame.Rect((0, 0), (600, 500)), 1))
- self.windows.append(animation.Apple(pygame.Rect((150, 600), (150, 172)), 10))
- self.windows.append(menu_creator.load_menu())
- self.windows.append(animation.FPS(pygame.Rect((650, 80), (50, 20)), 15, self.clock))
+<<<<<<< HEAD
+ #temporal para probar ActionWindow (se cargará el diccionario en un módulo aparte).
+ self.animations_dic = {'eat_apple': (animation.Apple(pygame.Rect((210, 20), (150, 172)), 10), "Eating an apple!") }
+ self.action_win = ActionWindow(container, pygame.Rect((0, 505), (600, 20)), 10, pygame.Surface((600, 200)), screen, windows_controller, self.animations_dic)
+=======
self.status_bars = status_bars.BarsWindow((0, 0), 1, pygame.Color("gray"))
self.windows.append(self.status_bars)
+>>>>>>> 73f0d4ce24750887870a439c77676d1efa9d84a4
- self.buttons = []
- self.buttons.append(MainWindow.CustomizationButton(self.rect, 10, 650))
- self.buttons.append(MainWindow.ChallengesButton(self.rect, 70, 650))
+ self.windows.append(KidWindow(container, pygame.Rect((0, 0), (600, 500)), 1, screen, windows_controller))
+ #self.windows.append(animation.Apple(pygame.Rect((700, 90), (150, 172)), 10))
+ self.windows.append(menu_creator.load_menu())
+ self.windows.append(animation.FPS(container, pygame.Rect((650, 80), (50, 20)), 15, self.clock))
+ self.windows.append(self.action_win)
+ #self.windows.append(status_bars.BarsWindow((700, 90), 1, pygame.Color("gray")))
- self.windows.extend(self.buttons) # Buttons are drawn too
+ challengesButton = ImageButton(self.rect, pygame.Rect((700, 300), (80, 80)), 1, "challenges/trophy.png", self._cb_button_click_challenges)
+ customizationButton = ImageButton(self.rect, pygame.Rect((700, 400), (80, 80)), 1, "customization/palette.png", self._cb_button_click_customization)
- def handle_mouse_down(self, (x, y), windows_controller):
- # Temporal para probar el manejo de ventanas entre 'challenges' y 'main'
- #windows_controller.set_active_window("challenges")
-
- for button in self.buttons:
- if button.contains_point(x, y):
- button.on_mouse_click(windows_controller)
+ self.buttons.append(challengesButton)
+ self.buttons.append(customizationButton)
- # Temporal para probar BarsWindow
- self.status_bars.on_mouse_click((x,y))
+ for b in self.buttons:
+ self.add_child(b)
+
+ ######## Callbacks buttons ########
- def handle_mouse_over(self, (x, y)):
- None
+ def _cb_button_click_challenges(self, button):
+ self.windows_controller.set_active_window("challenges")
+
+ def _cb_button_click_customization(self, button):
+ #self.windows_controller.set_active_window("customization")
+ pass
- def get_windows(self):
- return self.windows
+ ########################################
diff --git a/Saludame.activity/windowsController.py b/Saludame.activity/windows_controller.py
index 3657022..5b095f3 100755
--- a/Saludame.activity/windowsController.py
+++ b/Saludame.activity/windows_controller.py
@@ -12,11 +12,14 @@ class WindowsController:
self.reload_main = True
def close_active_window(self):
+ self.windows_stack[-1].repaint = True
# Solo puede ser llamado por la ventana activa e implica
# hacer un pop del stack
- self.windows_stack.pop()
+ self.windows_stack.pop()
if (self.windows_stack[-1].name == "main"):
self.reload_main = True
+ for win in self.windows_stack[-1].windows:
+ win.repaint = True
def set_active_window(self, window_key):
self.windows_stack.append(self.windows[window_key])
@@ -25,8 +28,7 @@ class WindowsController:
self.windows[window_key] = window
def handle_mouse_down(self, (x, y)):
- self.windows_stack[-1].handle_mouse_down((x, y), self)
- # Se pasa el controlador a si mismo por si el evento es cerrar la ventana
+ self.windows_stack[-1].handle_mouse_down((x, y))
def handle_mouse_over(self, (x, y)):
self.windows_stack[-1].handle_mouse_over((x, y))
@@ -41,11 +43,9 @@ class WindowsController:
pygame.display.flip() # Actualizamos el screen para hacer visibles los efectos
self.reload_main = False
- changes = []
- # Una ventana puede estar compuesta de varias ventanas a un mismo nivel
- for win in self.windows_stack[-1].get_windows():
- if frames % win.frame_rate == 0:
- changes.extend(win.draw(screen))
+ changes = []
+ if frames % self.windows_stack[-1].frame_rate == 0:
+ changes.extend(self.windows_stack[-1].draw(screen, frames))
if changes:
pygame.display.update(changes)
diff --git a/resources/uml.zargo b/resources/uml.zargo
new file mode 100644
index 0000000..6fbb98b
--- /dev/null
+++ b/resources/uml.zargo
Binary files differ