1 import pygame
2 import ResourceLoader
3 import pdb
4
6 '''
7 AnimatedSprite hereda de Sprite, y tiene como objetivo
8 simplificar la carga de sprites por tiles, ademas de la
9 animacion de estos.
10 '''
11 - def __init__(self, image, filas, columnas, fps = 10):
12 '''
13 Constructor de AnimatedSprite
14 image: es la ruta hacia la imagen por tiles
15 filas: el numero de imagenes en vertical
16 columnas: el numero de imagenes en horizontal
17 fps: frames por segundo de las animaciones
18 '''
19 pygame.sprite.Sprite.__init__(self)
20 self._images = self.load(image, filas, columnas)
21 self._hitmasks = self.hm(self._images)
22 self._animacion = [0]
23 self.nimages = filas * columnas
24
25
26
27 self._start = pygame.time.get_ticks()
28 self._delay = 1000 / fps
29 self._last_update = 0
30 self._frame = self._animacion[0]
31
32 self.image = self._images[self._frame]
33 self.hitmask = self._hitmasks[self._frame]
34 self.rect = self.image.get_rect()
35
36
37 self.animate(pygame.time.get_ticks())
38
39 - def hm(self, images):
40 '''
41 Crea un vector de mascaras para las colisiones
42 del vector de imagenes
43 '''
44 hitmasks = []
45 for i in images:
46 hitmasks.append(pygame.surfarray.array_colorkey(i))
47
48 i.unlock()
49 i.unlock()
50
51 return hitmasks
52
54 '''
55 Genera la animacion:
56 t: debe ser pygame.time.get_ticks
57 '''
58 if t - self._last_update > self._delay:
59 self._frame += 1
60 if not self._frame in self._animacion:
61 self._frame = self._animacion[0]
62 self.image = self._images[self._frame]
63 self.hitmask = self._hitmasks[self._frame]
64 self._last_update = t
65
66 - def load(self, path, filas, columnas, flip="True"):
67 '''
68 Carga una imagen de tipo tile, en un vector de imagenes.
69 Devuelve un vector de imagenes.
70 path: es la ruta a la imagen
71 filas: es el numero de frames en vertical
72 columnas: es el numero de frames en horizontal
73 '''
74
75 images = []
76
77 img = ResourceLoader.ResourceLoader.load(path)
78 if flip:
79 img_flip = pygame.transform.flip(img, True, False)
80
81 alto = img.get_height() / filas
82 ancho = img.get_width() / columnas
83
84 for i in range(filas):
85 for j in range(columnas):
86 aux_img = pygame.Surface((ancho, alto))
87
88 area = pygame.Rect(j*ancho, i*alto, ancho, alto)
89 aux_img.blit(img, (0,0), area)
90 aux_img = aux_img.convert()
91 aux_img = self.clip(aux_img, aux_img.get_at((0,0)))
92 aux_img.set_colorkey(aux_img.get_at((0,0)))
93 images.append(aux_img)
94
95 if flip:
96 cols = range(columnas)
97 cols.reverse()
98 for i in range(filas):
99 for j in cols:
100 aux_img = pygame.Surface((ancho, alto))
101
102 area = pygame.Rect(j*ancho, i*alto, ancho, alto)
103 aux_img.blit(img_flip, (0,0), area)
104 aux_img = aux_img.convert()
105 aux_img = self.clip(aux_img, aux_img.get_at((0,0)))
106 aux_img.set_colorkey(aux_img.get_at((0,0)))
107 images.append(aux_img)
108
109 return images
110
112 '''
113 Pone en self.image el frame seleccionado de la lista
114 de imagenes self._images
115 frame: un entero.
116 '''
117
118 self._frame = frame
119 self.image = self._images[self._frame]
120 self.hitmask = self._hitmasks[self._frame]
121
123 '''
124 Pone una animacion a mostrar
125 animation: una lista de enteros, donde estan los
126 indices de los frames
127 por ejemplo: [0,1,2,3] esto es una animacion de 4 frames
128 '''
129 self._animacion = animation
130
131 - def clip(self, image, colorkey):
132 '''
133 Recibe un surface, y devuelve el surface ajustado en x
134 '''
135 minx = image.get_width()
136 maxx = 0
137 for j in range(0, image.get_height(), 10):
138 for i in range(image.get_width()):
139 pixel = image.get_at((i,j))
140 if pixel != colorkey:
141 if i < minx:
142 minx = i
143 if i > maxx:
144 maxx = i
145
146 ancho = maxx - minx
147 alto = image.get_height()
148 if ancho < 0:
149 return image
150 new_image = pygame.Surface((ancho, alto))
151 area = pygame.Rect(minx, 0, ancho, alto)
152 new_image.blit(image, (0, 0), area)
153 new_image = new_image.convert()
154
155 return new_image
156