Skip to content

Commit d4c8606

Browse files
web
1 parent 25b82f6 commit d4c8606

File tree

9 files changed

+793
-1
lines changed

9 files changed

+793
-1
lines changed

README.ipynb

+107-1
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,116 @@
2626
"<img src=\"img/step1.gif\" width=\"320\" align=\"left\"><br><br><br><br><br><br><br><br><br><br><br><br><br><br>"
2727
]
2828
},
29+
{
30+
"cell_type": "markdown",
31+
"id": "2106a9bb-7055-4725-a2f7-cc06c3c70dbe",
32+
"metadata": {},
33+
"source": [
34+
"## From pygame to the browser"
35+
]
36+
},
37+
{
38+
"cell_type": "markdown",
39+
"id": "3dd18b8a-beff-4a4d-b217-379350c7c46f",
40+
"metadata": {},
41+
"source": [
42+
"You can convert a python pygame into an webassembly and play it in the browser."
43+
]
44+
},
45+
{
46+
"cell_type": "markdown",
47+
"id": "e629058f-b87d-4baf-8c83-12b7448a9d37",
48+
"metadata": {},
49+
"source": [
50+
"Install pygbag https://github.com/pygame-web/pygbag"
51+
]
52+
},
53+
{
54+
"cell_type": "code",
55+
"execution_count": null,
56+
"id": "df33cbec-11d4-4fbb-bbdf-8a764682db3d",
57+
"metadata": {},
58+
"outputs": [],
59+
"source": [
60+
"pip install pygbag"
61+
]
62+
},
63+
{
64+
"cell_type": "markdown",
65+
"id": "a04bb914-82c6-4eb1-8665-b5f14738a63d",
66+
"metadata": {},
67+
"source": [
68+
"You have to rename your game to main.py and make its loop async aware."
69+
]
70+
},
71+
{
72+
"cell_type": "code",
73+
"execution_count": null,
74+
"id": "e748e315-fb25-40c2-9c6e-9338fcf06c31",
75+
"metadata": {},
76+
"outputs": [],
77+
"source": [
78+
"import pygame, asyncio\n",
79+
"\n",
80+
"FRAMES_PER_SECOND = 30\n",
81+
"\n",
82+
"\n",
83+
"class Ping:\n",
84+
"...\n",
85+
"\n",
86+
" async def game_loop(self):\n",
87+
" while True:\n",
88+
" for event in pygame.event.get():\n",
89+
" if (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE) \\\n",
90+
" or (event.type == pygame.QUIT):\n",
91+
" return\n",
92+
"\n",
93+
" self.update()\n",
94+
" self.draw()\n",
95+
" await asyncio.sleep(0)\n",
96+
"\n",
97+
"...\n",
98+
"\n",
99+
"async def main():\n",
100+
" ping = Ping()\n",
101+
" await ping.game_loop()\n",
102+
"\n",
103+
"asyncio.run(main())"
104+
]
105+
},
106+
{
107+
"cell_type": "markdown",
108+
"id": "359968c1-1422-4ddc-984e-3adae372d63e",
109+
"metadata": {},
110+
"source": [
111+
"Compile to webassembly"
112+
]
113+
},
114+
{
115+
"cell_type": "code",
116+
"execution_count": null,
117+
"id": "4dae1af5-5436-4e06-a917-b9cc592e598d",
118+
"metadata": {},
119+
"outputs": [],
120+
"source": [
121+
"pygbag ./folder"
122+
]
123+
},
124+
{
125+
"cell_type": "markdown",
126+
"id": "50e49198-d3f6-406d-b08d-334ba26eac81",
127+
"metadata": {},
128+
"source": [
129+
"You can play it.\n",
130+
"Chrome should work. Use keyboard.\n",
131+
"\n",
132+
"https://hebi-python-ninja.itch.io/ping-from-pygame-to-the-web"
133+
]
134+
},
29135
{
30136
"cell_type": "code",
31137
"execution_count": null,
32-
"id": "74ce2b0b-7b66-45ee-8907-34a6ca2c8e48",
138+
"id": "5c775a9e-bae4-4c99-86ad-1d5d6e294421",
33139
"metadata": {},
34140
"outputs": [],
35141
"source": []

README.md

+55
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,61 @@ A classic table tennis game
66

77
<img src="img/step1.gif" width="320" align="left"><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
88

9+
## From pygame to the browser
10+
11+
You can convert a python pygame into an webassembly and play it in the browser.
12+
13+
Install pygbag https://github.com/pygame-web/pygbag
14+
15+
16+
```Rust
17+
pip install pygbag
18+
```
19+
20+
You have to rename your game to main.py and make its loop async aware.
21+
22+
23+
```Rust
24+
import pygame, asyncio
25+
26+
FRAMES_PER_SECOND = 30
27+
28+
29+
class Ping:
30+
...
31+
32+
async def game_loop(self):
33+
while True:
34+
for event in pygame.event.get():
35+
if (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE) \
36+
or (event.type == pygame.QUIT):
37+
return
38+
39+
self.update()
40+
self.draw()
41+
await asyncio.sleep(0)
42+
43+
...
44+
45+
async def main():
46+
ping = Ping()
47+
await ping.game_loop()
48+
49+
asyncio.run(main())
50+
```
51+
52+
Compile to webassembly
53+
54+
55+
```Rust
56+
pygbag ./folder
57+
```
58+
59+
You can play it.
60+
Chrome should work. Use keyboard.
61+
62+
https://hebi-python-ninja.itch.io/ping-from-pygame-to-the-web
63+
964

1065
```Rust
1166

build/version.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0.7.1

build/web/Archiv.zip

670 KB
Binary file not shown.

build/web/favicon.png

18 KB
Loading

0 commit comments

Comments
 (0)