diff options
author | dmazzone <mazzone.diego@gmail.com> | 2010-09-21 02:56:02 (GMT) |
---|---|---|
committer | dmazzone <mazzone.diego@gmail.com> | 2010-09-21 02:56:02 (GMT) |
commit | d0d22640264036f3b17eb629b862387f4df8ac30 (patch) | |
tree | 0b8232e27cfe08924a2872a51372e2e40c29d1ff | |
parent | a1e71cac7d637bd3366a170ac683c73f2e4c78a6 (diff) |
Primer acercamiento al control de ventanas
*Cambio en dimensiones de pantalla (1200 x 780)
*Uso de fuentes por defecto
*Módulos en UTF-8
*Stack de ventanas
*Eventos y update solo sobre ventana activa
*Fix imports duplicados o no usados.
game.py quedó bastante más “limpio”.
Por ahora en el tope del stack esta la ventana principal. En caso de querer probar el módulo de desafíos cambiar el orden en que se agregan a la pila (esto más adelante lo va a manejar el “controlador de ventanas”):
windows_stack.append(window.MainWindow(clock)
windows_stack.append(challenges.MultipleChoice(pygame.Rect((200, 150), (800, 400)), 1))
-rw-r--r-- | Saludame.activity/challenges.py | 29 | ||||
-rw-r--r-- | Saludame.activity/game.py | 59 | ||||
-rw-r--r-- | Saludame.activity/utilities.py | 8 | ||||
-rw-r--r-- | Saludame.activity/window.py | 55 |
4 files changed, 88 insertions, 63 deletions
diff --git a/Saludame.activity/challenges.py b/Saludame.activity/challenges.py index a0502c0..81becca 100644 --- a/Saludame.activity/challenges.py +++ b/Saludame.activity/challenges.py @@ -3,7 +3,6 @@ # Modulo de desafios import pygame -import window import os from utilities import * @@ -12,12 +11,13 @@ S_OVER_PATH = os.path.normpath("assets/sound/over.ogg") S_INCORRECT_PATH = os.path.normpath("assets/sound/incorrect.ogg") 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: def __init__(self, rect, frame_rate): self.rect = rect - self.frame_rate = frame_rate - + self.frame_rate = frame_rate self.background = pygame.image.load(I_FRANCIA_PATH).convert() self.question = Text(self.rect.left + 10, self.rect.top + 5, "Cual es la capital de Francia?") @@ -40,6 +40,29 @@ class MultipleChoice: for button in self.buttons: button.draw(screen) return [self.rect] + + def handle_mouse_down(self, (x, y)): + global FIN_MC + for button in self.buttons: # Hardcodeado para probar, despues lo dejo generico + if (button.contains_point(x, y) and not FIN_MC): + fin = button.on_mouse_click() # Fin representa el usuario ya contesto bien o se dio por vencido + if(fin): + FIN_MC = fin + self.buttons = [self.buttons[3], self.buttons[5]] + break # No tiene sentido seguir iterando sobre los botones si ya sabemos cual apreto + + 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: + button.over = False + button.on_mouse_out() + + def get_windows(self): + return [self] class Choice(Button): def __init__(self, rect, x, y, w, h, text): diff --git a/Saludame.activity/game.py b/Saludame.activity/game.py index 726392b..90ad70b 100644 --- a/Saludame.activity/game.py +++ b/Saludame.activity/game.py @@ -3,22 +3,19 @@ import pygame import logging from gettext import gettext as _ -import os import window -import animation -import menu import challenges -import menucreator log = logging.getLogger('saludame') log.setLevel(logging.DEBUG) +""" +Variables globales +""" MAX_FPS = 18 # Max frames per second SLEEP_TIMEOUT = 30 # Seconds until the PauseScreen if no events show up -FIN_MC = False # Toma el valor True cuando finaliza el juego de multiple choice, despues esto se va a cambiar - def main(fromSugar): """Main function of the game. @@ -34,7 +31,7 @@ def main(fromSugar): # Inits PyGame module pygame.init() - target_size = (1000, 700) + target_size = (1200, 780) if not fromSugar: screen = pygame.display.set_mode(target_size) @@ -42,22 +39,18 @@ def main(fromSugar): screen = pygame.display.get_surface() assert screen, "No screen" - pygame.display.update() - + pygame.display.update() + # This clock is used to keep the game at the desired FPS. clock = pygame.time.Clock() + + # Stack de ventanas para el control de venta activa + windows_stack = [] - windows = [] - windows.append(window.BlinkWindow(pygame.Rect((700, 0), (300, 140)), 5, pygame.Color("red"))) - windows.append(window.BlinkWindow(pygame.Rect((700, 150), (300, 140)), 5, pygame.Color("blue"))) - windows.append(window.StatusWindow(pygame.Rect((700, 300), (300, 140)), 2, pygame.Color("gray"))) - windows.append(window.MainWindow(pygame.Rect((0, 0), (600, 500)), 1)) - windows.append(animation.Apple(pygame.Rect((150, 500), (150, 172)), 10)) - windows.append(menucreator.load_menu()) - windows.append(animation.FPS(pygame.Rect((650, 80), (50, 20)), 15, clock)) - - #Challenges Module - windows.append(challenges.MultipleChoice(pygame.Rect((300, 200), (500, 250)), 1)) + # Challenges Window + windows_stack.append(challenges.MultipleChoice(pygame.Rect((200, 150), (800, 400)), 1)) + # Main Window + windows_stack.append(window.MainWindow(clock)) frames = 0 @@ -84,12 +77,12 @@ def main(fromSugar): elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: running = False elif event.type == pygame.MOUSEBUTTONDOWN: - handle_mouse_down(windows, pygame.mouse.get_pos()) + windows_stack[-1].handle_mouse_down(pygame.mouse.get_pos()) # Solo le pasamos los eventos a la ventana activa - handle_mouse_over(windows, pygame.mouse.get_pos()) # Muy ineficiente. Ver como podemos mejorarlo + windows_stack[-1].handle_mouse_over(pygame.mouse.get_pos()) # Solo le pasamos los eventos a la ventana activa changes = [] - for win in windows: + for win in windows_stack[-1].get_windows(): # Solo actualizamos la ventana activa if frames % win.frame_rate == 0: changes.extend(win.draw(screen)) @@ -102,25 +95,5 @@ def main(fromSugar): # Una vez que sale del loop manda la senal de quit para que cierre la ventana pygame.quit() -def handle_mouse_down(windows, (x, y)): - global FIN_MC - for button in windows[7].buttons: # Hardcodeado para probar, despues lo dejo generico - if (button.contains_point(x, y) and not FIN_MC): - fin = button.on_mouse_click() # Fin representa el usuario ya contesto bien o se dio por vencido - if(fin): - FIN_MC = fin - windows[7].buttons = [windows[7].buttons[3], windows[7].buttons[5]] - break # No tiene sentido seguir iterando sobre los botones si ya sabemos cual apreto - -def handle_mouse_over(windows, (x, y)): - for button in windows[7].buttons: - if (button.contains_point(x, y)): - if(not button.over): - button.on_mouse_over() - button.over = True - else: - button.over = False - button.on_mouse_out() - if __name__ == "__main__": main(False) diff --git a/Saludame.activity/utilities.py b/Saludame.activity/utilities.py index ff84707..ec1f24c 100644 --- a/Saludame.activity/utilities.py +++ b/Saludame.activity/utilities.py @@ -1,10 +1,12 @@ +# -*- coding: utf-8 -*- + # Utilitarios import pygame class Text: def __init__(self, x, y, text): - self.font = pygame.font.SysFont("Verdana", 15) + self.font = pygame.font.SysFont(None, 16) self.ren = self.font.render(text, 1, (0, 0, 100)) self.x = x self.y = y @@ -22,7 +24,7 @@ class Button: self.rect = pygame.Rect(rect.left + x, rect.top + y, w, h) self.text = text - self.font = pygame.font.SysFont("Verdana", 15) + self.font = pygame.font.SysFont(None, 16) self.ren = self.font.render(text, 1, (0, 0, 100)) self.background_color = (255, 0, 0) @@ -33,7 +35,7 @@ class Button: def draw(self, screen): screen.fill(self.background_color, self.rect) - screen.blit(self.ren, (self.rect.left + 5, self.rect.top + 1)) + screen.blit(self.ren, (self.rect.left + 5, self.rect.top + 5)) def set_background_color(self, color): self.background_color = color diff --git a/Saludame.activity/window.py b/Saludame.activity/window.py index 5f7ea67..e0d67f1 100644 --- a/Saludame.activity/window.py +++ b/Saludame.activity/window.py @@ -2,8 +2,11 @@ import pygame import os +import menucreator +import animation BLACK = pygame.Color("black") +BACKGROUND_PATH = os.path.normpath("assets/background/background.png") class Window: @@ -11,8 +14,7 @@ class Window: self.rect = rect self.frame_rate = frame_rate self.surface = pygame.Surface((rect.width, rect.height)) - self.background_color = background_color - + self.background_color = background_color self.surface.fill(self.background_color) def draw(self, screen): @@ -51,9 +53,9 @@ class StatusWindow(Window): self.surface.fill(self.background_color) self.bars = [] - self.bars.append(IdleStatusBar(pygame.Rect(20, 15, 260, 30), pygame.Color("green"))) - self.bars.append(IdleStatusBar(pygame.Rect(20, 55, 260, 30), pygame.Color("blue"), 35)) - self.bars.append(IdleStatusBar(pygame.Rect(20, 95, 260, 30), pygame.Color("yellow"), 65)) + 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) @@ -68,7 +70,7 @@ class StatusWindow(Window): class StatusBar: - def __init__(self, rect, color, value = 0): + def __init__(self, rect, color, value=0): self.rect = rect self.color = color self.surface = pygame.Surface(rect.size) @@ -77,14 +79,14 @@ class StatusBar: class IdleStatusBar(StatusBar): - def __init__(self, rect, color, value = 0): + def __init__(self, rect, color, value=0): StatusBar.__init__(self, rect, color, value) def draw(self, screen): self.value = (self.value + 1) % 101 factor = self.value / 100.0 - rect = pygame.Rect((0,0), self.rect.size) + rect = pygame.Rect((0, 0), self.rect.size) rect.width *= factor self.surface.fill(BLACK) @@ -93,11 +95,7 @@ class IdleStatusBar(StatusBar): return [self.rect] -import animation - -BACKGROUND_PATH = os.path.normpath("assets/background/background.png") - -class MainWindow(Window): +class KidWindow(Window): def __init__(self, rect, frame_rate): Window.__init__(self, rect, frame_rate, BLACK) @@ -106,7 +104,7 @@ class MainWindow(Window): self.background = pygame.image.load(BACKGROUND_PATH).convert() - kid_rect = pygame.Rect((100, 20),(350,480)) + kid_rect = pygame.Rect((100, 20), (350, 480)) kid_background = self.background.subsurface(kid_rect) self.windows = [] @@ -126,3 +124,32 @@ class MainWindow(Window): changes.extend(win.draw(screen)) return changes #[rect.move(self.rect.left, self.rect.top) for rect in changes] + +class MainWindow(): + + def __init__(self, clock): + self.clock = clock + 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, 500), (150, 172)), 10)) + self.windows.append(menucreator.load_menu()) + self.windows.append(animation.FPS(pygame.Rect((650, 80), (50, 20)), 15, self.clock)) + + def draw(self): + changes = [] + for win in self.windows: + changes += win.draw() + return changes + + + def handle_mouse_down(self, (x, y)): + None + + def handle_mouse_over(self, (x, y)): + None + + def get_windows(self): + return self.windows |