import pygame
from pygame.locals import*
from pygame.sprite import Sprite
import os
ANCHO = 640
ALTO = 480
def load_spritesheet(archivo,filas,columnas):
fullname = os.path.join ('data', archivo)
img=pygame.image.load(fullname).convert()
images=[]
alto=img.get_height()/filas
ancho=img.get_width()/columnas
aux_img=pygame.Surface((ancho,alto))
for i in range(filas):
for j in range(columnas):
aux_img=pygame.Surface((ancho,alto))
area=pygame.Rect(j*ancho,i*alto,ancho,alto)
aux_img.blit(img,(0,0),area)
aux_img=aux_img.convert()
aux_img.set_colorkey(aux_img.get_at((0,0)))
images.append(aux_img)
return images
class Animation(object):
def __init__(self, ima, str_frames = ""):
self.imagenes = ima
self.frames = self.load_frames(str_frames)
self.image = self.imagenes[self.frames[0]]
self.rect = self.image.get_rect()
self.step = 0
self.delay = 0
self.index = self.frames[0]
self.longitud = len(self.frames)
def load_frames(self,frames):
lista = []
frames = frames.split(',')
for i in frames:
lista.append(int(i))
return lista
def set_frame(self, indice):
self.image = self.imagenes[indice]
def update(self, delay = 9):
if self.step >= self.longitud-1:
self.end = True
self.step = 0
self.delay -= 1
if self.delay <= 0:
self.delay = delay
self.step += 1
self.set_frame(self.frames[self.step])
def draw(self,screen):
screen.blit(self.image,self.rect)
class State(object):
def __init__(self,luchador,state_name):
self.luchador = luchador
self.name = state_name
def update(self):
pass
class Intro(State):
def __init__(self, luchador):
State.__init__(self,luchador,"intro")
self.luchador.set_animation('intro')
def update(self):
self.luchador.current_animation.update()
class Guardia(State):
def __init__(self, luchador):
State.__init__(self,luchador,"guardia")
self.luchador.set_animation('guardia')
def update(self):
self.luchador.current_animation.update()
class Saltar(State):
def __init__(self, luchador):
State.__init__(self,luchador,"saltar")
self.luchador.set_animation('saltar')
def update(self):
self.luchador.current_animation.update()
class Luchador(Sprite):
def __init__(self, archivo, filas, columnas):
Sprite.__init__(self)
self.states = {}
self.frames = load_spritesheet(archivo,filas,columnas)
self.x2frames = self.double_sized_images()
self.animations = {'guardia': Animation(self.x2frames,'1,2,3,4,5'),
'saltar': Animation(self.x2frames,'76,77,76'),
'intro': Animation(self.x2frames,'181,182,183,184,185,186,187,188,189,190,191,192,193')}
intro = Intro(self)
saltar = Saltar(self)
guardia = Guardia(self)
self.add_state(intro)
self.add_state(guardia)
self.add_state(saltar)
self.set_state("intro")
def set_animation(self, anim_id):
self.current_animation = self.animations[anim_id]
def get_animation(self,anim_id):
return self.animations[anim_id]
def add_state(self, state):
self.states[state.name] = state
def set_state(self,sname):
self.current_state = self.states[sname]
def double_sized_images(self):
frames2x = []
for i in range (len(self.frames)-1):
frames2x.append(pygame.transform.scale2x(self.frames[i]))
return frames2x
def update(self):
self.current_state.update()
key = pygame.key.get_pressed()
if key[K_a]:
self.set_state("saltar")
if key[K_s]:
self.set_state("guardia")
def draw(self, screen):
self.current_animation.draw(screen)
def main():
quit = False
screen = pygame.display.set_mode((ANCHO, ALTO))
luchador = Luchador("Ryu.png",17,15)
clock = pygame.time.Clock()
while not quit:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit = True
screen.fill((200, 200, 200))
luchador.update()
print luchador.current_state
luchador.draw(screen)
pygame.display.flip()
if __name__ == '__main__':
pygame.init()
main()
Cuando se produce un cambio de estado del personaje, al presionar una tecla, NO se produce el cambio de animacion corespondiente a ese estado, no se si esta claro lo que pregunto.
Por las dudas pueden correr el codigo ustedes:
(la imagen debe estar en la carppeta "data" y el script en el mismo directorio que "data")
http://www.mediafire.com/?q919bsu39200qd7
Desde ya les estare muy agradecido por la ayuda.