Package: tween
tweenA tweening library inspired by flux by rxi. Using the PyTweening module by AlSweigart.Installingpip install tweenExample using a pygame setupimportsysimportpygamefrompygame.localsimportQUITimporttweenpygame.init()screen=pygame.display.set_mode(400,400)clock=pygame.time.Clock()dt=0.0classCharacter:def__init__(self,surface,x,y):self.sprite=surfaceself.x=xself.y=ydefdraw(surface):surface.blit(self.sprite,(self.x,self.y))hero_image=pygame.image.load("path/to/image.png")hero=Character(hero_image,0,200)hero_tween=tween.to(hero,"x",400,5.0,"easeInOutQuad")#Starting a tween.defsay_message():print("Started moving!")hero_tween.on_start(say_message)#Adding function that runs at the start of the tween-#.on_start() will only have an effect if you call it before the first time the tween is updateddefupdate(dt):tween.update(dt)#Updating all active tweens within the default groupdefdraw(surface):surface.fill(0,0,0)hero.draw(surface)pygame.display.flip()while1:foreventinpygame.event.get():ifevent.type==QUIT:sys.exit()update(dt)draw(screen)dt=clock.tick(60)/1000.0#Divide by 1000.0 to get dt (time_passed) in secondsUnless there is a typo in my code, theheroobject should move from(y = 200, x = 0) to (y = 200, x = 400)in the span of exactly 5 seconds. Using"easeInOutQuad"the sprite will slowly accelerate and decelerate.Functions and classestween.to(container,key,final_value,time,easing_type="linear",_group=tween.tweens)-->tween.TweenCreates and adds a Tween object to the default tween module group.Thecontainerargument can be a list, dictionary or object.If thecontaineris a list, thekeymust be an integer.If thecontaineris a dictionary, thekeymust be a string.If thecontaineris an object, thekeymust be a string.final_valueis the target value the tween will stop at.timeis how long the tween should take to finish inseconds.easing_typeis a string describing the easing function you want to use. There is a list of all types at the bottom of this readme.If there already exists a tween for that container and key, the existing tween will delete itself os the new one can startThe_groupargument should not be passed.Instead you should create an instance of thetween.Groupclass, and call its.tomethodtweening_group=tween.Group()tweening_group.to(container,key,final_value,time,easing_type="linear")-->tween.TweenThe tween module and all instances oftween.Grouphas anupdatefunction/method.tween.update(passed_time,_group=tween.tweens)tween.Group.update(passed_time)The_groupargument should not be passed here either. The tween module will pick the rigth group for you.passed_timeis the time since the last update was called inseconds.If you want to pause all tweens within a certain group, it is as simple as not calling the.updatemethod for that group.Thetween.Tweenobject returned from the.tofunction/method has a couple methods of its own.tween.Tween.stop()#stops and deletes the tween from its group.tween.Tween.on_start(func)#appends a function that runs at the start of the tween.tween.Tween.on_update(func)#appends a function that runs every update.tween.Tween.on_complete(func)#appends a function that runs at the end of the tween.You can append as many functions as you like.Easing typeseaseInBackeaseInBounceeaseInCirceaseInCubiceaseInElasticeaseInExpoeaseInOutBackeaseInOutBounceeaseInOutCirceaseInOutCubiceaseInOutElasticeaseInOutExpoeaseInOutQuadeaseInOutQuarteaseInOutQuinteaseInOutSineeaseInQuadeaseInQuarteaseInQuinteaseInSineeaseOutBackeaseOutBounceeaseOutCirceaseOutCubiceaseOutElasticeaseOutExpoeaseOutQuadeaseOutQuarteaseOutQuinteaseOutSinelinearIf you want to get a list of all the easing types without reading this readme, you can do:tween.print_ease_types()or simply run the module directly in the terminal:python3 -m tween