Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/menu.py
diff options
context:
space:
mode:
Diffstat (limited to 'menu.py')
-rwxr-xr-xmenu.py153
1 files changed, 153 insertions, 0 deletions
diff --git a/menu.py b/menu.py
new file mode 100755
index 0000000..bd8c9bc
--- /dev/null
+++ b/menu.py
@@ -0,0 +1,153 @@
+# -*- coding: utf-8 -*-
+# filename: menu.py
+#
+# Ceibal Rol
+# Copyright (C) 2008,2009 Gabriel Balbuena
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+# Contact information:
+# Ceibal Jam http://ceibaljam.org
+
+
+import random
+import pygame
+from pygame.locals import *
+
+class Logo():
+ " Representa el logo principal de el juego y sus propiedades "
+ def __init__(self):
+ pass
+
+
+class Menu:
+ "Representa un menu con opciones para el juego"
+ def __init__(self, opciones):
+ self.opciones = []
+ fuente = pygame.font.Font('./data/fonts/slammertag.ttf', 36)
+ x = 405
+ y = 305
+ paridad = 1
+
+ self.cursor = Cursor(x - 30, y, 30)
+
+ for titulo, funcion in opciones:
+ self.opciones.append(Opcion(fuente, titulo, x, y, paridad, funcion))
+ y += 30
+ if paridad == 1:
+ paridad = -1
+ else:
+ paridad = 1
+
+ self.seleccionado = 0
+ self.total = len(self.opciones)
+ self.mantiene_pulsado = False
+
+ def update(self):
+ """Altera el valor de 'self.seleccionado' con los direccionales."""
+ k = pygame.key.get_pressed()
+
+ if not self.mantiene_pulsado:
+ if k[K_UP]:
+ self.seleccionado -= 1
+ elif k[K_DOWN]:
+ self.seleccionado += 1
+ elif k[K_RETURN]:
+ # Invoca a la función asociada a la opcion.
+ self.opciones[self.seleccionado].activar()
+
+ # procura que el cursor este entre las opciones permitidas
+ if self.seleccionado < 0:
+ self.seleccionado = 0
+ elif self.seleccionado > self.total - 1:
+ self.seleccionado = self.total - 1
+
+ self.cursor.seleccionar(self.seleccionado)
+
+ # indica si el usuario mantiene pulsada alguna tecla.
+ self.mantiene_pulsado = k[K_UP] or k[K_DOWN] or k[K_RETURN]
+
+ self.cursor.actualizar()
+
+ for o in self.opciones:
+ o.actualizar()
+
+ def _print(self, screen):
+ """Imprime sobre 'screen' el texto de cada opción del menú."""
+
+ self.cursor.imprimir(screen)
+
+ for opcion in self.opciones:
+ opcion.imprimir(screen)
+
+
+
+class Opcion:
+
+ def __init__(self, fuente, titulo, x, y, paridad, funcion_asignada):
+ self.imagen_normal = fuente.render(titulo, 1, (0, 0, 0))
+ self.imagen_destacada = fuente.render(titulo, 1, (200, 0, 0))
+ self.image = self.imagen_normal
+ self.rect = self.image.get_rect()
+ self.rect.x = 500 * paridad
+ self.rect.y = y
+ self.funcion_asignada = funcion_asignada
+ self.x = float(self.rect.x)
+
+ def actualizar(self):
+ destino_x = 405
+ self.x += (destino_x - self.x) / 5.0
+ self.rect.x = int(self.x)
+
+ def imprimir(self, screen):
+ screen.blit(self.image, self.rect)
+
+ def destacar(self, estado):
+ if estado:
+ self.image = self.imagen_destacada
+ else:
+ self.image = self.imagen_normal
+
+ def activar(self):
+ self.funcion_asignada()
+
+
+class Cursor:
+
+ def __init__(self, x, y, dy):
+ self.image = pygame.image.load('./data/menu/bichito.png').convert_alpha()
+
+ self.rect = self.image.get_rect()
+ self.rect.x = x -60
+
+ self.y_inicial = y -40
+
+ self.dy = dy
+
+ self.y = 0
+
+ self.seleccionar(0)
+
+ def actualizar(self):
+ self.y += (self.to_y - self.y) / 10.0
+ self.rect.y = int(self.y)
+
+ def seleccionar(self, indice):
+ self.to_y = self.y_inicial + indice * self.dy
+
+ def imprimir(self, screen):
+ screen.blit(self.image, self.rect)
+
+
+