You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
+
defmenu(events):
29
+
# draw
30
+
# check events
31
+
# change state
32
+
pass
33
+
34
+
35
+
defplay(events):
36
+
# draw
37
+
# check events
38
+
# change state
39
+
pass
40
+
41
+
game_state = menu
42
+
43
+
asyncdefmain():
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