Bringing Pygame to the Browser with Pygbag

in #python6 days ago

Ever wanted to create a browser-based video game or utility but didn't want to learn a whole lot of new 'stuff'?

This is the situation I was in recently. I had picked up some of my old computer magazines, and a maze-generation algorithm caught my eye. I thought I could share it here, but I didn't really want to have my desire for a playable demo dictate which language to use.

Enter Pygbag!

Pygbag makes it possible to run Pygame-based Python apps directly in a web browser using WebAssembly (wasm).

This means you can share your games and applications online without requiring users to install Python. It works on desktop and mobile browsers, making it a great option for bringing Python projects to the web.

WebAssembly is a portable binary-code format that runs almost as fast as native code. With Pygbag, your Pygame project gets converted into a format browsers can understand, allowing it to run smoothly online.

Setting Up Your Game for Pygbag

For Pygbag to work, your game needs to be structured properly. The key requirement is that your project folder, for example mazegen, must contain a main.py file. Inside this file, the game loop must be asynchronous using Python’s asyncio library.

Instead of running continuously, the event loop in a Pygbag game syncs with the display refresh rate, usually 60 times per second on desktops and sometimes lower on mobile. This helps keep performance smooth. To avoid lag or loading issues, it is best to preload any assets at the start of the game.

Installing Pygbag

To get started, install Pygbag along with Pygame if you have not already:

pip install pygame pygbag

Running mazegen in the Browser

Once your game is ready, running it in a browser is simple. Make sure your main script is called main.py, then open a terminal, navigate to the project folder, and run:

pygbag mazegen

This starts a local server at localhost:8000. Open that address in your browser and your game will run right there with no extra setup needed.

Using the --archiveparameter allows you to create a zip file for use on Itch.io rather than your website.

You can see it in action on my Itch account!

Posted Using INLEO

Sort:  

Loving Python more and more each day... There is an import for everything

https://xkcd.com/353/

This is cool! well done Makerhacks :)
I first started developing games with Unity3D, but lately, I've also been using Python (for the past few months). I remember once creating a simple game and connecting it to Telegram. I used Python and called it Monkey Punky. The main idea was to learn how to integrate it with Telegram so people could play. Players could earn points by sending a banana emoji to the monkey bot on Telegram. Their names were then saved in a database, and their points were recorded on a leaderboard to show who had the highest score.

Long story short, I stopped working on it when I realized I needed a server to keep it running 24/7. One website allowed me to run it for only 24 hours but it was also not what I needed, and then I tried with my laptop, but since I couldn’t keep my laptop running Python all the time, I eventually gave up on it.

I see you used Itch, I should try it in future.

Itch is fine for html5/js games but for backend python I think you'd need an actual server :/

I think https://replit.com/ might work for a free and low-traffic option ..

Thank you my friend, I will check it out and test my py game there

I see, it seems like a good platform to share games though.

Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).


 
You may also include @stemsocial as a beneficiary of the rewards of this post to get a stronger support.