KayPy Discord GitHub

KayPy

Write a game in Python.

Open the playground Get started

from kaypy import *

kaypy(width=800, height=600, background=[141, 183, 255])

loadSprite("bean", "images/bean.png")
setGravity(1600)

player = add([sprite("bean"), pos(120, 80), area(), body()])

@onKeyPress("space")
def jump():
    if player.isGrounded():
        player.jump(800)

That is a whole game — a character that falls, lands and jumps. There is no run() call and no main loop to write. The loop starts on its own once your file has been read, so the first thing a beginner writes is the part they came for.

Nothing to install, to start

The playground runs Python in your browser. No account, no setup, no download — write a game and press Run. Try it →

Games are built from parts

A player is a sprite, plus a position, plus a body. Add one component, get one behavior. How that works →

One file, one web page

kaypy web game.py builds a single HTML file. Double-click it to play; upload the same file to itch.io. How →

Written for a classroom

Thirteen lessons from an empty file to enemy AI, and error messages aimed at somebody three weeks into programming. The guide →

Teaching with this, or stuck on something? There is a Discord.

Games are built from parts

KayPy is pygame-ce underneath, all the way down. What it adds is a shape for a game. Instead of one while True loop where the whole game happens at once, you describe an object by listing what it has:

player = add([
    sprite("bean"),  # it looks like this
    pos(100, 200),   # it is here
    area(),          # it has a collision box
    body(),          # gravity, jumping, isGrounded()
    "player",        # a tag, for finding it later
])

Each line does one thing, so a student can add one line and watch one thing change. Delete body() and the player floats. Add health(3) and it can be hurt. Nothing else in the file has to be rewritten to make room, which is the part that usually ends a beginner's first game.

Collisions read the same way — say what two tags do when they touch, and the engine watches for it:

@player.onCollide("coin")
def collect(coin):
    coin.destroy()
    play("score")

Errors in words, not stack traces

When a game crashes, KayPy trims the traceback down to the line in your file and says what went wrong in a sentence. A beginner who misspells a sprite name gets told they misspelled a sprite name — not forty lines of engine internals with their own mistake buried in the middle.

Take it home

A game written in the playground runs unchanged on your own computer:

pip install kaypy
python game.py

and kaypy new mygame sets up a folder with the sprites already in it. Nothing you write here is stuck here — not in this browser, not on this site, not in a format only we can open.

Where the API comes from

The function names follow KAPLAY, a JavaScript game engine with unusually good documentation. You do not need to know it or ever touch JavaScript. It means only that when you search for how to do something, there are examples to find — and they translate almost line for line into Python.