{"id":30063402,"url":"https://github.com/therealprohacker/statemanager","last_synced_at":"2025-08-08T04:24:24.417Z","repository":{"id":203412535,"uuid":"709554418","full_name":"theRealProHacker/StateManager","owner":"theRealProHacker","description":"A state manager for pygame","archived":false,"fork":false,"pushed_at":"2023-11-02T15:31:57.000Z","size":12,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-07-31T21:16:32.116Z","etag":null,"topics":["game-development","pygame","python3","state-machine","state-management"],"latest_commit_sha":null,"homepage":"https://pypi.org/project/pgsm/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/theRealProHacker.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-10-24T23:18:41.000Z","updated_at":"2024-05-23T14:57:21.000Z","dependencies_parsed_at":null,"dependency_job_id":"4e6715f0-902e-47ce-86a1-4e75272c4e7b","html_url":"https://github.com/theRealProHacker/StateManager","commit_stats":null,"previous_names":["therealprohacker/statemanager"],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/theRealProHacker/StateManager","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theRealProHacker%2FStateManager","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theRealProHacker%2FStateManager/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theRealProHacker%2FStateManager/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theRealProHacker%2FStateManager/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/theRealProHacker","download_url":"https://codeload.github.com/theRealProHacker/StateManager/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theRealProHacker%2FStateManager/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269363279,"owners_count":24404750,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-08T02:00:09.200Z","response_time":72,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["game-development","pygame","python3","state-machine","state-management"],"created_at":"2025-08-08T04:24:22.549Z","updated_at":"2025-08-08T04:24:24.398Z","avatar_url":"https://github.com/theRealProHacker.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# State Manager\n\nA simple pygame state manager with threaded loading using loading screens. \n\nThis means you can use heavy initialization and entry code without having to worry about it. As long as your code needs to finish up, a loading state will be displayed. \n\n# Set Up\n\nYou can choose to install either `pygame` or the community version `pygame-ce`\n```shell\npip install pygame\n# or\npip install pygame-ce\n```\n\nThe package is called pgsm (`PyGameStateMachine`)\n\n```shell\npip install pgsm\n```\n\n# Usage\n\nYou first write the states by inheriting from `State` then you put it all together with the `StateManager`.\n\nTake this as a simple example with a play state and a pause state.\n\nWe want to toggle between play and pause state when the user presses \"p\"\n\n```py\nimport pygame as pg\nfrom StateManager import State, StateManager\n\nclass PlayState(State):\n    def on_enter(self):\n        print(\"Playing\")\n\n    def update(events, dt):\n        # This is an example for handling events\n        # and changing states\n        for event in events:\n            if event.type == pg.KeyDown and event.key == pg.K_p:\n                self.exit(\"pause\")\n\n    def draw(self, s):\n        \"\"\" \n        Whatever you want to draw \n        s is the surface or screen you may draw to\n        \"\"\"\n\nclass PauseState(State):\n    def on_enter(self, _frm):\n        print(\"Pausing\")\n\n    def update(events, dt):\n        for event in events:\n            if event.type == pg.KEYDOWN and event.key == pg.K_p:\n                self.exit(\"play\")\n\n    def draw(self, s):\n        \"\"\" Whatever you want to draw \"\"\"\n\n# We use strings as keys to refer to our states\nsm = StateManager({\n    \"play\":PlayState(),\n    \"pause\":PauseState()\n}, start=\"play\")\n```\n\nNow you can use your state machine like this.\nThe state machine will by default just draw to the main screen. \n\n```py\nSCREEN = pg.display.set_mode((900, 600))\n\nwhile running:\n    dt = clock.tick(60)\n    events = pg.event.get()\n    for event in events:\n        if event.type == pg.QUIT:\n            running = False\n        if event.type == pg.KEYDOWN and event.key == pg.K_ESCAPE:\n            running = False\n        # we could also have done the toggle here\n    SCREEN.fill(\"white\")\n    sm(events, dt)\n    pg.display.flip()\n```\n\nThe default loading state draws a little spinner. If you want to change how that works, simply override the `LoadingState`.\n\nFor example:\n\n```py\nfrom StateManager import LoadingState\n\nclass BlankLoadingState(LoadingState):\n    def __init__(self, color):\n        self.color = color\n\n    def update(self, *args):\n        pass\n\n    def draw(self, s):\n        s.fill(self.color)\n```\n\nAnd use like this\n\n```py\nsm = StateManager({\n        \"play\":PlayState(),\n        \"pause\":PauseState()\n    }, \n    start=\"play\", \n    loading_state=BlankLoadingState(\"black\")\n)\n```\n\n# License\n\nMIT License","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftherealprohacker%2Fstatemanager","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftherealprohacker%2Fstatemanager","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftherealprohacker%2Fstatemanager/lists"}