easygraphics.turtle package

The turtle graphics package.

The turtle graphics is a classic and popular way to introducing programming to kids.

In the turtle graphics, you control a turtle to move around the graphics window. The traces left by its move form drawings.

Note that in the turtle graphics, the origin (0,0) is in the center of the graphics window, and Y-axis is bottom-up.

from easygraphics.turtle import *

def polyspi(side, angle, inc):
    while is_run():
        fd(side)
        rt(angle)
        side += inc

create_world(800, 600)
set_speed(100)
polyspi(0, 117, 5)
close_world()
../_images/infinite.png

Functions

easygraphics.turtle.create_world(width: int = 800, height: int = 600) → None

Create an world for turtle drawing.

Parameters:
  • width – width of the graphics window
  • height – height of the graphics window
easygraphics.turtle.close_world() → None

Close the turtle world and the graphics window.

easygraphics.turtle.forward(distance: float)

Move the turtle forward by the specified distance, in the direction the turtle is heading.

Parameters:distance – the distance to move
easygraphics.turtle.fd(distance: float)

Move the turtle forward by the specified distance, in the direction the turtle is heading.

Parameters:distance – the distance to move
easygraphics.turtle.backward(distance: float)

Move the turtle backward by the specified distance, in the direction the turtle is heading.

Parameters:distance – the distance to move
easygraphics.turtle.back(distance: float)

Move the turtle backward by the specified distance, in the direction the turtle is heading.

Parameters:distance – the distance to move
easygraphics.turtle.bk(distance: float)

Move the turtle backward by the specified distance, in the direction the turtle is heading.

Parameters:distance – the distance to move
easygraphics.turtle.left_turn(degree: float)

Turn turtle left (counter-clockwise) by “degree” degree.

Parameters:degree – the degree to turn
easygraphics.turtle.lt(degree: float)

Turn turtle left (counter-clockwise) by “degree” degree.

Parameters:degree – the degree to turn
easygraphics.turtle.right_turn(degree: float)

Turn turtle right (clockwise) by “degree” degree.

Parameters:degree – the degree to turn
easygraphics.turtle.rt(degree: float)

Turn turtle right (clockwise) by “degree” degree.

Parameters:degree – the degree to turn
easygraphics.turtle.left(degree: float)

Turn turtle left (counter-clockwise) by “degree” degree.

Parameters:degree – the degree to turn
easygraphics.turtle.right(degree: float)

Turn turtle right (clockwise) by “degree” degree.

Parameters:degree – the degree to turn
easygraphics.turtle.clear_screen()

Delete all drawings from the screen and reset turtles to its original state.

easygraphics.turtle.cs()

Delete all drawings from the screen and reset turtles to its original state.

easygraphics.turtle.gotoxy(x: float, y: float)

Move the turtle to point (x,y). Will leave traces if the pen is down.

Parameters:
  • x – x coordinate value of the destination point.
  • y – x coordinate value of the destination point.
easygraphics.turtle.home()

Move the turtle back to the origin(0,0), and heading up. Will leave traces if the pen is down.

easygraphics.turtle.turn_to(angle)

Turn the angle to orient to the specified angle.

Parameters:angle – the new heading angle (in degrees).
easygraphics.turtle.facing(x: float, y: float)

Turn the turtle to face the point(x,y).

Parameters:
  • x – x coordinate value of the target point
  • y – y coordinate value of the target point
easygraphics.turtle.begin_fill()

Begin to record the turtle’s shape drawing path for filling.

easygraphics.turtle.end_fill()

Fill the shape enclosed by the turtle’s drawing path after the last call to begin_fill.

easygraphics.turtle.setxy(x: float, y: float)

Set the turtle’s current position to point (x,y). Will not leave traces.

Parameters:
  • x – x coordinate value of the destination point.
  • y – y coordinate value of the destination point.
easygraphics.turtle.set_heading(angle)

Set the angle current heading to the specified angle.

Parameters:angle – the new heading angle (in degrees).
easygraphics.turtle.move_arc(radius: float, angle: float = 360)

Move the turtle in a arc path.

