python动画精灵move_按键上的精灵动画

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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值