Skip to main content

A zero-boilerplate canvas drawing framework for Python 3, based on Pygame.

Project description

A zero-boilerplate canvas drawing framework for Python 3, based on Pygame.

Some examples

Here’s some of the neat stuff you can do:

# import all
from drawzero import *

# simple shapes
fill('#12bbae')
line('red', (400, 400), (800, 800))
circle('yellow', (500, 560), 200)
filled_circle('brown', (500, 500), 20)
text('red', 'Hello, world!', (300, 200), 72)
rect('blue', (200, 600), 100, 100)
filled_rect('orange', (400, 600), 100, 100)
polygon('white', [(20, 200), (100, 240), (40, 160)])
filled_polygon('burlywood', 200, 400, 130, 304, 20, 342, 20, 458, 130, 496, )
https://raw.githubusercontent.com/ShashkovS/drawzero/master/doc/hello_world.png

Animation

Animations are also straightforward:

from drawzero import *
from math import sin, cos, pi

earth_orbit = 400
earth_radius = 30
earth_rot_step = 2 * pi / 360
moon_orbit = 100
moon_radius = 10
moon_rot_step = 2 * pi / 60

i = 0
while True:
    i += 1
    e_x = 500 + earth_orbit * cos(earth_rot_step * i)
    e_y = 500 + earth_orbit * sin(earth_rot_step * i)
    m_x = e_x + moon_orbit * cos(moon_rot_step * i)
    m_y = e_y + moon_orbit * sin(moon_rot_step * i)

    clear()
    filled_circle(C.red, (500, 500), 100)
    filled_circle(C.blue, (e_x, e_y), earth_radius)
    filled_circle(C.yellow, (m_x, m_y), moon_radius)
    tick()
https://raw.githubusercontent.com/ShashkovS/drawzero/master/doc/planet_animation.gif

Transparency

Transparency is also straightforward via alpha or RGBA:

from drawzero import *

clear()
fill(C.black)

filled_circle('red', (100, 100), 20)
filled_circle('blue', (100, 110), 22, alpha=100)
circle('red', (100, 100), 50, line_width=10)
circle((0, 255, 0, 50), (100, 110), 50, line_width=10)

filled_rect(C.aquamarine, (200, 100), 100, 40)
filled_rect(C.darkmagenta, (210, 110), 100, 40, alpha=80)
rect(C.darkgoldenrod, (180, 90), 200, 80, line_width=10)
rect(C.hotpink, (190, 90), 200, 90, alpha=180, line_width=10)

line('red', 600, 400, 600, 990)
image('cat.png', (500, 500))
image('cat.png', (500, 800), width=200, alpha=128)
image('cat.png', (550, 850), width=200, alpha=128)

polygon('yellow', [(20, 300), (100, 340), (40, 260)], line_width=20)
polygon((0, 0, 255, 200), [(20, 300), (100, 340), (40, 260)], line_width=15)
polygon('red', [(20, 300), (100, 340), (40, 260)])

filled_polygon('burlywood', 200, 600, 130, 504, 20, 542, 20, 658, 130, 696)
filled_polygon(C.hotpink, 200, 700, 130, 604, 20, 642, 20, 758, 130, 796, alpha=100)

line(C.green, (700, 100), (800, 200))
line(C.green, (710, 100), (810, 200), line_width=5)
line(C.red, (820, 100), (720, 200), line_width=10, alpha=50)
line(C.blue, (830, 100), (730, 200), line_width=10, alpha=128)
https://raw.githubusercontent.com/ShashkovS/drawzero/master/doc/transparent.png

Keyboard and mouse events

Process keyboard events in two ways: check if key is pressed via get_keys_pressed() or run throw keysdown or keysup events:

from drawzero import *

upper_text = 'Typed: '
SIZE = 20
x = y = 500 - SIZE // 2

while True:
    # Mouse buttons events
    if mousebuttonsdown:
        x, y = mousebuttonsdown[0].pos
    # Keys which are still pressed
    keys = get_keys_pressed()
    if keys[K.UP] or keys[K.w]:
        y -= 5
    if keys[K.DOWN] or keys[K.s]:
        y += 5
    if keys[K.LEFT] or keys[K.a]:
        x -= 5
    if keys[K.RIGHT] or keys[K.d]:
        x += 5
    # Keyboard events
    for ev in keysdown:
        if ev.unicode:
            upper_text += ev.unicode

    # Redraw everything
    clear()
    filled_rect(C.red, x, y, SIZE, SIZE)
    text(C.white, upper_text, (100, 5))
    filled_circle(C.yellow, mouse_pos(), 3)
    tick()
https://raw.githubusercontent.com/ShashkovS/drawzero/master/doc/keyboard_and_mouse_events.gif

Installation

In a Terminal window, type:

pip install drawzero --upgrade --user

Or run the following program:

import os, sys
python = sys.executable
user = '--user' if 'venv' not in python else ''
cmd = f'"{python}" -m pip install drawzero --upgrade {user}'
print(cmd)
os.system(cmd)

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

drawzero-0.2.2.tar.gz (307.2 kB view details)

Uploaded Source

Built Distribution

drawzero-0.2.2-py3-none-any.whl (13.8 kB view details)

Uploaded Python 3

File details

Details for the file drawzero-0.2.2.tar.gz.

File metadata

  • Download URL: drawzero-0.2.2.tar.gz
  • Upload date:
  • Size: 307.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.26.0 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.60.0 importlib-metadata/4.11.3 keyring/21.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.6

File hashes

Hashes for drawzero-0.2.2.tar.gz
Algorithm Hash digest
SHA256 3c02de3caaa642605b5ab4f3468eb337750a06a2e69819972384de451cdc7513
MD5 8e9b7ad18d3a8f3607585c356aa1e491
BLAKE2b-256 b95d145ff9879d290fc0c3375efae8fe6669b5ad4c76b8ee45f9d304d40cefff

See more details on using hashes here.

File details

Details for the file drawzero-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: drawzero-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 13.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.26.0 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.60.0 importlib-metadata/4.11.3 keyring/21.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.6

File hashes

Hashes for drawzero-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1e419d02463f0cd31a3f76707c31bca9cd89485e45fe2ac364d6b53825ae90c2
MD5 c6f87ec3514ae340658fcf719d379b27
BLAKE2b-256 cf93ed8d7f0871cc9296e53b272b28a00ce5b20a84e35ff46a530dd8b1cbd5b1

See more details on using hashes here.

Supported by

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