Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/BiblioJAM/JAMFire.py
blob: fc609838fcb72dfe4391045b65defa1b2de0228f (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env python
# -*- coding: utf-8 -*-

#   BiblioJAM (Versión 2.0) - 31/05/2011 - CeibalJAM! - Uruguay
#   JAMFire.py por: Flavio Danesse fdanesse@gmail.com
#   https://sites.google.com/site/flaviodanesse/
#   https://sites.google.com/site/sugaractivities/
#   http://codigosdeejemplo.blogspot.com/

import pygame, gc, sys, os
from pygame.locals import *
gc.enable()

import JAMGlobals as VG

class JAMFire(pygame.sprite.OrderedUpdates):
	''' Efecto Fuego. '''
	def __init__(self):
		pygame.sprite.OrderedUpdates.__init__(self)
		self.posicion= (0,0)
		self.latencia= 6
		self.disminucion= 5
		self.deformacion= (0,0)

		self.fuego= Fuego(self) 
		self.add(self.fuego)

	def set_posicion(self, punto= (0,0)):
		''' Setea la posición desde donde se dibujará el efecto, es decir que punto es la base del fuego.'''
		if type(punto)== tuple:
			if len(punto)== 2 and type(punto[0])==int and type(punto[1]):
				self.fuego.rect.x= punto[0]-self.fuego.rect.w/2
				self.fuego.rect.y= punto[1]-self.fuego.rect.h
				self.posicion= punto

	def set_deforma_fire(self, valor):
		''' Divide por valor el tamaño del fuego. '''
		if type(valor)== tuple:
			if len(valor)==2 and type(valor[0])== int and type(valor[1])== int:
				self.deformacion= valor
				self.fuego.deforma_imagenes()
				self.set_posicion(punto= self.posicion)

	def set_disminuye_fire(self, valor):
		''' Divide por valor el tamaño del fuego. '''
		if type(valor)== int and valor > 0:
			self.disminucion= valor
			self.fuego.load_imagenes()
			self.set_posicion(punto= self.posicion)

	def set_latencia(self, latencia):
		''' Setea la velocidad de secuenciación de imágenes para el fuego. '''
		if type(latencia)== int: self.latencia= latencia

	def get_tamanio(self):
		''' Devuelve el tamanio actual del fuego. '''
		return (self.fuego.rect.w, self.fuego.rect.h)

class Fuego(pygame.sprite.Sprite):
	''' El Sprite para el efecto. '''
	def __init__(self, fire):
		pygame.sprite.Sprite.__init__(self)
		self.fire= fire
		self.imagenes= []
		self.indice_actual= 0
		self.image= None
		self.rect= None
		self.contador= 0
		self.load_imagenes()

	def load_imagenes(self):
		''' carga las imagenes ajustando su tamaño según el indice de disminución especificado. '''
		self.imagenes= []
		for imagen in VG.get_fire():
			tam= imagen.get_size()
			im= pygame.transform.scale(imagen, (tam[0]/self.fire.disminucion,tam[1]/self.fire.disminucion)) 
			self.imagenes.append(im)
		self.indice_actual= 0
		self.image= self.imagenes[self.indice_actual]
		self.rect= self.image.get_rect()
		self.contador= 0

	def deforma_imagenes(self):
		''' carga las imagenes ajustando su tamaño según la información en self.fire.deformacion. '''
		self.imagenes= []
		for imagen in VG.get_fire():
			im= pygame.transform.scale(imagen, self.fire.deformacion) 
			self.imagenes.append(im)
		self.indice_actual= 0
		self.image= self.imagenes[self.indice_actual]
		self.rect= self.image.get_rect()
		self.contador= 0
			
	def update(self):
		''' Secuencia las imágenes. '''
		if self.contador == self.fire.latencia:
			try:
				self.indice_actual += 1 
				self.image= self.imagenes[self.indice_actual]
			except:
				self.indice_actual = 0
				self.image= self.imagenes[self.indice_actual]
			self.contador = 0
		self.contador += 1

# ----- FIN DE CLASE JAMFire - INICIO DE DEBUG Y EJEMPLO DE LA CLASE -----
class Ejemplo(object):
	def __init__(self):
		self.ventana = None
		self.reloj = None
		self.nivel = "menu_0"

		self.fondo = None
		self.widgets = None

		self.resolucion = (800,600)

		self.setup()
		self.Run()

	def setup(self):
		pygame.init()
		pygame.display.set_mode(self.resolucion , 0, 0)
		pygame.display.set_caption("Ejemplo de Efecto JAMFire")
		self.fondo = self.get_Fondo()

		#from JAMDragAndDrop import JAMDragAndDrop
		#fire= JAMFire()
		#self.widgets= JAMDragAndDrop(fire)
		self.widgets = JAMFire()
		self.widgets.set_posicion(punto= (400,300))
		#self.widgets.set_disminuye_fire(1)
		#self.widgets.set_deforma_fire((50,100))
		self.ventana = pygame.display.get_surface()
		self.reloj = pygame.time.Clock()
		pygame.event.set_blocked([JOYAXISMOTION, JOYBALLMOTION, JOYHATMOTION, JOYBUTTONUP, JOYBUTTONDOWN,
					USEREVENT, QUIT, ACTIVEEVENT])
		pygame.event.set_allowed([MOUSEMOTION, MOUSEBUTTONUP, MOUSEBUTTONDOWN, KEYDOWN, KEYUP, VIDEORESIZE, VIDEOEXPOSE])
		pygame.mouse.set_visible(True)

	def Run(self):
		self.ventana.blit(self.fondo, (0,0))
		self.widgets.draw(self.ventana)
		pygame.display.update()

		contador= 0
		while self.nivel == "menu_0":
			self.reloj.tick(35)
	
			cambios=[]
			self.widgets.clear(self.ventana, self.fondo)

			'''
			if contador==100:
				self.widgets.set_posicion(punto= (200,300))
				self.widgets.set_disminuye_fire(2)
			if contador==200:
				self.widgets.set_posicion(punto= (400,300))
				self.widgets.set_disminuye_fire(3)
			if contador==300:
				self.widgets.set_posicion(punto= (200,300))
				self.widgets.set_disminuye_fire(4)
			if contador==400:
				self.widgets.set_posicion(punto= (400,300))
				self.widgets.set_disminuye_fire(5)
			if contador==500:
				self.widgets.set_posicion(punto= (200,300))
				self.widgets.set_disminuye_fire(6)
			if contador==600:
				self.widgets.set_posicion(punto= (400,300))
				self.widgets.set_disminuye_fire(7)
			if contador==700:
				self.widgets.set_posicion(punto= (200,300))
				self.widgets.set_disminuye_fire(8)
			if contador==800:
				self.widgets.set_posicion(punto= (400,300))
				self.widgets.set_disminuye_fire(9)
			if contador==900:
				self.widgets.set_posicion(punto= (200,300))
				self.widgets.set_disminuye_fire(1)
			if contador==1000:
				self.widgets.set_posicion(punto= (400,300))
				self.widgets.set_disminuye_fire(10)
				contador= 0
			contador += 1'''

			self.widgets.update()
			self.handle_event()
			pygame.event.clear()
			cambios.extend ( self.widgets.draw(self.ventana) )
			pygame.display.update(cambios)

	def get_Fondo(self):
		superficie = pygame.Surface( self.resolucion, flags=HWSURFACE )
		superficie.fill(VG.get_gris1())
		#superficie.fill(VG.get_negro())
		#superficie.fill(VG.get_blanco())
		return superficie

	def handle_event(self):
		for event in pygame.event.get():
			if event.type == pygame.KEYDOWN:
				teclas = pygame.key.get_pressed()
				if teclas[pygame.K_ESCAPE]:
					self.salir()
		pygame.event.clear()

	def salir(self):
		pygame.quit()
		sys.exit()

if __name__ == "__main__":
	Ejemplo()