Package: unipygame
UniPygame - A Unity-similar entity system for pygameUniPygame is a system for unity-similar objects, but in pygame.
It allows you to define single entites and not have to bother with long complicated mainloops.UniPygame EntitiesA basic rect on the screen, would look like this:fromUniPygameimportScene,Entityimportpygamepygame.init()screen=pygame.display.set_mode((1080,720))scene=Scene(surf=screen,clear_color=pygame.Color(255,255,255))cube=Entity(scene=scene,color=pygame.Color(255,0,0),rect=pygame.Rect(100,100,100,100))running=Truescene.Awake()whilerunning:ifscene.quit_event:pygame.display.quit()running=Falsescene.Update(fps_limit=60)UniPygame SpritesAs you might have seen in the example above, anEntitycan only be arect.If you, however want to add something like an image to a scene, you can use a different class, theSprite. It would look something like this:fromUniPygameimportScene,Sprite,Vec2importpygamepygame.init()screen=pygame.display.set_mode((1080,720))scene=Scene(surf=screen,clear_color=pygame.Color(255,255,255))image=Sprite(scene=scene,image=pygame.image.load("image.png"),position=Vec2(100,100))running=Truescene.Awake()whilerunning:ifscene.quit_event:pygame.display.quit()running=Falsescene.Update(fps_limit=60)As you can see, not terribly differentAnd however large the amount of Entities and Sprites you add, thewhile runningloop will always have the same sizeFrame functionsUniPygame's frame functions is UniPygames way of updating objetcs.
This example shows with Entities but it will also work for sprites, in the same way
The way they work is as follows:fromUniPygameimportEntityimportpygamepygame.init()screen=pygame.display.set_mode((1080,720))scene=Scene(surf=screen,clear_color=pygame.Color(255,255,255))#First we define a frame function#This function needs a parameter 'self' else it will spit out an errordefcube_move(self):#Here we move the object that inherites the function downwards#The speed of the object is multiplied by the delta time, so that it always moves by the same amount whatever the fps isself.position.y+=20*scene.delta_time#Then we define a object and set the frame_function to the cube_move functioncube=Entity(scene=scene,color=pygame.Color(255,0,0),rect=pygame.Rect(100,100,100,100),frame_function=cube_move)running=Truescene.Awake()whilerunning:ifscene.quit_event:pygame.display.quit()running=False#Now, when calling update, the cube_move function will be calledscene.Update(fps_limit=60)UniPygame Input systemWell, now you might be wondering, since the loop is always the same size, how will inputs work?
Well for that there is a parameter in the Scene classScene.keydown_listener, this parameter needs a function into it, this function has to have a keyword argumentkey, everytime a button is pressed, this argument will be set to the pygame key code of the pressed key, and the function will be called.An effective listener would look like this:fromUniPygameimportSceneimportpygamepygame.init()defkeydown(key):ifkey==pygame.K_1:print("Key 1 has been pressed")ifkey==pygame.K_2:print("Key 2 has been pressed")ifkey==pygame.K_3:print("Key 3 has been pressed")screen=pygame.display.set_mode((1080,720))scene=Scene(surf=screen,clear_color=pygame.Color(255,255,255),keydown_listener=keydown)Also, the same works for the keyup event, using theScene.keyup_listenerparameter.But if you want continuus button presses, there is a list in the sceneScene.held_keyswhere you just have to index the pygame key code of the key you want to check if its pressed/held pressed, and do something with that.There are some more functions, these however are documented in the wiki