#!/usr/bin/env python # -*- coding: utf-8 -*- # Manejador_de_Eventos.py por: # Flavio Danesse # CeibalJAM! - Uruguay - Plan Ceibal # # 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 2 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, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA import pygame from pygame.locals import * import gc gc.enable() class Manejador_de_Eventos(): ''' Maneja todos los eventos del juego. ''' def __init__(self): pass def setup(self, Jugador_JAMtank=None, Enlace_Red=None): # Configurar objetos de eventos para el juego y asociarlos al jugador local self.red = Enlace_Red print "Seteando Eventos de Usuario para JAMtank" self.protagonista = Jugador_JAMtank pygame.event.set_blocked([MOUSEMOTION, MOUSEBUTTONUP, MOUSEBUTTONDOWN, JOYAXISMOTION, JOYBALLMOTION, JOYHATMOTION, JOYBUTTONUP, JOYBUTTONDOWN, KEYUP, VIDEORESIZE, VIDEOEXPOSE, USEREVENT, QUIT, ACTIVEEVENT]) # bloqueados pygame.event.set_allowed([KEYDOWN]) # permitidos pygame.key.set_repeat(15, 15) # repeticiĆ³n de teclas def detectar(self): # detecta si hay eventos hayTeclas = False for event in pygame.event.get(): if event.type == pygame.KEYDOWN or event.type == pygame.KEYUP: hayTeclas = True if hayTeclas: self.Manejar_Eventos() pygame.event.clear() def Manejar_Eventos(self): # Maneja los eventos del usuario teclas = pygame.key.get_pressed() # girar en movimiento if teclas[pygame.K_UP] and teclas[pygame.K_RIGHT]:# adelante - derecha self.protagonista.evento(pygame.K_UP) self.protagonista.evento(pygame.K_RIGHT) elif teclas[pygame.K_UP] and teclas[pygame.K_LEFT]:# adelante - izquierda self.protagonista.evento(pygame.K_UP) self.protagonista.evento(pygame.K_LEFT) elif teclas[pygame.K_DOWN] and teclas[pygame.K_RIGHT]:# atras - derecha self.protagonista.evento(pygame.K_DOWN) self.protagonista.evento(pygame.K_LEFT) elif teclas[pygame.K_DOWN] and teclas[pygame.K_LEFT]:# atras - izquierda self.protagonista.evento(pygame.K_DOWN) self.protagonista.evento(pygame.K_RIGHT) # moverse sin girar elif teclas[pygame.K_UP] and not teclas[pygame.K_DOWN]:# adelante self.protagonista.evento(pygame.K_UP) elif teclas[pygame.K_DOWN] and not teclas[pygame.K_UP]:# atras self.protagonista.evento(pygame.K_DOWN) # girar sin moverse elif teclas[pygame.K_RIGHT] and not teclas[pygame.K_LEFT]:# derecha self.protagonista.evento(pygame.K_RIGHT) elif teclas[pygame.K_LEFT] and not teclas[pygame.K_RIGHT]:# izquierda self.protagonista.evento(pygame.K_LEFT) # Disparar. if teclas[pygame.K_LCTRL]: self.protagonista.evento(pygame.K_LCTRL) # salir elif teclas[pygame.K_ESCAPE]: print "*** Cerrando pygame y Saliendo del Juego ***" self.red.desconectarse() pygame.quit() import sys sys.exit() else: pass