import pygame, spriteanimation
from pygame.locals import*

class Rect_test(pygame.sprite.Sprite):
    explosion = [] 
    animation = spriteanimation.Animation(explosion)
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        sprite_sheet = spriteanimation.SpriteSheet("explosion_Sprite_Sheet.png")
        image = sprite_sheet.get_image(0, 0, 20, 20)
        self.animation.add_frame(image)
        image = sprite_sheet.get_image(20, 0, 20, 20)
        self.animation.add_frame(image)
        image = sprite_sheet.get_image(40, 0, 20, 20)
        self.animation.add_frame(image)
        image = sprite_sheet.get_image(60, 0, 20, 20)
        self.animation.add_frame(image)
        
        self.image = self.explosion[0]
        self.rect = self.image.get_rect()
        self.animation.set_framerate(10)
        
    def update(self, time_passed):
        self.rect.x += 1
        #pos = self.rect.x
        self.image = self.animation.get_frame(time_passed)
         

def make_list(sheet, liste):
    sprite_sheet = spriteanimation.SpriteSheet(sheet)
    image = sprite_sheet.get_image(0, 0, 20, 20)
    liste.append(image)
    image2 = sprite_sheet.get_image(20, 0, 20, 20)
    liste.append(image2)
    image3 = sprite_sheet.get_image(40, 0, 20, 20)
    liste.append(image3)
    image4 = sprite_sheet.get_image(60, 0, 20, 20)
    liste.append(image4)
    
    return liste
    
pygame.init()
screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption('Test')

rect_test = Rect_test()
sprite_list = pygame.sprite.Group()
sprite_list.add(rect_test)

#animation = spriteanimation.Animation(explosion)
#animation.set_framerate(3)

background = pygame.Surface(screen.get_size())
background = background.convert_alpha()

screen.blit(background, (0, 0))
pygame.display.update()

clock = pygame.time.Clock()
milliseconds = clock.tick(60)
seconds = milliseconds/1000.0
time_passed = 0

running = True

while running:
    time_passed += seconds
    for event in pygame.event.get():
        if event.type == QUIT:
            running = False
            
    screen.fill((0, 0, 0))
    sprite_list.update(time_passed)
    screen.blit(background, (0, 0))
    sprite_list.draw(screen)
    print rect_test.animation.get_act_frame()
    if rect_test.animation.get_act_frame() >= len(rect_test.animation.surfaces)-1:
        print "done"
        rect_test.animation.reset_animation()
    clock.tick(60)
    #screen.blit(animation.get_frame(int(time_passed)), (100 ,100))
    pygame.display.update()

pygame.quit() 