The center is radius units left of the turtle. That is, if radius > 0, the center is on the left of the turtle; if radius < 0, the center is on the right of the turtle.

If angle > 0, the turtle moves forward around the center; if angle < 0, the turtle moves backward around the center. So:

  • if angle > 0 and radius > 0, the turtle moves forward and turns counter-clockwise;
  • if angle > 0 and raidus < 0, the turtle move forward and turns clockwise;
  • if angle <0 and radius > 0, the turtle moves backward and turns clockwise;
  • if angle <0 and radius < 0, the turtle moves backward and turns counter-clockwise.
Parameters:
  • radius – radius of the arc
  • angle – how many degrees the turtle will move
easygraphics.turtle.move_ellipse(radius_left: float, radius_top: float, angle: float = 360)

Move the turtle in an elliptical path.

“radius_left” is the radius of the ellipse on the direction perpendicular to the turtle’s orientation, it can be postive or negtive;”radius_top” is the radius of the ellipse on the direction parallel to the turtle’s orientation, it must be postive.

The center is radius_left units left of the turtle. That is, if radius_left > 0, the center is on the left of the turtle; if radius_left < 0, the center is on the right of the turtle.

If angle > 0, the turtle moves forward around the center; if angle < 0, the turtle moves backward around the center. So:

  • if angle > 0 and radius_left > 0, the turtle moves forward and turns counter-clockwise;
  • if angle > 0 and radius_left < 0, the turtle move forward and turns clockwise;
  • if angle <0 and radius_left > 0, the turtle moves backward and turns clockwise;
  • if angle <0 and radius_left < 0, the turtle moves backward and turns counter-clockwise.
Parameters:
  • radius_left – the radius of the ellipse on the direction perpendicular to the turtle’s orientation
  • radius_top – the radius of the ellipse on the direction parallel to the turtle’s orientation
  • angle – how many degrees the turtle will move
easygraphics.turtle.get_y() → float

Get the turtle’s current y position.

Returns:the turtle’s y position.
easygraphics.turtle.get_x() → float

Get the turtle’s current x position.

Returns:the turtle’s x position.
easygraphics.turtle.get_heading() → float

Get the turtle’s heading angle.

Returns:the turtle’s heading angle.
easygraphics.turtle.get_turtle() → easygraphics.turtle.turleclass.Turtle

Get the current turtle.

Returns:the current turtle
easygraphics.turtle.get_turtle_world() → easygraphics.turtle.turleclass.TurtleWorld

Get the current turtle world.

Returns:the current turtle world
easygraphics.turtle.set_pen_size(width: float, image: easygraphics.image.Image = None)

Set line width (thickness) of the specified image.

It will be used when drawing lines or shape outlines

Parameters:
  • width – line width (line thickness)
  • image – the target image whose line width is to be set. None means it is the target image (see set_target() and get_target())
easygraphics.turtle.set_immediate(immediate: bool)

Set if there are animations when the turtle moving.

Parameters:immediate – True to turn off animation (the move finishes immediately). False to turn on.
easygraphics.turtle.set_speed(speed)

Set moving speed of the turtle.

Parameters:speed – the new speed
easygraphics.turtle.pen_down()

Pull the pen down – drawing when the turtle moving.

easygraphics.turtle.pen_up()

Pull the pen up – no drawing when the turtle moving.

easygraphics.turtle.pu()

Pull the pen up – no drawing when the turtle moving.

easygraphics.turtle.pd()

Pull the pen down – drawing when the turtle moving.

easygraphics.turtle.hide()

Hide the turtle.

easygraphics.turtle.show()

Show the turtle.

easygraphics.turtle.pause()

Pause the program and wait for mouse clicking or keyboard hitting.

>>> from easygraphics import *
>>> init_graph(800,600)
>>> pause()
>>> close_graph()
easygraphics.turtle.is_run() → bool

Test if the graphics system is running.

Returns:True if the graphics system is running.
easygraphics.turtle.is_out_of_window() → bool

Test if the turtle is out of the graphics window.

