Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authordmazzone <mazzone.diego@gmail.com>2010-09-19 06:15:14 (GMT)
committer dmazzone <mazzone.diego@gmail.com>2010-09-19 06:15:14 (GMT)
commitfeb0f807671ba215d5d1478168f54456c30ffb32 (patch)
treeaab509abec863da0b8d08dde61e3054214978045
parenta2a543085ec62ccf0cfb14aaddbc752d1c64cc54 (diff)
Módulo de desafíos.
Se agregaron un par de clases 'utilitarias' como Text (para representar un texto) y Button (para representar un botón). La clase Button es una clase abstracta. Las coordenadas de los botones son relativas a la 'ventana' que los contiene. Se agregó una clase MultipleChoice que representa un primer 'juego desafío'. Los principales componentes de dicha clase son objetos "Choice" que heredan el comportamiento de 'Button' sobreescribiendo los eventos producidos por el mouse (on_mouse_click(), on_mouseOver(), etc...) El código está comentado. Hay varias cosas 'hardcodeadas' que luego pasarían a ser genéricas. Utilicé una variable global FIN_MC (temporalmente) para saber cuando el 'desafío' finalizó (ya sea porque contestó bien o porque se dio por vencido). Detalles menores: * Se agregaron sonidos e imágenes. Los sonidos no están muy buenos pero los usé para probar no más.
-rw-r--r--Saludame.activity/animation.py1
-rw-r--r--Saludame.activity/assets/challenges/francia.jpgbin0 -> 10428 bytes
-rw-r--r--Saludame.activity/assets/sound/correct.oggbin0 -> 26925 bytes
-rw-r--r--Saludame.activity/assets/sound/incorrect.oggbin0 -> 9851 bytes
-rw-r--r--Saludame.activity/assets/sound/over.oggbin0 -> 6994 bytes
-rw-r--r--Saludame.activity/challenges.py86
-rw-r--r--Saludame.activity/game.py50
-rw-r--r--[-rwxr-xr-x]Saludame.activity/menu.py0
-rw-r--r--Saludame.activity/utilities.py50
9 files changed, 175 insertions, 12 deletions
diff --git a/Saludame.activity/animation.py b/Saludame.activity/animation.py
index 9460261..98ec8f6 100644
--- a/Saludame.activity/animation.py
+++ b/Saludame.activity/animation.py
@@ -93,7 +93,6 @@ class Apple(Food):
def __init__(self, rect, frame_rate):
Food.__init__(self, APPLE_PATH, rect, frame_rate)
-
class FPS:
def __init__(self, rect, frame_rate, clock):
diff --git a/Saludame.activity/assets/challenges/francia.jpg b/Saludame.activity/assets/challenges/francia.jpg
new file mode 100644
index 0000000..412fbbc
--- /dev/null
+++ b/Saludame.activity/assets/challenges/francia.jpg
Binary files differ
diff --git a/Saludame.activity/assets/sound/correct.ogg b/Saludame.activity/assets/sound/correct.ogg
new file mode 100644
index 0000000..86539d8
--- /dev/null
+++ b/Saludame.activity/assets/sound/correct.ogg
Binary files differ
diff --git a/Saludame.activity/assets/sound/incorrect.ogg b/Saludame.activity/assets/sound/incorrect.ogg
new file mode 100644
index 0000000..182227e
--- /dev/null
+++ b/Saludame.activity/assets/sound/incorrect.ogg
Binary files differ
diff --git a/Saludame.activity/assets/sound/over.ogg b/Saludame.activity/assets/sound/over.ogg
new file mode 100644
index 0000000..9a1da65
--- /dev/null
+++ b/Saludame.activity/assets/sound/over.ogg
Binary files differ
diff --git a/Saludame.activity/challenges.py b/Saludame.activity/challenges.py
new file mode 100644
index 0000000..a0502c0
--- /dev/null
+++ b/Saludame.activity/challenges.py
@@ -0,0 +1,86 @@
+# -*- coding: utf-8 -*-
+
+# Modulo de desafios
+
+import pygame
+import window
+import os
+from utilities import *
+
+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")
+I_FRANCIA_PATH = os.path.normpath("assets/challenges/francia.jpg")
+
+class MultipleChoice:
+
+ def __init__(self, rect, frame_rate):
+ self.rect = rect
+ 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?")
+
+ self.buttons = []
+ self.buttons += [Choice(self.rect, 20, 40, 200, 20, "Buenos Aires")]
+ self.buttons += [Choice(self.rect, 20, 70, 200, 20, "Oslo")]
+ self.buttons += [Choice(self.rect, 20, 100, 200, 20, "Roma")]
+ self.buttons += [Choice(self.rect, 20, 130, 200, 20, "Paris")]
+ self.buttons += [Choice(self.rect, 20, 160, 200, 20, "Moscu")]
+
+ self.btn_view_answer = ViewAnswer(self.rect, 20, 200, 200, 20, "Me doy por vencido! :(...", self.buttons[3])
+ self.buttons += [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_view_answer.draw(screen)
+ for button in self.buttons:
+ button.draw(screen)
+ return [self.rect]
+
+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.
+ """
+
+ def on_mouse_click(self):
+ if(self.y == 130):
+ self.s_correct.play()
+ return 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 on_mouse_over(self):
+ self.s_over.play()
+ self.set_background_color((45, 255, 100))
+
+ def on_mouse_out(self):
+ self.set_background_color((20, 100, 45))
diff --git a/Saludame.activity/game.py b/Saludame.activity/game.py
index 52f781d..5e4f3c7 100644
--- a/Saludame.activity/game.py
+++ b/Saludame.activity/game.py
@@ -1,7 +1,5 @@
# -*- coding: utf-8 -*-
-####
-
import pygame
import logging
from gettext import gettext as _
@@ -10,6 +8,7 @@ import os
import window
import animation
import menu
+import challenges
log = logging.getLogger('saludame')
log.setLevel(logging.DEBUG)
@@ -17,6 +16,8 @@ log.setLevel(logging.DEBUG)
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.
@@ -46,13 +47,16 @@ def main(fromSugar):
clock = pygame.time.Clock()
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(menu.Menu(pygame.Rect((150, 500),(150,172)), 1))
- windows.append(animation.FPS(pygame.Rect((650, 80),(50,20)), 15, clock))
+ 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(menu.Menu(pygame.Rect((150, 500), (150, 172)), 1))
+ 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))
frames = 0
@@ -68,9 +72,9 @@ def main(fromSugar):
# Waits for events, if none the game pauses:
# http://wiki.laptop.org/go/Game_development_HOWTO#Reducing_CPU_Load
- milliseconds = clock.tick(MAX_FPS) # waits if the game is running faster than MAX_FPS
+ milliseconds = clock.tick(MAX_FPS) # waits if the game is running faster than MAX_FPS
- events = pygame.event.get()
+ events = pygame.event.get()
if events:
for event in events:
@@ -78,6 +82,10 @@ def main(fromSugar):
running = False
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())
+
+ handle_mouse_over(windows, pygame.mouse.get_pos()) # Muy ineficiente. Ver como podemos mejorarlo
changes = []
for win in windows:
@@ -92,6 +100,26 @@ 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/menu.py b/Saludame.activity/menu.py
index 63a1afb..63a1afb 100755..100644
--- a/Saludame.activity/menu.py
+++ b/Saludame.activity/menu.py
diff --git a/Saludame.activity/utilities.py b/Saludame.activity/utilities.py
new file mode 100644
index 0000000..ff84707
--- /dev/null
+++ b/Saludame.activity/utilities.py
@@ -0,0 +1,50 @@
+# Utilitarios
+
+import pygame
+
+class Text:
+ def __init__(self, x, y, text):
+ self.font = pygame.font.SysFont("Verdana", 15)
+ self.ren = self.font.render(text, 1, (0, 0, 100))
+ self.x = x
+ self.y = y
+
+ def draw(self, screen):
+ screen.blit(self.ren, (self.x, self.y))
+
+
+class Button:
+
+ # 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
+
+ self.font = pygame.font.SysFont("Verdana", 15)
+ self.ren = self.font.render(text, 1, (0, 0, 100))
+
+ self.background_color = (255, 0, 0)
+ self.over = False
+
+ def contains_point(self, x, y):
+ return self.rect.collidepoint(x, y)
+
+ def draw(self, screen):
+ screen.fill(self.background_color, self.rect)
+ screen.blit(self.ren, (self.rect.left + 5, self.rect.top + 1))
+
+ def set_background_color(self, color):
+ self.background_color = color
+
+ # Eventos sobre el boton... seran sobreescritos por los hijos
+
+ def on_mouse_clik(self):
+ None
+
+ def on_mouse_over(self):
+ None
+
+ def on_mouse_out(self):
+ None