From 62eae792b0b392cf111dc95aab3ee520ca9c58f1 Mon Sep 17 00:00:00 2001 From: Mateu Batle Date: Tue, 23 Nov 2010 15:33:31 +0000 Subject: Implemented cookie system to avoid creation of many objects --- diff --git a/game1/collectgame.py b/game1/collectgame.py index 50ba314..04748ce 100755 --- a/game1/collectgame.py +++ b/game1/collectgame.py @@ -11,13 +11,14 @@ from gettext import gettext as _ class CollectGame(usmpgames.ApplicationState): - def __init__(self, game_mode, next_state = None, background = None): - usmpgames.ApplicationState.__init__(self, next_state, background) + def __init__(self, game_mode, next_state = None, background = None, next_cookie = None): + usmpgames.ApplicationState.__init__(self, next_state, background, next_cookie) self.game_mode = game_mode self.font = pygame.font.SysFont(None, 80) - def entering_state(self, fromStack): - usmpgames.ApplicationState.entering_state(self, fromStack) + def entering_state(self, fromStack, cookie): + usmpgames.ApplicationState.entering_state(self, fromStack, cookie) + self.game_mode = cookie if (not fromStack) : self.track = Track() self.player = Player(self.game_mode) diff --git a/game2/game2.py b/game2/game2.py index 30a9a54..8294c7e 100755 --- a/game2/game2.py +++ b/game2/game2.py @@ -103,8 +103,8 @@ n = 0 class Game2(usmpgames.ApplicationState): - def __init__(self, game_mode, next_state = None, background_image = None): - usmpgames.ApplicationState.__init__(self, next_state, background_image) + def __init__(self, game_mode, next_state = None, background_image = None, next_cookie = None): + usmpgames.ApplicationState.__init__(self, next_state, background_image, next_cookie) self.game_mode = game_mode global gameOverSound, correctAnswerSound, musicSound @@ -130,12 +130,12 @@ class Game2(usmpgames.ApplicationState): self.new_operation = True - def entering_state(self, fromStack): - usmpgames.ApplicationState.entering_state(self, fromStack) + def entering_state(self, fromStack, cookie): + usmpgames.ApplicationState.entering_state(self, fromStack, cookie) global n global GAMEMODE n = 0 - GAMEMODE = self.game_mode + GAMEMODE = cookie #Get initial time global TIMESTART TIMESTART= round(time.time(),0) diff --git a/mathquwy.py b/mathquwy.py index c41a808..ff5ccb7 100755 --- a/mathquwy.py +++ b/mathquwy.py @@ -24,12 +24,13 @@ class MathQuwy(usmpgames.Application): congrats = usmpgames.InfoState( None, background_congrats ) # even numbers game - even_numbers_game = game1.collectgame.CollectGame( "pares", congrats ) - odd_numbers_game = game1.collectgame.CollectGame( "impares", congrats ) + #even_numbers_game = game1.collectgame.CollectGame( "pares", congrats ) + #odd_numbers_game = game1.collectgame.CollectGame( "impares", congrats ) + evenodd_game = game1.collectgame.CollectGame( "", congrats ) # even numbers game state (tutorial) background_tutorial = "data/backgrounds/tutorial.jpg" - even_numbers_tutorial = usmpgames.InfoState( even_numbers_game, background_tutorial ) + even_numbers_tutorial = usmpgames.InfoState( evenodd_game, background_tutorial, "pares" ) even_numbers_tutorial.add_text2( _("""Instrucciones Juego Pares\n\nSuma puntos cogiendo las frutas\ncon numeros pares.\n\nEvita tocar las frutas con numeros\nimpares o te restaran puntos.\n\nMueve al cuy Quwy con las fechas.\n\nEvita los peligros como el agua y\notros animales.\n\nPulsa una tecla para jugar."""), color = (0, 0, 0, 0), @@ -37,7 +38,7 @@ class MathQuwy(usmpgames.Application): rectsize = (380, 390)); # odd numbers game state (tutorial) - odd_numbers_tutorial = usmpgames.InfoState( odd_numbers_game, background_tutorial ) + odd_numbers_tutorial = usmpgames.InfoState( evenodd_game, background_tutorial, "impares" ) odd_numbers_tutorial.add_text2( _(""" Instrucciones Juego Impares\n\nSuma puntos cogiendo las frutas\ncon numeros impares.\n\nEvita tocar las frutas con numeros\npares o te restaran puntos.\n\nMueve al cuy Quwy con las fechas.\n\nEvita los peligros como el agua y\notros animales.\n\nPulsa una tecla para jugar."""), color = (0, 0, 0, 0), @@ -45,13 +46,14 @@ class MathQuwy(usmpgames.Application): rectsize = (380, 390)); # add game - add_game = game2.Game2( 1, congrats ) - substract_game = game2.Game2( 2, congrats ) - multiply_game = game2.Game2( 3, congrats ) - count_game = game2.Game2( 4, congrats ) + #add_game = game2.Game2( 1, congrats ) + #substract_game = game2.Game2( 2, congrats ) + #multiply_game = game2.Game2( 3, congrats ) + #count_game = game2.Game2( 4, congrats ) + allgame2 = game2.Game2( 0, congrats ) # add numbers game state (tutorial) - add_tutorial = usmpgames.InfoState( add_game, background_tutorial ) + add_tutorial = usmpgames.InfoState( allgame2, background_tutorial, 1 ) add_tutorial.add_text2( _(""" Instrucciones Juego Sumar\n\nCalcula el resultado de la suma.\nSuma puntos cogiendo las frutas\ncon el resultado correcto de la suma.\n\nEvita tocar las frutas con resultados\nincorrectos o te restaran puntos.\n\nMueve al cuy Quwy a la izquierda\ny derecha con las fechas.\n\nPulsa una tecla para jugar."""), color = (0, 0, 0, 0), @@ -59,7 +61,7 @@ class MathQuwy(usmpgames.Application): rectsize = (380, 390)); # substract numbers game state (tutorial) - substract_tutorial = usmpgames.InfoState( substract_game, background_tutorial ) + substract_tutorial = usmpgames.InfoState( allgame2, background_tutorial, 2 ) substract_tutorial.add_text2( _(""" Instrucciones Juego Restar\n\nCalcula el resultado de la resta.\nSuma puntos cogiendo las frutas\ncon el resultado correcto de la resta.\n\nEvita tocar las frutas con resultados\nincorrectos o te restaran puntos.\n\nMueve al cuy Quwy a la izquierda\ny derecha con las fechas.\n\nPulsa una tecla para jugar."""), color = (0, 0, 0, 0), @@ -67,7 +69,7 @@ class MathQuwy(usmpgames.Application): rectsize = (380, 390)); # multiply numbers game state (tutorial) - multiply_tutorial = usmpgames.InfoState( multiply_game, background_tutorial ) + multiply_tutorial = usmpgames.InfoState( allgame2, background_tutorial, 3 ) multiply_tutorial.add_text2( _(""" Instrucciones Juego Multiplicar\n\nCalcula el resultado de la multiplicacion.\nSuma puntos cogiendo las frutas\ncon el resultado correcto de la\nmultiplicacion.\n\nEvita tocar las frutas con resultados\nincorrectos o te restaran puntos.\n\nMueve al cuy Quwy a la izquierda\ny derecha con las fechas.\n\nPulsa una tecla para jugar."""), color = (0, 0, 0, 0), @@ -75,7 +77,7 @@ class MathQuwy(usmpgames.Application): rectsize = (380, 390)); # count game state (tutorial) - count_tutorial = usmpgames.InfoState( count_game, background_tutorial ) + count_tutorial = usmpgames.InfoState( allgame2, background_tutorial, 4 ) count_tutorial.add_text2( _(""" Instrucciones Juego Contar\n\nCuentael numero de animales.\nSuma puntos cogiendo las frutas\ncon el resultado correcto.\n\nEvita tocar las frutas con resultados\nincorrectos o te restaran puntos.\n\nMueve al cuy Quwy a la izquierda\ny derecha con las fechas.\n\nPulsa una tecla para jugar."""), color = (0, 0, 0, 0), diff --git a/usmpgames/application.py b/usmpgames/application.py index d492b5c..7871ee8 100755 --- a/usmpgames/application.py +++ b/usmpgames/application.py @@ -28,7 +28,9 @@ class Application(): def __init__(self): self._state_stack = [] + self._cookie_stack = [] self._current_state = None + self._current_cookie = None self._running = True self._fps = 25 if Application._instance is None: @@ -40,12 +42,13 @@ class Application(): def screen(self): return self._screen - def push_state(self, new_state): + def push_state(self, new_state, new_cookie = None): # exit from current state if self._current_state is not None: self._current_state.exiting_state( True ) if new_state is not None: self._state_stack.append(self._current_state) + self._cookie_stack.append(self._current_state.cookie()) # process new state fromStack = False @@ -55,18 +58,20 @@ class Application(): return else : self._current_state = self.pop_state() + self._current_cookie = self._current_state.cookie() fromStack = True else: self._current_state = new_state + self._current_cookie = new_cookie # entering new state if self._current_state is None: self.set_running(False) else: self._current_state.set_screen(self.screen()) - self._current_state.entering_state(fromStack) + self._current_state.entering_state(fromStack, self._current_cookie) - def change_state(self, new_state): + def change_state(self, new_state, new_cookie): # exit from current state if self._current_state is not None: self._current_state.exiting_state(False) @@ -81,11 +86,12 @@ class Application(): return else: self._current_state = new_state + self._current_cookie = new_cookie # entering new state if self._current_state is not None: self._current_state.set_screen(self.screen()) - self._current_state.entering_state( False ) + self._current_state.entering_state( False, self._current_cookie ) else: self.set_running(False) @@ -96,11 +102,13 @@ class Application(): self._current_state.exiting_state( False ) if len(self._state_stack) >= 1: self._current_state = self._state_stack.pop() + self._current_cookie = self._current_state.cookie() else: self._current_state = None + self._current_cookie = None if self._current_state is not None: self._current_state.set_screen(self.screen()) - self._current_state.entering_state( True ) + self._current_state.entering_state( True, self._current_cookie ) def current_state(self): return self._current_state diff --git a/usmpgames/applicationstate.py b/usmpgames/applicationstate.py index 6f6439b..ff8613b 100755 --- a/usmpgames/applicationstate.py +++ b/usmpgames/applicationstate.py @@ -6,12 +6,14 @@ from application import * class ApplicationState(): - def __init__(self, next_state = None, background_name = None): + def __init__(self, next_state = None, background_name = None, next_cookie = None): self._running = True self._screen = None self._next_state = next_state + self._next_cookie = next_cookie self._background_name = background_name self._background = None + self._cookie = None def input(self, ms): pass @@ -56,17 +58,22 @@ class ApplicationState(): self.render(ms) self.post_render(ms) - def entering_state(self, fromStack): + def entering_state(self, fromStack, cookie): print "Entering state ", self self.set_running(True) + self._cookie = cookie def exiting_state(self, toStack): print "Exiting state ", self + #self._cookie = None pass def go_to_next_state(self): - Application.instance().change_state( self._next_state ) + Application.instance().change_state( self._next_state, self._next_cookie ) def next_state(self): return self._next_state + def cookie(self): + return self._cookie + diff --git a/usmpgames/infostate.py b/usmpgames/infostate.py index 9bf46ee..23560b6 100755 --- a/usmpgames/infostate.py +++ b/usmpgames/infostate.py @@ -13,8 +13,8 @@ import pygame.font class InfoState(ApplicationState): - def __init__(self, next_state, background = None): - ApplicationState.__init__(self, next_state, background) + def __init__(self, next_state, background = None, next_cookie = None): + ApplicationState.__init__(self, next_state, background, next_cookie) self._images = [] self._ktexts = [] @@ -67,7 +67,7 @@ class InfoState(ApplicationState): for info in self._ktexts: info.draw(self.screen()) - def entering_state(self, fromStack): - ApplicationState.entering_state(self, fromStack) + def entering_state(self, fromStack, cookie): + ApplicationState.entering_state(self, fromStack, cookie) pygame.time.wait(500) pygame.event.clear() diff --git a/usmpgames/menustate.py b/usmpgames/menustate.py index 325e4a4..fa20ad0 100755 --- a/usmpgames/menustate.py +++ b/usmpgames/menustate.py @@ -7,8 +7,8 @@ from infostate import * class MenuState(InfoState): - def __init__(self, background = None): - InfoState.__init__(self, None, background) + def __init__(self, background = None, cookie = None): + InfoState.__init__(self, None, background, cookie) self._menu_options = [] self._menu_states = [] self.pos = ('center', 'center') @@ -41,5 +41,5 @@ class MenuState(InfoState): def input(self, ms): pass - def entering_state(self, fromStack): - ApplicationState.entering_state(self, fromStack) + def entering_state(self, fromStack, cookie): + ApplicationState.entering_state(self, fromStack, cookie) -- cgit v0.9.1