Python Game API Docs

Simple API for making browser games with Python

<- Back to Home

Getting Started

Every game needs to import game_api and create a Game object:

import game_api

game = game_api.Game(400, 400, "My Game")

def update():
    # Game logic here
    pass

def draw():
    # Draw stuff here
    game.clear()
    game.draw_text("Hello!", 150, 200, "white")

game.set_update(update)
game.set_draw(draw)
game.run(30)  # 30 FPS
Tip: update() handles game logic, draw() handles rendering. Keep them separate!

API Reference

Here are all the functions you can use:

Game(width, height, title)
Creates a new game canvas with the specified width and height in pixels.
game = game_api.Game(400, 400, "My Awesome Game")
game.clear()
Clears the canvas. Call this at the start of your draw() function!
game.draw_rect(x, y, width, height, color)
Draws a rectangle at position (x, y) with the given dimensions and color. Colors can be names like "red" or hex codes like "#ff0000".
game.draw_rect(100, 100, 50, 50, "red")
game.draw_rect(200, 200, 30, 30, "#00ff00")
game.draw_circle(x, y, radius, color)
Draws a circle at position (x, y) with the given radius and color.
game.draw_circle(200, 200, 25, "blue")
game.draw_text(text, x, y, color)
Draws text on the screen at position (x, y).
game.draw_text("Score: 100", 10, 20, "white")
game.draw_text("Game Over", 150, 200, "red")
game.set_key_handler(func)
Sets up a function to handle key presses. The function receives the key as a string (like "ArrowUp", "a", "Enter", etc).
def on_key(key):
    if key == "ArrowUp":
        y -= 10
    elif key == "ArrowDown":
        y += 10

game.set_key_handler(on_key)
game.set_update(func)
Sets the update function that runs your game logic every frame.
game.set_draw(func)
Sets the draw function that renders everything every frame.
game.game_over(message)
Displays a game over message and stops the game. Use this when the player loses.
if lives <= 0:
    game.game_over(f"Game Over! Score: {score}")
game.run(fps)
Starts the game loop at the specified frames per second (30-60 is good for most games).

Example: Simple Pong

Here's a basic pong game to get you started:

import game_api

game = game_api.Game(400, 300, "Pong")
paddle_y = 125
ball_x, ball_y = 200, 150
ball_dx, ball_dy = 3, 3

def update():
    global ball_x, ball_y, ball_dx, ball_dy, paddle_y
    
    ball_x += ball_dx
    ball_y += ball_dy
    
    # Bounce off top/bottom
    if ball_y <= 0 or ball_y >= 300:
        ball_dy *= -1
    
    # Bounce off paddle
    if ball_x <= 10 and abs(ball_y - paddle_y) < 50:
        ball_dx *= -1
    
    # Reset if ball goes off screen
    if ball_x > 400:
        ball_x, ball_y = 200, 150

def draw():
    game.clear()
    # Draw paddle
    game.draw_rect(0, paddle_y, 10, 50, "white")
    # Draw ball
    game.draw_circle(ball_x, ball_y, 5, "red")

def on_key(key):
    global paddle_y
    if key == "ArrowUp" and paddle_y > 0:
        paddle_y -= 10
    elif key == "ArrowDown" and paddle_y < 250:
        paddle_y += 10

game.set_update(update)
game.set_draw(draw)
game.set_key_handler(on_key)
game.run(60)