|
26 | 26 | "<img src=\"img/step1.gif\" width=\"320\" align=\"left\"><br><br><br><br><br><br><br><br><br><br><br><br><br><br>"
|
27 | 27 | ]
|
28 | 28 | },
|
| 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 | + }, |
29 | 135 | {
|
30 | 136 | "cell_type": "code",
|
31 | 137 | "execution_count": null,
|
32 |
| - "id": "74ce2b0b-7b66-45ee-8907-34a6ca2c8e48", |
| 138 | + "id": "5c775a9e-bae4-4c99-86ad-1d5d6e294421", |
33 | 139 | "metadata": {},
|
34 | 140 | "outputs": [],
|
35 | 141 | "source": []
|
|
0 commit comments