Package pilas :: Package actores :: Module temporizador
[hide private]
[frames] | no frames]

Source Code for Module pilas.actores.temporizador

 1  # -*- encoding: utf-8 -*- 
 2  # For Pilas engine - A video game framework. 
 3  # 
 4  # Copyright 2010 - Pablo Garrido 
 5  # License: LGPLv3 (see http://www.gnu.org/licenses/lgpl.html) 
 6  # 
 7  # Website - http://www.pilas-engine.com.ar 
 8  # 
 9   
10  import pilas 
11  from pilas.actores import Texto 
12  from pilas import colores 
13   
14 -class Temporizador(Texto):
15 """Representa un contador de tiempo con cuenta regresiva. 16 17 Por ejemplo: 18 19 >>> t = pilas.actores.Temporizador() 20 >>> def hola_mundo(): 21 ... pilas.avisar("Hola mundo, pasaron 10 segundos...") 22 ... 23 >>> t.ajustar(10, hola_mundo) 24 >>> t.iniciar() 25 26 """
27 - def __init__(self, x=0, y=0, color=colores.negro, fuente=None):
28 """Inicializa el temporizador. 29 30 :param x: Posición horizontal. 31 :param y: Posición vertical. 32 :param color: El color que tendrá el texto. 33 """ 34 Texto.__init__(self, '0', x=x, y=y, fuente=fuente) 35 self.ajustar(1, self.funcion_vacia) 36 self.color = color
37 38 # funcion cuando no se ajusta temporizador
39 - def funcion_vacia(self):
40 pass
41
42 - def definir_tiempo_texto(self, variable):
43 """Define el texto a mostrar en el temporizador. 44 45 :param variable: La cadena de texto a mostrar. 46 """ 47 self.texto = str(variable)
48 49 # con la funcion ajustar manipulamos el tiempo y la 50 # funcion queremos ejecutar
51 - def ajustar(self, tiempo=1, funcion=None):
52 """Indica una funcion para ser invocada en el tiempo indicado. 53 54 La función no tiene que recibir parámetros, y luego de 55 ser indicada se tiene que iniciar el temporizador. 56 """ 57 58 self.tiempo = tiempo 59 self.definir_tiempo_texto(self.tiempo) 60 61 if funcion == None: 62 self.funcion = self.funcion_vacia() 63 else: 64 self.funcion = funcion
65
66 - def _restar_a_contador(self):
67 if self.tiempo != 0: 68 self.tiempo -= 1 69 self.definir_tiempo_texto(self.tiempo) 70 return True
71
72 - def iniciar(self):
73 """Inicia el contador de tiempo con la función indicada.""" 74 pilas.mundo.agregar_tarea_una_vez(self.tiempo, self.funcion) 75 pilas.mundo.agregar_tarea_siempre(1, self._restar_a_contador)
76