pygame wasm

CPython WASM specifics

These code snippets are useful when running your game in a browser with Emscripten.

Check if game is running in the browser

if sys.platform == "emscripten":
    pass

Detect WebAssembly CPU

if 'wasm' in __import__('platform').machine():
    pass

Main loop example (must be asynchronous)

import asyncio
import pygame

pygame.init()


def menu(events):
    # draw
    # check events
    # change state
    pass


def play(events):
    # draw
    # check events
    # change state
    pass

game_state = menu

async def main():
    global game_state
    
    # You can initialise pygame here as well

    while game_state:
        game_state(pygame.event.get())
        pygame.display.update()
        await asyncio.sleep(0) # do not forget that one, it must be called on every frame
        
    # Closing the game (not strictly required)
    pygame.quit()
    sys.exit()
        
if __name__ == "__main__":
    asyncio.run(main())

Everything else is probably specific to Pygbag. You can find more information/code here.

Contribute to this page