Graphics library
Project description
PIX (pixpy)
A graphics library with a python interface. Designed for learning and 2D game development.
- Uses OpenGL/GLES2 to make it fast and portable
- Efficient Console/TileSet rendering for tile or text based games
- Composable Images using only GL textures
Install
pip install pixpy
For Linux, we need to build from source so dependencies must be installed first;
sudo apt install libxinerama-dev libxi-dev libxrandr-dev libxcursor-dev
The Basics
The following is a full program that opens a window and draws a circle;
import pixpy as pix
screen = pix.open_display(size=(1280,720))
screen.circle(center=(640,360), radius=300)
NOTE: This simple example works because pix is smart enough to "swap" the screen to automatically display what you have drawn, and then leave the window open and wait for the user to close the window, before the script ends.
Normally you create your own main loop and do this yourself;
import pixpy as pix
screen = pix.open_display(width=1280, height=720)
x = 0
while pix.run_loop():
screen.clear()
screen.circle(center=(x, screen.height/2), radius=x/4)
x += 1
screen.swap()
To read the keyboard and/or mouse, you can use is_pressed() or was_pressed()
import pixpy as pix
screen = pix.open_display(width=640, height=480)
background = pix.load_png("data/background.png")
sprite = pix.load_png("data/ufo.png")
# Starting position will be the center of the bottom of the screen
pos = pix.Float2(screen.size.x/2, screen.size.y - 50)
while pix.run_loop():
# Draw the background so it fills the screen
screen.draw(image=background, size=screen.size)
if pix.is_pressed(pix.key.RIGHT):
pos += (2,0)
elif pix.is_pressed(pix.key.LEFT):
pos -= (2,0)
screen.draw(image=sprite, center=pos)
screen.swap()
For more advanced needs you use events
# A simple paint program
import pixpy as pix
screen = pix.open_display(width=1280, height=720)
canvas = pix.Image(size=screen.size)
while pix.run_loop():
# Get all events generated this "frame"
for e in pix.all_events():
if isinstance(e, pix.event.Click):
# Zero length line just to remember last `end`
canvas.line(start=e.pos, end=e.pos)
elif isinstance(e, pix.event.Move):
if e.buttons:
# Draw from last end to new end
canvas.line(end=e.pos)
screen.draw(image=canvas)
screen.swap()
Float2 and Int2
The Float2 and Int2 classes acts like tuples of 2 elements, except allows for basic math operations. They are used to represents points and sizes throughout pixpy.
The act similar to pythons normal float and int, so for instance a true division between two Int2 will always be promoted to a Float2.
Images
All images are actually just references into Open GL Textures.This means that it's easy to cheaply manipulate images without doing a lot of copying.
One way to think of it is that an image is like an array slice or array view; it is cheap to create another view into an existing image.
For instance, you can crop an image like this;
cropped = image.crop(top_left=(10,10), size=image.size-(20,20))
cropped
now becomes a new view into the image, no need to duplicate the actual pixels.
(NOTE: In practice, an image is "a reference to a GL texture, and 4 pairs of UV coordinates".)
The Console
A major part of pix is the Console
In its simplest form, it can be used for text output and input.
You can also (re)define the tiles in the console and use it for graphics, such as a tile based platform game.
The console needs to be drawn to be visible, just like everything else.
Text output
The console starts out with a backing font that lets you write text;
import pixpy as pix
screen = pix.open_display(width=1280, height=720)
con = pix.Console(cols=80, rows=50)
con.write('Hello\n')
screen.draw(con)
Text input
console.read_line()
can be used to read lines of text. The result will be posted as a TextEvent.
import pixpy as pix
screen = pix.open_display(width=1280, height=720)
con = pix.Console(cols=40, rows=25)
con.write('What is your name?\n')
con.read_line()
while pix.run_loop():
for e in pix.all_events():
if isinstance(e, pix.event.Text):
con.write(f"Hello {e.text.rstrip()}!")
con.read_line()
screen.draw(con)
screen.swap()
Graphic tiles
Tiles can be both text and graphics. We can easily add more tiles to the console by copying images into it.
Note that the split()
call below is (as we mentioned) cheap -- we only create new views into the original image.
import pixpy as pix
screen = pix.open_display(width=1280, height=720)
con = pix.Console(cols=128, rows=128)
# Load our tile sheet, and split it into an array of tile images
tile_sheet = pix.load_png('tiles16x16.png')
tiles = tile_sheet.split(width=16, height=16)
# Iterate over all tile images, and copy them into the console tile map
for i,tile in enumerate(tiles):
# Get a reference to the image used to represent tile number i+1000.
# If there is no such tile, an image will be allocated for that tile.
console_tile = con.get_image_for(i+1000)
# Copy the actual pixels from one image to another.
console_tile.copy_from(tile)
con.put(pos=(5,5), tile=1000)
screen.draw(con)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for pixpy-0.1.8-pp39-pypy39_pp73-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 78f884c0ff2af1e39c93950922b2e46287c928244c250543aaa6aa716d097da7 |
|
MD5 | 3ff6024a74289b52eaed9818d055d9bc |
|
BLAKE2b-256 | fd2f126cdc2956e9623b9a39f66a5d37b41f152feeee3c062f10e0ea4cd85667 |
Hashes for pixpy-0.1.8-pp39-pypy39_pp73-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8ae0546630de9c4999fdea42293a0630f8f4d8873dc7defb129cd629bcd7c791 |
|
MD5 | 780217e6f36729b1cd8671bc4a3c60c8 |
|
BLAKE2b-256 | a7b196c1658b1864d54d8176780fbae61283885d7490f4c64d18f59cfe770be6 |
Hashes for pixpy-0.1.8-pp38-pypy38_pp73-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 393fa82c60d861e9c78a3ae9f481e12a00f5ea8e953c603b722076d363b4c14b |
|
MD5 | 12e57838aeb437f52c749ae67d579628 |
|
BLAKE2b-256 | 36c623009c3a5dc5f4b180b69c3a87eacc3a9ab9c1eb4bd71b7b86b5f39a593a |
Hashes for pixpy-0.1.8-pp38-pypy38_pp73-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 98c33f4da754014b171abf745815485614902c99df3ef8baaf9c6eb813fdf999 |
|
MD5 | 5c4dc35af78c74978ecaf8cb6be12909 |
|
BLAKE2b-256 | 23be5c5b5f29a19b562b246fe5340af054b12d20496cd658ef8afc13ffa0c37a |
Hashes for pixpy-0.1.8-pp37-pypy37_pp73-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | df6da93b196d1ea306b0ae16cd31f215c7cee91a6116eb01c5726a9a95c0d3ba |
|
MD5 | e8c6ebb5d8f2737e8a75c78a64bd0440 |
|
BLAKE2b-256 | 8c1ae2d96996702a9467d10d645d52f4950dff27138a7a6f223218a389ea9659 |
Hashes for pixpy-0.1.8-pp37-pypy37_pp73-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dca07c23e1cd993cb115045b36b99d192791b07373bc3ee49b59ce7629393f86 |
|
MD5 | cd2427ba5f86dcdeb33d8eafddcb3735 |
|
BLAKE2b-256 | 8ee6d90002eacca6d26fa45e455013b38e5c791354ba197f7038ff3eafc70129 |
Hashes for pixpy-0.1.8-cp311-cp311-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e81483f985f6ee36e07e7ad0bb9384a889d19aedc5a4498467464aca66761588 |
|
MD5 | ce7a8820e51adee74ebbbd2f503f51cf |
|
BLAKE2b-256 | 5b0b7b1de98c0970aa48480231583bb580cde474ee53c4dae7a3fbe8f9830570 |
Hashes for pixpy-0.1.8-cp311-cp311-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c87730ba5aa3597365324e9420ca32085ad39704b6ad195b711998671f6b2b67 |
|
MD5 | 55542cfa5ff706f8292f208136b6039b |
|
BLAKE2b-256 | 749106252da00579a0f6d9f1269ebb8d41e719661d3c09e304dd922aa5f1b1f7 |
Hashes for pixpy-0.1.8-cp311-cp311-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 32fb6c88c44fa57a7c0397d6e1487741ef7834aeabaa3737637fec272e6e6af3 |
|
MD5 | e829f8a4fa7cad452098881fb01110cd |
|
BLAKE2b-256 | c11e4edd6b0a1c476f38c7afe9176f408351e6d5fbfc26d3968720c889ae5f91 |
Hashes for pixpy-0.1.8-cp311-cp311-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5e87167f1c46853cb17089dd2a910c118e107e507a2c3a9c3908b09753699fe9 |
|
MD5 | 4169938be466c0173fcd63b784a7a21b |
|
BLAKE2b-256 | 7b934c17573f5ec2b8c5b7c6bb899b3025b919abc756bfabd42a1cb18b62272f |
Hashes for pixpy-0.1.8-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c5ddc0db49507c11e0c91fd893687de85b93c8e5a6137fd69208cfbf4270f01c |
|
MD5 | db5b33313e44673a119cdb2cbb647bdd |
|
BLAKE2b-256 | 36293d5034f58170b434ca7bb17f36eaba6b11e946f20ee2b40660e72c059423 |
Hashes for pixpy-0.1.8-cp310-cp310-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 155c9f44fc2e705735558de80faa7b38a8f2333a0c3649108e87e608b6581edb |
|
MD5 | 16f8373990246e8b2c5185c542536419 |
|
BLAKE2b-256 | 949bd0e5d5e755fbe43af229e28c43ab9262d9969377e4970ae93bfddd8838e0 |
Hashes for pixpy-0.1.8-cp310-cp310-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 43e437f545de614397f7a542c846fd48815414b533c5a1caf29090eb745e03f2 |
|
MD5 | 4c296a32eca352e2e2e2b464e3a815e6 |
|
BLAKE2b-256 | 3ec0b09d5f69dcf5e9a2d10ea62a61c28490b4d7ef81e62cf442b3e8cad2325f |
Hashes for pixpy-0.1.8-cp310-cp310-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6716a25f06d6b46397357ed995c95ed6c6db4b06602faa910c01d8f9ac191dfd |
|
MD5 | c6288ecd02143a238ab5d54da3ed5994 |
|
BLAKE2b-256 | b2beb94472b1631cec57bdca40f62f82b832debc1c19853dad8ae99b9cd193c1 |
Hashes for pixpy-0.1.8-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 85c39d2cba523a5cbee833c7465f60a4f878d7c72b4bde0ec0daea48242d4c98 |
|
MD5 | ca40936a35306819b35c65f8a006780d |
|
BLAKE2b-256 | 1666c426d8bdcc270f63e0dc3806d8a66570d78b4314a7da52f959b14d2e8040 |
Hashes for pixpy-0.1.8-cp39-cp39-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7a9832ae7be27942c26ed47aa2beed9401127d609d03bdfe10cab0faba9aeb0e |
|
MD5 | ac44ef7fed34c7c1624c7ddf20c1f90e |
|
BLAKE2b-256 | 467e69f5f8906dfbd53e064f82fc09d0ce15e8693dc293cc591bf367d3d351a5 |
Hashes for pixpy-0.1.8-cp39-cp39-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 87815790dca67886efd40ace34aa36ec8601ec165d3d9f05a9998f2c5c22078b |
|
MD5 | 6983da603fbd5fc33f2e367d891f0bcf |
|
BLAKE2b-256 | 48dd3af602b23f67905777874440e6eb0186b35f3c09b3a339a61eaf983473fd |
Hashes for pixpy-0.1.8-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2595a547e36691e0d8a5f77b7e875eeab8ab3d7ad27e256635af33f2de53fa84 |
|
MD5 | 578e215c351838f97f6a635705b58164 |
|
BLAKE2b-256 | 89f40115a7e40935f9e1638868b8b6d2eadf2fe3d7cd9a5240a275b2fb97672a |
Hashes for pixpy-0.1.8-cp38-cp38-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 49e665be2aea9a6d25c49e2a65c5b23b0930274c5d81b084ff2494709c52d048 |
|
MD5 | 6e0ca746006eea92dd9809d7a8d814ec |
|
BLAKE2b-256 | 4486978962395b2cd96ecaa6c81b67bd22e8bb9a55b2f30f88b92318bdb2d950 |
Hashes for pixpy-0.1.8-cp38-cp38-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a201e0d10902e57fa7e27bc8137c6298ae2fc6c36b9ef1e0db7059a05bbeda7e |
|
MD5 | db21f4c4930819788391ae74f6f64631 |
|
BLAKE2b-256 | 5e884610a82e42e5170d739f6886187eaa50870a85831c9beffb7e9ff0ec4dc6 |
Hashes for pixpy-0.1.8-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a8d4877b133f121f662ce9a1fe3023fb2152137666d1bb3de0aa79a5a301afcb |
|
MD5 | d46da302b45fac560400fe53d61604a0 |
|
BLAKE2b-256 | 33c4081ffe3ddd034d2059cfb5ebe47858409016a87ad46e27fd3cb1fbe2b6c8 |
Hashes for pixpy-0.1.8-cp37-cp37m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4c8780b02bfff52934f6c2d5f9ae2911a3e4ea23ad86eb5a940eb2b219da2cae |
|
MD5 | d1c5dff7f4bd879115d92f4ece3c37f3 |
|
BLAKE2b-256 | cae89a17678a3917d7c6efdd76e0d172683d078becc9480815bc3d2069f2d883 |
Hashes for pixpy-0.1.8-cp37-cp37m-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b8ffe887d42d4497952d7550dbbca96af79da1a6edc070ad6d5b49c1b5f23bc9 |
|
MD5 | 2e65ad2067040177b372e81df2bf581b |
|
BLAKE2b-256 | 65e2a40f7f737783c0dd6a0a0bdfc269ce813e20f831bd8c3efe77d881f5ecd7 |