simpleguics2pygame — canvas

simpleguics2pygame module: simpleguics2pygame/canvas.

Class Canvas.

Piece of SimpleGUICS2Pygame. https://bitbucket.org/OPiMedia/simpleguics2pygame

license

GPLv3 — Copyright (C) 2015-2016, 2020-2021 Olivier Pirson

author

Olivier Pirson — http://www.opimedia.be/

version

May 4, 2021

class SimpleGUICS2Pygame.simpleguics2pygame.canvas.Canvas(frame: Optional[pygame.Frame], canvas_width: int, canvas_height: int)[source]

Canvas similar to SimpleGUI Canvas of CodeSkulptor.

__init__(frame: Optional[pygame.Frame], canvas_width: int, canvas_height: int) None[source]

Set the canvas.

Don’t use directly, a canvas is created by Frame() and reachable by handler defined by Frame.set_draw_handler().

Parameters
  • frame – Frame (or None)

  • canvas_width – int >= 0

  • canvas_height – int >= 0

__repr__() str[source]

Return ‘<Canvas object>’.

Returns

str

__weakref__

list of weak references to the object (if defined)

_background_pygame_color = '#000000'

Default pygame.Color of the background of the canvas.

_bg_pygame_surface_image: Optional[pygame.surface.Surface] = None

pygame.surface.Surface default background image replaces _background_pygame_color.

_draw() None[source]

If self._draw_handler != None then call it and update display of the canvas.

(Not available in SimpleGUI of CodeSkulptor.)

_save(filename: str) None[source]

Save the canvas in filename.

Supported formats are supported formats by Pygame to save: TGA, PNG, JPEG or BMP (see https://www.pygame.org/docs/ref/image.html#pygame.image.save ).

If filename extension is not recognized then TGA format is used.

(Not available in SimpleGUI of CodeSkulptor.)

Parameters

filename – str

draw_arc(center_point: Sequence[Union[int, float]], radius: Union[int, float], start_angle: Union[int, float], end_angle: Union[int, float], line_width: Union[int, float], line_color: str) None[source]

Draw an arc of circle, from start_angle to end_angle. Angles given in radians are clockwise and start from 0 at the 3 o’clock position.

(Available in CodeSkulptor3 but not in CodeSkulptor2!)

Parameters
  • center_point – (int or float, int or float) or [int or float, int or float]

  • radius – (int or float) > 0

  • start_angle – int or float

  • end_angle – int or float

  • line_width – (int or float) > 0

  • line_color – str

draw_circle(center_point: Sequence[Union[int, float]], radius: Union[int, float], line_width: Union[int, float], line_color: str, fill_color: Optional[str] = None) None[source]

Draw a circle.

If fill_color != None then fill with this color.

Parameters
  • center_point – (int or float, int or float) or [int or float, int or float]

  • radius – (int or float) > 0

  • line_width – (int or float) > 0

  • line_color – str

  • fill_color – None or str

draw_image(image: SimpleGUICS2Pygame.simpleguics2pygame.image.Image, center_source: Sequence[Union[int, float]], width_height_source: Sequence[Union[int, float]], center_dest: Sequence[Union[int, float]], width_height_dest: Sequence[Union[int, float]], rotation: Union[int, float] = 0) None[source]

Draw image on the canvas.

Specify center position and size of the source (image) and center position and size of the destination (the canvas).

Size of the source allow get a piece of image. If width_height_source is bigger than image then draw nothing.

Size of the destination allow rescale the drawed image.

rotation specify a clockwise rotation in radians.

Each new Pygame surface used is added to image._pygamesurfaces_cached. See Image._pygamesurfaces_cached_clear() .

If number of surfaces in this caches is greater than image._pygamesurfaces_cache_max_size then remove the oldest surface.

Parameters
  • image – Image

  • center_source – (int or float, int or float) or [int or float, int or float]

  • width_height_source – ((int or float) >= 0, (int or float) >= 0) or [(int or float) >= 0, (int or float) >= 0]

  • center_dest – (int or float, int or float) or [int or float, int or float]

  • width_height_dest – ((int or float) >= 0, (int or float) >= 0) or [(int or float) >= 0, (int or float) >= 0]

  • rotation – int or float

draw_line(point1: Sequence[Union[int, float]], point2: Sequence[Union[int, float]], line_width: Union[int, float], line_color: str) None[source]

Draw a line segment from point1 to point2.

Parameters
  • point1 – (int or float, int or float) or [int or float, int or float]

  • point2 – (int or float, int or float) or [int or float, int or float]

  • line_width – (int or float) > 0

  • line_color – str

draw_point(position: Sequence[Union[int, float]], color: str) None[source]

Draw a point.

Parameters
  • position – (int or float, int or float) or [int or float, int or float]

  • color – str

draw_polygon(point_list: Sequence[Sequence[Union[int, float]]], line_width: Union[int, float], line_color: str, fill_color: Optional[str] = None) None[source]

Draw a polygon from a list of points. A segment is automatically drawed between the last point and the first point.

If fill color is not None then fill with this color.

If line_width > 1, ends are poorly made!

Parameters
  • point_list – not empty (tuple or list) of ((int or float, int or float) or [int or float, int or float])

  • line_width – (int or float) > 0

  • line_color – str

  • fill_color – None or str

draw_polyline(point_list: Sequence[Sequence[Union[int, float]]], line_width: Union[int, float], line_color: str) None[source]

Draw line segments between a list of points.

If line_width > 1, ends are poorly made!

Parameters
  • point_list – not empty (tuple or list) of ((int or float, int or float) or [int or float, int or float])

  • line_width – (int or float) > 0

  • line_color – str

draw_text(text: str, point: Sequence[Union[int, float]], font_size: Union[int, float], font_color: str, font_face: str = 'serif', _font_size_coef: Union[int, float] = 0.75) None[source]

Draw the text string at the position point.

(point[0] is the left of the text, point[1] is the bottom of the text.)

If correponding font in Pygame is not founded, then use the default pygame.font.Font.

_font_size_coef is used to adjust the vertical positioning. (This paramater is not available in SimpleGUI of CodeSkulptor.)

Warning

This method can’t draw multiline text.

To draw multiline text, see simplegui_lib_draw.draw_text_multi() .

Parameters
  • text – str

  • point – (int or float, int or float) or [int or float, int or float]

  • font_size – (int or float) >= 0

  • font_color – str

  • font_face – str == ‘monospace’, ‘sans-serif’, ‘serif’

  • _font_size_coef – int or float

Raise

ValueError if text contains unprintable whitespace character

(Alpha color channel don’t work!!!)

SimpleGUICS2Pygame.simpleguics2pygame.canvas.create_invisible_canvas(width: int, height: int) SimpleGUICS2Pygame.simpleguics2pygame.canvas.Canvas[source]

NOT IMPLEMENTED! (Return a “weak” Canvas.)

(Available in SimpleGUI of CodeSkulptor but not in CodeSkulptor documentation!)

Parameters
  • width – int >= 0

  • height – int >= 0

Returns

Canvas

SimpleGUICS2Pygame.simpleguics2pygame.canvas.__all__ = ('Canvas', 'create_invisible_canvas')

Built-in immutable sequence.

If no argument is given, the constructor returns an empty tuple. If iterable is specified the tuple is initialized from iterable’s items.

If the argument is a tuple, the return value is the same object.

[source]