Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/Manejador_de_Eventos.py
blob: 5e1b5108adda777d9c29d6b7bf7a841baf3e1435 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python
# -*- coding: utf-8 -*-

#   Manejador_de_Eventos.py por:
#   Flavio Danesse <fdanesse@gmail.com>
#   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