Simple Graphics
simple graphics is a lightweight python graphics package that offers a more readable alternative to pygame
simple graphics is meant to be extremely readable, allowing you to focus on the logic of your code instead of graphic specifics
Installation
pip
pip install simple-graphics-lib
Quick Start
from simple_graphics import *
# Create shapes
rect = Rect(50, 50, 100, 100, color="blue")
circle = Circle(200, 200, radius=50, color="red")
# Define event handlers
@on_click(rect)
def on_rect_click():
print("Rectangle clicked!")
@on_press("space")
def on_space_press():
print("Space pressed!")
# Run the window
run(width=400, height=400, caption="My Graphics App")
Shapes
Simple Graphics offers support for various shapes, that will automatically appear on the screen when defined
Rect
Arguments:
| Argument | Type | Role | Default |
|---|---|---|---|
| x | int | The x position of the rect | None |
| y | int | The y position of the rect | None |
| width | int | the width of the rect | 10 |
| height | int | the height of the rect | 10 |
| color | str | the color of the rect | "black" |
| outline | bool | whether the rect is just an outline | False |
| draggable | bool | whether the rect can be dragged | False |
| visible | bool | whether the rect is drawn on screen | True |
Methods:
| Method | Purpose | Arguments | Returns |
|---|---|---|---|
| is_obj_over | determines if a point is in the rectangle | x: int, y:int | bool |
| get_area | returns the area of the rectangle | None | int |
| get_perimeter | returns the perimeter of the rectangle | None | int |
| rotate | rotates the rectangle by an angle in degrees | angle: int | None |
Circle
Arguments:
| Argument | Type | Role | Default |
|---|---|---|---|
| x | int | the x position of the circle | None |
| y | int | the y position of the circle | None |
| radius | int | the radius of the circle | 10 |
| color | str | the color of the circle | "black" |
| outline | bool | whether the circle is just an outline | False |
| draggable | bool | whether the circle can be dragged | False |
| visible | bool | whether the circle is drawn on screen | True |
Methods:
| Method | Purpose | Arguments | Returns |
|---|---|---|---|
| is_obj_over | determines if a point is in the circle | x: int, y:int | bool |
| get_area | returns the area of the circle | None | int |
Polygon
Arguments:
| Argument | Type | Role | Default |
|---|---|---|---|
| points | list[tuple] | the vertices of the polygon | None |
| color | str | the color of the polygon | "black" |
| outline | bool | whther the polygon is just an outline | False |
| draggable | bool | whether the polygon can be dragged | False |
| visible | bool | whether the polygon is drawn on screen | True |
Methods:
| Method | Purpose | Arguments | Returns |
|---|---|---|---|
| is_obj_over | determines if a point is in the polygon | x: int, y:int | bool |
| rotate | rotates the polygon by an angle in degrees, optionally around a point | angle: int, around: tuple | None |
Line
Arguments:
| Argument | Type | Role | Default |
|---|---|---|---|
| points | list[tuple] | the points that the line will connect | None |
| width | int | the width of the line | 10 |
| color | str | the color of the line | "black" |
| draggable | bool | whether the line can be dragged | False |
| visible | bool | whether the line is drawn on screen | True |
Methods:
| Method | Purpose | Arguments | Returns |
|---|---|---|---|
| is_obj_over | determines if a point is over the line | x: int, y:int | bool |
| rotate | rotates the line by an angle in degrees, optionally around a point | angle: int, around: tuple | None |
Text
Arguments:
| Argument | Type | Role | Default |
|---|---|---|---|
| x | int | the x value of the text | None |
| y | int | the y value of the text | None |
| text | str | the text of the text | None |
| color | str | the color of the text | "black" |
| size | int | the size of the text | 36 |
| draggable | bool | whether the text can be dragged | False |
| visible | bool | whether the text is drawn on screen | True |
Methods:
| Method | Purpose | Arguments | Returns |
|---|---|---|---|
| is_obj_over | determines if a point is in the text | x: int, y:int | bool |
Image
Arguments:
| Argument | Type | Role | Default |
|---|---|---|---|
| x | int | the x value of the image | None |
| y | int | the y value of the image | None |
| img_path | str | the path to the image | None |
| width | int | the width of the image | the width from the image path |
| height | int | the height of the image | the height from the image path |
| draggable | bool | whether the image can be dragged | False |
| visible | bool | whether the image is drawn on screen | True |
Methods:
| Method | Purpose | Arguments | Returns |
|---|---|---|---|
| is_obj_over | determines if a point is in the image | x: int, y:int | bool |
| rotate | rotates the image by an angle in degrees | angle: int | None |
Rotation
Shapes that support rotation use rotate(angle), where angle is measured in degrees. Each call adds to the current rotation. Rectangles and images rotate around their center by default. Polygons and lines can optionally rotate around a specific (x, y) point.
rect = Rect(100, 100, 80, 40, color="blue")
polygon = Polygon([(200, 100), (250, 150), (200, 200)], color="red")
line = Line([(50, 50), (100, 50)], width=5)
rect.rotate(45)
polygon.rotate(30)
line.rotate(90, around=(75, 50))
Group
Group groups a bunch of objects together, so you can treat them all as one shape, modifying x,y and color and checking for collisions for every shape in the group at once
the below code creates a group of two rectangles
r1 = Rect(20, 20)
r2 = Rect(40, 40)
group = Group(r1, r2)
group.x += 10
group.y -= 10
group.color = "green"
Arguments:
| Argument | Type | Role | Default |
|---|---|---|---|
| *shapes | list[Shape] | the shapes to group | None |
| visible | bool | whether shapes in group are drawn on screen | True |
Methods:
| Method | Purpose | Arguments | Returns |
|---|---|---|---|
| add | appends shapes to the group | *shapes | None |
| remove | removes shapes from the group | *shapes | None |
| clear | clears the group | None | None |
| is_obj_over | checks if a point is over the group | x, y | bool |
Examples:
r1 = Rect(20, 20)
r2 = Rect(40, 40)
group = Group(r1, r2)
c1 = Circle(100,100)
t1 = Text(300, 300, "hi")
group.add(c1, t1)
group.remove(r2, t1)
Run
run() is Simple Graphics main function.
it creates a screen where all defined objects appear and can be interacted with.
Arguments:
| Argument | Type | Role | Default |
|---|---|---|---|
| width | int | The screen width in pixels | 200 |
| height | int | the screen height in pixels | 200 |
| resizable | bool | whether or not the screen is resizable via dragging | True |
| caption | str | the name of the graphics window | "SG window" |
| fps | int | the frames per second of the game | 60 |
Collisions
to check for a collision between 2 shapes, use the is_colliding() method:
rect1 = Rect(10, 10)
ball = Circle(10 , 5)
print(is_colliding(ball, rect1)) # True
Events
Instead of a for loop, Simple Graphics handles events using decorators
on_tick
The on_tick decorator runs the function every frame or every n frames if specified
The default framerate is 60 FPS
Functions with the on_tick decorator may not take arguments
@on_tick #runs every frame
def main():
pass
@on_tick(3) #runs every 3 frames
def every3():
pass
run()
on_press
the on_press runs the decorator whenever a specified button is pressed, or when any button is pressed if none are specified
The function below will run whenever w or s is pressed
@on_press("w,s")
def do_w():
do_soemthing()
run()
The function below will run whenever any key is pressed
@on_press
def any_key():
do_something()
run()
Functions with the @on_press decorator may only take 1 argument, which will show up as the key pressed
@on_press
def print_key(key):
print(key)
run()
on_hold
the on_hold decorator functions exactly like the on_press decorator, but the function runs every frame for as long as the key is held, instead of just once
The function below will run for as long as w or s is pressed
@on_hold("w,s")
def do_w():
do_soemthing()
run()
The function below will run while any key is pressed
@on_press
def any_key():
do_something()
run()
Functions with the @on_hold decorator may only take 1 argument, which will show up as the key held
@on_press
def print_key(key):
print(key)
run()
on_click
functions defined with the on_click decorator will run whenever the specified object is clicked, or whenever any click is detected if none is specified
the function below will run whenever the button rect is clicked
button = Rect(10, 10)
@on_click(button)
def click_button():
print("button clicked")
run()
the function below will run when there is any click at all
@on_click
def print_click():
print("clicked!")
run()
on_hover
functions defined with the @on_hover decorator will run whenever the specified object is being hovered over
the function below will run whenever the mouse is over the rectangle
r1 = Rect(200,200, 100, 100)
@on_hover(r1)
def p():
print("hovering")
run(400,400)
on_drag
functions defined with the @on_drag decorator will run whenever the specified object is being dragged
the function below will run whenevr the r1 rect is being dragged
r1 = Rect(200,200,draggable = True)
@on_drag(r1)
def do_something():
print("dragging r1")
run(400,400)
The mouse
A mouse object is already initialized in the Simple Graphics import
to get mouse coordinates, simply use mouse.x and mouse.y
Example:
@on_tick
def main():
print(mouse.x, mouse.y)
run()
dragging
to make a shape draggable, add the keyword argument to the object initialization:
r = Rect(250,250, draggable = True)
this works for all shapes including images and text
Miscellaneous Functions
set_bg
set_bg() simply sets the background to the specified color
the default background color is white
set_bg("black")
changes the background color to black
clear_screen
clear_screen() clears the screen.
objects defined before a screen clear can no longer be interacted with
erase
erase() deletes a single object
erased objects can no longer be invoked
gamestate object
instead of using the global keyword at the top of your functions, you can instead assign your variable as part of the game class
game.x = 10
@on_tick
def main():
game.x += 1
either this or the global method will work
Metadata
Release files for simple-graphics-lib 0.0.12
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| simple_graphics_lib-0.0.12.tar.gz | 16.3 kB | Without provenance | Details for simple_graphics_lib-0.0.12.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| simple_graphics_lib-0.0.12-py3-none-any.whl | Python 3 | none | any | Details for simple_graphics_lib-0.0.12-py3-none-any.whl |
Total release size: 29.4 kB
Release files / simple_graphics_lib-0.0.12.tar.gz
| Download URL | simple_graphics_lib-0.0.12.tar.gz |
|---|---|
| Size | 16.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e867cb92bedf858c8d90cc52ebd23cc3b87187fb8529a891325ce6c7a15656ac
|
|
BLAKE2b-256 checksum How to use checksums |
4076cc6d4f2519f03841ca6b97d20f886c99ce2c55ab801470bc9e81b08aec20
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.15.2
|
Release files / simple_graphics_lib-0.0.12-py3-none-any.whl
| Download URL | simple_graphics_lib-0.0.12-py3-none-any.whl |
|---|---|
| Size | 13.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5a2ee0872ca9aee5a2cc992fcd054b3fd84d67ef22d8efbe2d33e68c959e7f95
|
|
BLAKE2b-256 checksum How to use checksums |
ff3c5b9972e77f85bd2503df1045f9a2b2ff3888ff40a00ade3bfc90a272d3b8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.15.2
|
Security for simple-graphics-lib 0.0.12
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.