Skip to content

Commit

Permalink
speficic python wasm info
Browse files Browse the repository at this point in the history
  • Loading branch information
pmp-p committed Aug 9, 2024
1 parent e2f2d94 commit 23c55d2
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions wiki/python-wasm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# 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

```py
if sys.platform == "emscripten":
pass
```

## Detect WebAssembly CPU

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

## Main loop example (must be asynchronous)

```py
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)

# 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](https://pygame-web.github.io/wiki/pygbag-code/#pygbag-code-specificssamples).


[Contribute to this page](https://github.com/pygame-web/pygame-web.github.io/edit/main/wiki/python-wasm/README.md)

0 comments on commit 23c55d2

Please sign in to comment.