- Copy
g2d.py
into the same folder containing your scripts; this will draw into a PyGame window - Alternatively, copy
g2d_pyodide.py
in that location and rename the file asg2d.py
; this will draw into a browser window
- A
Color
is a tuple(red, green, blue)
; each value must be in the range0..255
- A
Point
is a tuple(x, y)
, representing a position or a size
init_canvas
(size: Point)
: Initialize the drawing canvasmain_loop
(tick: Callable=None, fps: float=30)
: Start the event loop, accepting an optionaltick
function, which will be called periodicallyclear_canvas
(background: Color=None)
: Clear the canvas, with white or an optionally providedbackground
colorclose_canvas
()
: Close the canvas and exit the main loop
set_color
(color: Color)
: Set the drawing colorset_stroke
(width: float=0)
: Set the stroke width for outlining shapes, instead of filling themdraw_line
(pt1: Point, pt2: Point)
: Draw a line frompt1
topt2
draw_circle
(center: Point, radius: float)
: Fill a circle, givencenter
andradius
draw_rect
(pos: Point, size: Point)
: Fill a rectangle, given left-top position and sizedraw_polygon
(points: list[Point])
: Fill a polygon, given its list of verticesdraw_text
(text: str, center: Point, size: float)
: Draw a text, given itscenter
position and font pxsize
load_image
(src: str) -> str
: Preload an image and return a name for itdraw_image
(src: str, pos: Point, clip_pos: Point=None, clip_size: Point=None)
: Blit an image, given its name and position; optionally, a rectangle is clipped from imageload_audio
(src: str) -> str
: Load a sound and return a name for itplay_audio
(src: str, loop: bool=False)
: Play a sound, possibly in a loop, given its namepause_audio
(src: str)
: Stop playing a sound, given its name
mouse_pos
() -> Point
: Get current mouse positionmouse_clicked
() -> bool
: Check if left mouse button has been clickedcurrent_keys
() -> list[str]
: Get all keys that are currently held downprevious_keys
() -> list[str]
: Get all keys held down at the previous frameprompt
(message: str) -> str
: Show a dialog for entering a line of textconfirm
(message: str) -> bool
: Show a dialog for confirming a decisionalert
(message: str)
: Show a dialog with a message
Key values + "Spacebar", "LeftButton", "RightButton", "MiddleButton"
--
Copyleft (ɔ) 2008-2024 Michele Tomaiuolo - https://tomamic.github.io/
This software is free - https://opensource.org/license/mit