Skip to content

Commit 23c55d2

Browse files
committed
speficic python wasm info
1 parent e2f2d94 commit 23c55d2

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

wiki/python-wasm/README.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# CPython WASM specifics
2+
3+
These code snippets are useful when running your game in a browser with Emscripten.
4+
5+
## Check if game is running in the browser
6+
7+
```py
8+
if sys.platform == "emscripten":
9+
pass
10+
```
11+
12+
## Detect WebAssembly CPU
13+
14+
```py
15+
if 'wasm' in __import__('platform').machine():
16+
pass
17+
```
18+
19+
## Main loop example (must be asynchronous)
20+
21+
```py
22+
import asyncio
23+
import pygame
24+
25+
pygame.init()
26+
27+
28+
def menu(events):
29+
# draw
30+
# check events
31+
# change state
32+
pass
33+
34+
35+
def play(events):
36+
# draw
37+
# check events
38+
# change state
39+
pass
40+
41+
game_state = menu
42+
43+
async def main():
44+
global game_state
45+
46+
# You can initialise pygame here as well
47+
48+
while game_state:
49+
game_state(pygame.event.get())
50+
pygame.display.update()
51+
await asyncio.sleep(0)
52+
53+
# Closing the game (not strictly required)
54+
pygame.quit()
55+
sys.exit()
56+
57+
if __name__ == "__main__":
58+
asyncio.run(main())
59+
```
60+
61+
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).
62+
63+
64+
[Contribute to this page](https://github.com/pygame-web/pygame-web.github.io/edit/main/wiki/python-wasm/README.md)

0 commit comments

Comments
 (0)