simpleguics2pygame — canvas

simpleguics2pygame module: simpleguics2pygame/canvas.

Class Canvas.

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

license:GPLv3 — Copyright (C) 2015-2016, 2020 Olivier Pirson
author:Olivier Pirson — http://www.opimedia.be/
version:November 29, 2020
class SimpleGUICS2Pygame.simpleguics2pygame.canvas.Canvas(frame, canvas_width, canvas_height)[source]

Canvas similar to SimpleGUI Canvas of CodeSkulptor.

__init__(frame, canvas_width, canvas_height)[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__()[source]

Return ‘<Canvas object>’.

Returns:str
__weakref__

list of weak references to the object (if defined)

_background_pygame_color

Default pygame.Color of the background of the canvas.

_bg_pygame_surface_image = None

pygame.surface.Surface default background image replaces _background_pygame_color.

_draw()[source]

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

(Not available in SimpleGUI of CodeSkulptor.)

_save(filename)[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, radius, start_angle, end_angle, line_width, line_color)[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 CodeSkulptor 2!)

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, radius, line_width, line_color, fill_color=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, center_source, width_height_source, center_dest, width_height_dest, rotation=0)[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, point2, line_width, line_color)[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, color)[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, line_width, line_color, fill_color=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, line_width, line_color)[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, point, font_size, font_color, font_face='serif', _font_size_coef=0.75)[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, height)[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]