Returns:True if the turtle is out of the window, False otherwise.
class easygraphics.turtle.Turtle(world: easygraphics.turtle.turleclass.TurtleWorld)

The Turtle class.

BASE_STEP = 1
DEFAULT_ORENTATION = 90
back(distance: float)

Move the turtle backward by the specified distance, in the direction the turtle is heading.

Parameters:distance – the distance to move
backward(distance: float)

Move the turtle backward by the specified distance, in the direction the turtle is heading.

Parameters:distance – the distance to move
begin_fill()

Begin to record the turtle’s shape drawing path for filling.

bk(distance: float)

Move the turtle backward by the specified distance, in the direction the turtle is heading.

Parameters:distance – the distance to move
cancle_fill()

Cancle the turtle’s drawing path recording.

close()

Close and cleanup the turtle.

static create_turtle_icon() → easygraphics.image.Image

Create the default turtle icon.

Returns:the turtle icon image
end_fill()

Fill the shape enclosed by the turtle’s drawing path after the last call to begin_fill.

facing(x, y)

Turn the turtle to face the point(x,y).

Parameters:
  • x – x coordinate value of the point facing to
  • y – y coordinate value of the point facing to
fd(distance: float)

Move the turtle forward by the specified distance, in the direction the turtle is heading.

Parameters:distance – the distance to move
forward(distance: float)

Move the turtle forward by the specified distance, in the direction the turtle is heading.

Parameters:distance – the distance to move
get_heading() → float

Get the turtle’s heading angle.

Returns:the turtle’s heading angle.
get_icon() → easygraphics.image.Image

Get the icon image of the turtle.

Returns:the icon image
get_x() → float

Get the turtle’s current x position.

Returns:the turtle’s x position.
get_y() → float

Get the turtle’s current y position.

Returns:the turtle’s y position.
gotoxy(x, y)

Move the turtle to point (x,y). Will leave traces if the pen is down.

Parameters:
  • x – x coordinate value of the destination point.
  • y – x coordinate value of the destination point.
hide()

Hide the turtle.

home()

Move the turtle back to the origin(0,0), and heading up. Will leave traces if the pen is down.

is_filling() → bool

Test if it is recording the turtle’s drawing path (for fill).

Returns:
is_out_of_window() → bool

Test if the turtle is out of the graphics window.

Returns:True if the turtle is out of the window, False otherwise.
is_show()

Check if the turtle is shown.

Returns:True the turtle is shown, False the turtle is hiding.
left(degree: float)

Turn turtle left (counter-clockwise) by “degree” degree.

Parameters:degree – the degree to turn
left_turn(degree: float)

Turn turtle left (counter-clockwise) by “degree” degree.

Parameters:degree – the degree to turn
lt(degree: float)

Turn turtle left (counter-clockwise) by “degree” degree.

Parameters:degree – the degree to turn
move_arc(radius: float, angle: float = 360)

Move the turtle in a arc path.

The center is radius units left of the turtle. That is, if radius > 0, the center is on the left of the turtle; if radius < 0, the center is on the right of the turtle.

If angle > 0, the turtle moves forward around the center; if angle < 0, the turtle moves backward around the center. So:

  • if angle > 0 and radius > 0, the turtle moves forward and turns counter-clockwise;
  • if angle > 0 and raidus < 0, the turtle move forward and turns clockwise;
  • if angle <0 and radius > 0, the turtle moves backward and turns clockwise;
  • if angle <0 and radius < 0, the turtle moves backward and turns counter-clockwise.
Parameters:
  • radius – radius of the arc
  • angle – how many degrees the turtle will move
move_ellipse(radius_left: float, radius_top: float, angle: float = 360)

Move the turtle in an elliptical path.

“radius_left” is the radius of the ellipse on the direction perpendicular to the turtle’s orientation, it can be postive or negtive;”radius_top” is the radius of the ellipse on the direction parallel to the turtle’s orientation, it must be postive.

The center is radius_left units left of the turtle. That is, if radius_left > 0, the center is on the left of the turtle; if radius_left < 0, the center is on the right of the turtle.

