Build a platformer game

Chapter 11 of 1240 min

Share it

  1. Game loop
  2. Player
  3. Platforms
  4. Level
  5. Camera
  6. Coins
  7. Enemies
  8. Online

In this chapter you will:

  • put your game on your own GitHub
  • make it run in a web browser, with one small change to the loop
  • publish it on itch.io and get a link anyone can play

Put it on your GitHub

Your project was cloned from the JrCodex starter, so Git still points there. Point it at your own copy instead.

  1. On github.com, sign in (or sign up), click New repository, name it pixel-run, keep it Public, and leave every box below unticked. Create it.
  2. In your terminal, with your own user name in place of YOUR-NAME:
git remote set-url origin https://github.com/YOUR-NAME/pixel-run.git
git push -u origin main

Open your repository page. Your code is there, and so is your history: one commit per chapter, the game growing step by step.

How a Python game runs in a browser

Browsers don't run Python. A tool called pygbag packs your game together with a version of Python that does run in a browser (it's compiled to WebAssembly, a format browsers can run fast).

There's one catch. A web page has a single line of work, shared between your game and the browser itself. Your while loop never stops, so the browser would never get a turn: the page would freeze. The fix is to make the loop pause for a moment at the end of every frame and give the browser its turn. Python does this with asyncio.

The change: an async main loop

Make these edits to main.py:

  1. Add import asyncio above import pygame.
  2. Put async def main(): on a new line just above state = "new".
  3. Select every line from state = "new" to the end of the file (including pygame.quit()) and press Tab, so they're all inside main.
  4. Under clock.tick(60), inside the loop, add await asyncio.sleep(0).
  5. At the very end of the file, with no indent, add asyncio.run(main()).

The middle and the end of the file now look like this:

main.py
level_width = len(LEVEL[0]) * TILE
 
 
async def main():
    state = "new"
    running = True
 
    while running:
        # 0. A new game: build the level and put the player at the start
        if state == "new":
 
        # ... everything else in the loop, one level further in ...
 
        pygame.display.flip()
        clock.tick(60)
        await asyncio.sleep(0)  # lets the browser keep up
 
    pygame.quit()
 
 
asyncio.run(main())
  • async def makes main a function that can pause and carry on later.
  • await asyncio.sleep(0) is the pause: "let anything else that's waiting run, then come back to me".
  • asyncio.run(main()) starts it.

The variables inside the loop (state, player, score and the rest) are now inside a function, which is fine: everything that uses them is in there too. On your computer, python main.py still runs the game exactly as before. Check that it does.

Try it in your browser

pip install pygbag
python -m pygbag .

The . means "the game in this folder". pygbag builds the web version and starts a small web server on your computer. Open http://localhost:8000 in your browser.

The first load takes a few seconds, while the browser downloads Python itself. Then click the page (browsers only start a game after you interact with it), and play. Stop the server with Ctrl+C in the terminal.

pygbag packs every file in the folder into the game, except folders it knows to skip, like venv and build. That's why chapter 2 asked you to call your virtual environment venv. If the first load is very slow, check that no other big folders are in your project.

Publish it on itch.io

itch.io is where independent game makers share their games, and it's free.

  1. Make the files to upload:
python -m pygbag --archive .

This creates build/web.zip.

  1. Sign up on itch.io, then open your account menu and choose Upload new project.
  2. Give it a title, and set Kind of project to HTML.
  3. Upload build/web.zip, and tick This file will be played in the browser.
  4. Under the embed options, set the size to 800 × 480.
  5. Set Visibility to Public and save.

Your game is at a link like https://your-name.itch.io/pixel-run. Send it to a friend, open it on another computer, put it on your CV.

build/ is already in your .gitignore, so the build files never end up in Git.

Predict, then check

You forgot step 4 of the code change, await asyncio.sleep(0). What happens on your computer, and what happens in the browser?

Show answer

On your computer the game still works, because nothing else needs a turn. In the browser the page freezes, since the loop never lets the browser draw your frames or read your keys. That's why a game can work perfectly locally and break online: always test the web version with python -m pygbag . before you upload.

Make the README yours

Your repository's README.md still describes the starter. Rewrite it for someone visiting your GitHub: what the game is, the link to play it on itch.io, the controls (arrows, space, R), how to run it locally, and a screenshot. Commit and push.

Save your progress

git add .
git commit -m "Chapter 11: playable in the browser"
git push

Stuck, or want to compare? The finished code for this chapter is tagged ch11: run git diff ch11, or see it on GitHub.