importosimportpygamefrompygame.localsimport*defload_sliced_sprites(w,h,filename):'''
Specs :
Master can be any height.
Sprites frames width must be the same width
Master width must be len(frames)*frame.width
Assuming you ressources directory is named "resources"
'''images=[]master_image=pygame.image.load(os.path.join('resources',filename)).convert_alpha()master_width,master_height=master_image.get_size()foriinxrange(int(master_width/w)):images.append(master_image.subsurface((i*w,0,w,h)))returnimagesclassAnimatedSprite(pygame.sprite.Sprite):def__init__(self,images,fps=10):pygame.sprite.Sprite.__init__(self)self._images=images# Track the time we started, and the time between updates.# Then we can figure out when we have to switch the image.self._start=pygame.time.get_ticks()self._delay=1000/fps
self._last_update=0self._frame=0# Call update to set our first image.self.update(pygame.time.get_ticks())defupdate(self,t):# Note that this doesn't work if it's been more that self._delay# time between calls to update(); we only update the image once# then, but it really should be updated twice.ift-self._last_update>self._delay:self._frame+=1#if self._frame >= len(self._images): self._frame = 0ifself._frame=len(self._images):self._frame=0if__name__=="__main__":print"the game"pygame.init()screen=pygame.display.set_mode((800,600))images=load_sliced_sprites(100,71,"spinning_roundhouse_kick.png")sprite=AnimatedSprite(images,20)#while pygame.event.poll().type != KEYDOWN:whileTrue:screen.fill((255,255,255))event=pygame.event.poll()ifevent.type==KEYDOWNandevent.key==K_a:#print "keydown"#for image in images:whileTrue:#print "frame"#print sprite._frame#print sprite.isfinished()time=pygame.time.get_ticks()sprite.update(time)screen.blit(sprite.getimage(),(0,0))pygame.display.update()#sprite.reset()ifsprite.isfinished()==True:sprite.reset()break#time = pygame.time.get_ticks()#sprite.update(time)#screen.blit(sprite.image, (0,0))pygame.display.update()