Package: pygame-easy-menu
table of contentdisclaimerThis package is free of uses, modifications for any project.why ?If you want to implement menus in your pygame app in python without recoding every classic widgets, this is for you. Pygame_easy_menu allow you to link your windows to a menu_manager that you can toggle or not. It will then allow you to add widget to every menu with pre-code functions.installationcommand :python -m pip install pygame_easy_menuHow to use ?To use menu in your pygame window you first need to initiate a menu manager, this class will be link to your pygame window and allow you to add menu, activate and desactivate it.You can then add sprite to your menu and define their function trigger on pygame event.link your window or make onecreate a new window :frompygame_easy_menuimport*frompygame_easy_menu.toolsimport*importpygamepygame.init()menu_manager=Menu_Manager(name="MySuperGame",size=Vector2(1000,800),background=BG)link the library to an already existing window :frompygame_easy_menuimport*frompygame_easy_menu.toolsimportBG# a free background image for your tests"""[...] your previous code"""menu_manager=Menu_Manager(window=win,background=BG)# win is your pygame windowadd menuto add a menu you juste need to create it with the Menu class and it will automatically be added to your menu manager. To select the menu at screen of the menu manager, you need to store it in theactual_menuattribute. Every time a new menu is store inactual_menuthe setup fonction of the menu will be executed.# to add a menu :principal=Menu("principale")# you can also select a specific background for a menusecond=Menu("second",background="myimage.png")menu_manager.actual_menu=principal#this will change the actual menu of your game, if your menu manager is running this attribute can't be empty.To edit the setup function of a menu you need to use the decorator set_setup add pass any function under it, the decorator will update the setup function automatically.@principal.set_setupdefsetup():# you can name your function like you want# your stuff hereadd spriteTo add a sprite to a menu you need to declare a function where you return a sprite based class and put it under theadd_spritedecorator of you menu. If you want to create your own sprite class you need to pass thespriteclass in its inherance.@principal.add_spritedefback_button():_button=Button(name="mybutton",path="myimageofbutton.png")"""put the config of your button here"""return_buttonthere currently are the following widget : AlertBox,InputBox,Button,textZone,spriterun the menu_managerTo run the you need to start themenu_manager.run()function, this function will run untilmenu_manager.runningis False. You need to put this function at the end of your code or in a function if you don't want your code to be stucked.You can the stop the manager by callingmenu_manager.stop()if you want to close the menu and launch back the game. You can also completly stop the program by callingmenu_manager.destroy().menu functions and parameterschild and parentsEvery menu can be link with other menu by parent/child system. This way you can retreive a menu either with your global variable or by menu's functions.
One menu can have multiple children but only one parent. When you define your Menu you can also pass name of children or future children, or the name of its parent. Warning if you say that a menu is the children of a parent menu, the name wont be automatically add to the list of the other instance (for now).Add a child :principal=Menu("principal",childs=["second"])Add a parent :second=Menu("second",parent="principal",childs="third")// childs parameter can be either a list or a string.You can then when the menu_manager is running get one child of your menu or its parent.Get a child : Menu.get_child(self,child_name)mychilds=second.get_child("third")Get all children (return a generator) : Menu.get_childs(self)# return a generator you can itter inforchildinsecond.get_childs():"""your code here"""# to transform to a listmychild=[childforchildinsecond.get_childs()]Get the parent : Menu.get_parent(self)mychilds=second.get_parent()spritesOnce you add a sprite to a Menu you can then access it with either the list of sprite or with theget_spritefunction.Get a sprite : Menu.get_sprite(self,name)_button=second.get_sprite("mybutton")# you can also itter in the list directlyfor_buttoninsecond.sprites:"""your code here"""sprite functions and parametersSpriteEach widget is a derivate from the sprite class, those fonctions are in most cases in other widget.add a sprite : Sprite.add_sprite(self,sprite)@main_menu.add_spritedefmy_sprite():_sprite=Sprite(name="my_sprite",path="myimage.png",manager=menu_manager)"""Put your config here"""return_spritechange postion : Sprite.set_positon(pos:Vector2)_sprite.set_position(Vector2(110,250))# absolute position_sprite.set_position(Vector2(0.5,0.33))# position in percentage of the screenchange scale : Sprite.set_scale(pos:Vector2)_sprite.set_scale(Vector2(110,250))# absolute scale_sprite.set_scale(Vector2(0.5,0.33))# scale in percentage of the previous surface sizeadd function on event : Sprite.Event(_event:pygame.Event)(function)@_sprite.Event(pygame.QUIT)defdebug_bye(event):"""your code here"""this function will we executed each time the event pygame.QUIT is raise.ButtonA Button is a derivate of the sprite class.Button has the decorator on click : Button.on_click(self,function)@main_menu.add_spritedefmy_button():_button=Button(name="mybutton",path="myimageofbutton.png",manager=menu_manager)"""put the config of your button here"""@_Button.on_clickdefdebug(event):"""your code here"""return_buttonthis function will we executed each time the button is clicked.TextBoxA TextBox is a derivate of the sprite class.@_textbox.on_changeInputBoxAlert booxexemple codefrompygame_easy_menuimport*frompygame_easy_menu.toolsimportBG,B_BGimportpygamepygame.init()menu_manager=Menu_Manager(name="mygame",size=Vector2(1000,800),background=BG)main_menu=Menu(name="Main")@main_menu.add_spritedefplay_button():_button=Button(name="Play_Button",path=B_BG)_button.set_position(Vector2(0.5,0.33))_button.set_text("Play",padding=0.15)@_button.on_clickdeflaunch():print("my game is launch")return_button@main_menu.add_spritedefexit_button():_button=Button(name="Exit_button",path=B_BG)_button.set_position(Vector2(0.5,0.66))_button.set_text("Exit",padding=0.15)@_button.on_clickdeflaunch():menu_manager.destroy()return_buttonmenu_manager.actual_menu=main_menuif__name__=="__main__":try:menu_manager.run()exceptKeyboardInterrupt:menu_manager.destroy()a faireajouter icon par défaut dans le module pour exemple code
finir read me
how to run the menu