Skip to main content

PlayPy is a lightweight Python library for creating simple games and interactive applications with ease. It provides a straightforward API for handling graphics, input, and basic game mechanics, making it ideal for beginners and those looking to quickly prototype their ideas.

Project description

PlayPy (0.3.0)

PlayPy is a lightweight Python library for creating games, tools, and interactive applications using a retained-mode UI and scene system built on top of pygame. It focuses on rapid prototyping, composable rendering, and simple but powerful layout primitives.

Requirements

  • Python >=3.11
  • pygame(-ce) >=2.6.1

Installation

pip install playpy

If that does not work:

python -m pip install playpy

Core Concepts

Workspace

Workspace owns:

  • the window
  • render loop
  • input state
  • scene stack
  • coroutine scheduler

Key methods:

ws.run()
ws.quit()

ws.queue_scene_change(scene)
ws.queue_scene_push(scene)
ws.queue_scene_pop(scene=None)

ws.step()

Scene Scope

Temporarily push a scene using a context manager:

with ws.scene_scope(scene) as (scn, handle):
    ...

Call:

handle.disconnect()

to remotely pop the scoped scene.


Layout Values

PlayPy uses two rectangle value types:

FRect

Relative scale values.

plp.FRect(x, y, w, h)

Rect

Absolute pixel offsets.

plp.Rect(x, y, w, h)

Final element rectangle:

(scale * parent_size) + offset

Helpers:

plp.empty_rect()
plp.empty_frect()
plp.full_screen_rect()

Rect types are iterable and support multiple constructor overloads.


Parenting

Any Element can contain children.

child.parent = parent

or:

parent.add_child(child)

Relationship helpers:

is_parent_of()
is_child_of()

is_ancestor_of()
is_descendant_of()

Properties:

parent
children
ancestors
descendants

Rendering System

PlayPy now uses a compositing pipeline.

Element.draw() returns a SurfaceHandler instead of drawing directly to the workspace.

This enables:

  • outlines for arbitrary shapes
  • gradients inheriting shape alpha
  • subtree compositing
  • layered visual effects
  • future post-processing support

Elements

Core Elements

  • Panel
  • Text
  • Button
  • Textbox
  • Line
  • Scene

Effects

Effect is a subclass of Element that visually modifies its parent subtree.

Built-in effects:

  • Gradient
  • ButtonGradient
  • Outline
  • BorderRadius

Effects are ordered using z.

Typical layering:

Negative z:
    backgrounds/gradients

Normal z:
    content

Positive z:
    outlines/glows/overlays

Components

Components modify behavior/layout but do not draw.

Built-in components:

  • Padding
  • Font
  • Camera
  • Scrollable
  • GlobalElement

Attach/get/remove:

element.set_component(component)

element.get_component(ComponentType)

element.remove_component(ComponentType)

or:

component.parent = element

Input State

Access input using:

workspace.input

Properties:

keys_pressed
key_downs
key_ups

mouse_buttons_pressed
mouse_downs
mouse_ups

mouse_pos
mouse_delta
mouse_wheel

text_input

dt
runtime
quit

Helper methods:

key_held()
key_down()
key_up()

mousebutton_held()
mousebutton_down()
mousebutton_up()

Hover State

Workspace hover helpers:

is_mouse_top(element)
is_mouse_over(element)

just_hovered(element)
just_unhovered(element)

just_hovered_inclusive(element)
just_unhovered_inclusive(element)

Event helpers:

@on_hover(...)
@on_unhover(...)
@while_hovered(...)

@on_hover_inclusive(...)
@on_unhover_inclusive(...)
@while_hovered_inclusive(...)

Events

Decorator helpers create Event elements.

@plp.on_start(target)
@plp.on_update(target)
@plp.on_quit(target)

@plp.on_scene_change(target)

@plp.create_event(target, condition)

Events attached to the main workspace are global by default.

Example:

@plp.on_update(ws)
def tick(w: plp.Workspace):
    if w.input.key_down(plp.Key.ESCAPE):
        w.quit()

Coroutines

Event-like functions can yield.

If a handler returns a generator, PlayPy resumes it on future frames.

Example:

def flash(_: plp.Workspace):
    for _ in range(60):
        print("frame")
        yield

Supported in:

  • event callbacks
  • button callbacks
  • textbox callbacks
  • other event-like handlers

Textbox Features

Textbox supports:

  • placeholders
  • caret blinking
  • text confirmation/reverting
  • character filtering
  • maximum length
  • coroutine callbacks

Useful arguments:

is_char_accepted=
on_text_updated=
confirm_on_click_off=

Special keys:

Backspace -> remove character
Delete    -> clear text
Return    -> confirm text
Escape    -> revert text

Scenes

Scenes support lifecycle hooks:

on_enter()
on_exit()
on_pause()
on_resume()

Scene changes are queued internally.


Cameras and Scrolling

Camera offsets descendant elements.

Scrollable extends camera functionality with built-in scrolling support.

Example:

scrollable = plp.Scrollable()
scrollable.parent = panel

Logging

PlayPy replaces standard exceptions/logging with a categorized logging system.

plp.log(severity, category, message)

Severities:

plp.Severity.INFO
plp.Severity.WARNING
plp.Severity.ERROR
plp.Severity.CRITICAL

ERROR and CRITICAL are treated as NoReturn.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

playpy-0.3.0.tar.gz (23.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

playpy-0.3.0-py3-none-any.whl (28.8 kB view details)

Uploaded Python 3

File details

Details for the file playpy-0.3.0.tar.gz.

File metadata

  • Download URL: playpy-0.3.0.tar.gz
  • Upload date:
  • Size: 23.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for playpy-0.3.0.tar.gz
Algorithm Hash digest
SHA256 8c76b8207a91d1d4602af2ea59246c25c4faedd45e819aab0e75069df339d43a
MD5 4066da036da0477a350822e4992b6695
BLAKE2b-256 6a39651da9176b86ac3f4b47704902294eddfe6947f85567b844a11edf1ae692

See more details on using hashes here.

File details

Details for the file playpy-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: playpy-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 28.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for playpy-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 21aab9ed88150ed115cce7fee6f721a7d814fdaaaa91f5cb16bdc45dc08fe5e6
MD5 a71e6ce33ecd6891a69dd1ebc8a77bb4
BLAKE2b-256 1f6f0d6ea4389a49ab415678f2fd565ca622efa756aa8f1dcf28cbf76c88c6d2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page