If angle > 0, the turtle moves forward around the center; if angle < 0, the turtle moves backward around the center. So:

  • if angle > 0 and radius_left > 0, the turtle moves forward and turns counter-clockwise;
  • if angle > 0 and radius_left < 0, the turtle move forward and turns clockwise;
  • if angle <0 and radius_left > 0, the turtle moves backward and turns clockwise;
  • if angle <0 and radius_left < 0, the turtle moves backward and turns counter-clockwise.
Parameters:
  • radius_left – the radius of the ellipse on the direction perpendicular to the turtle’s orientation
  • radius_top – the radius of the ellipse on the direction parallel to the turtle’s orientation
  • angle – how many degrees the turtle will move
pd()

Pull the pen down – drawing when the turtle moving.

pen_down()

Pull the pen down – drawing when the turtle moving.

pen_up()

Pull the pen up – no drawing when the turtle moving.

pu()

Pull the pen up – no drawing when the turtle moving.

reset()

Reset the turtle’s state

right(degree: float)

Turn turtle right (clockwise) by “degree” degree.

Parameters:degree – the degree to turn
right_turn(degree: float)

Turn turtle right (clockwise) by “degree” degree.

Parameters:degree – the degree to turn
rt(degree: float)

Turn turtle right (clockwise) by “degree” degree.

Parameters:degree – the degree to turn
set_heading(angle)

Set the angle current heading to the specified angle.

Parameters:angle – the new heading angle (in degrees).
set_speed(speed: int)

Set moving speed of the turtle.

Parameters:speed – the new speed
setxy(x, y)

Set the turtle’s current position to point (x,y). Will not leave traces.

Parameters:
  • x – x coordinate value of the destination point.
  • y – y coordinate value of the destination point.
show()

Show the turtle.

turn_to(angle)

Turn the angle to orient to the specified angle.

Parameters:angle – the new heading angle (in degrees).
class easygraphics.turtle.TurtleWorld(canvas: Optional[easygraphics.image.Image] = None)

Turtles move and draw in a world. This is the class representing the world.

You must remember to close it by calling the close() method, if you have finished the drawing and won’t use it anymore.

Note that in the default world, we are using a normal coordinate system that (0,0) is in the center of the graphics window (image), the X-axis grows from left to right, and the Y-axis grows from bottom to top. A positive degree means turn counter-clockwise, and a negtive degree means turn clockwise.
add_turtle(turtle: easygraphics.turtle.turleclass.Turtle)

Put the turtle into the world.

Parameters:turtle – the turtle
clear()

Delete all drawings from the screen and reset turtles to its original state.

clear_screen()

Delete all drawings from the screen and reset turtles to its original state.

close()

Close the turtles world.

create_snap_shot() → easygraphics.image.Image

Create a snap shot of the current drawing.

Returns:the snap shot image.
create_turtle()

Create a new turtle in the world and put it to the origin(0,0).

If x and y are None, the turtle is put on the center.

Returns:the created turtle.
cs()

Delete all drawings from the screen and reset turtles to its original state.

get_height() → float

Get the height of the underlying graphics window (image).

Returns:height of the underlying graphics window (image)
get_width() → float

Get the width of the underlying graphics window (image).

Returns:width of the underlying graphics window (image)
get_world_image()

Return the drawing image of the world.

Returns:the drawing image of the world.
is_immediate() → bool

Check if there are animations when turtles moving.

Returns:True if there aren’t any animations (the move finishes immediately), False otherwise.
is_on_screen() → bool

Test if the underlying image is the graphics window.

Returns:True if the underlying image is the graphics window, False otherwise.
is_running()
set_immediate(immediate: bool)

Set if there are animations when turtles moving.

Parameters:immediate – True to turn off animation (the move finishes immediately). False to turn on.
snap_shot_to_image(image, x=0, y=0)

Create a snap shot to the specified image.

The snap shot will be copied to the image’s (x,y).

Parameters:
  • image – the image to copy the snap shot
  • x – x coordinate value of the target position
  • y – y coordinate value of the target position
easygraphics.turtle.easy_run(main_func: Callable, width=640, height=480)