How to Create Custom Turtle shapes in Python?


Python’s Turtle library is used for generating 2D graphics and animations. It has a very simple interface with help of which we can create shapes and patterns on the screen. It comes with some built-in shapes like squares, circles, triangles etc. However, we can even create our own shapes with the help of Turtle. In this article, we are going to see how to create custom turtle shapes in python.

Before going forward with the creating custom shapes we need to install Turtle on the system and this can be done by simply using the following command −

pip install turtle

We all need to see certain functions of Turtle that will be used to help create custom shapes and they are as follows 

  • register_shape()  It is used to register a new custom shape in Turtle. It takes in 2 arguments which are the name of the shape (as a string) and a tuple of (x, y) coordinate pairs which represents the vertices of the shape.

  • .shape()  Once a shape has been successfully registered, using the register_shape() method, it can be used as a shape by making use of the .shape() functions and has to be given a string argument which is the name of the shape.

  • .color()  This method is only used for setting a colour of figures and it can take in multiple arguments all of which would be related to colours. The arguments could be either the direct name of the colour like red, blue, green etc or hex code of the colour or even the RGB values which would mean 3 arguments in integer format.

In short, register_shape() is used to create and register the shape whereas .shape() is used to implement the registered shape.

1. Square

We will 1st see how to create a simple square manually and will require the above-mentioned functions.

Example

import turtle

# create turtle object
t = turtle.Turtle()

# define shape vertices
square_shape = ((-50, -50), (-50, 50), (50, 50), (50, -50))

# register the shape
turtle.register_shape("square", square_shape)

# use the shape
t.shape("square")
t.color("green")

# This functions is important to keep the turtle window on
turtle.done()

Output

2. Star

A star is also a simple shape with points that extend from the centre. They are often used as decorative elements but can have a scientific context in mathematics. It just requires careful declaration of the coordinates whereas the rest of the code would remain the same as above.

Example

import turtle

t = turtle.Turtle()

star_shape = ((0, 50), (-14, 16), (-50, 16), (-23, -12), (-35, -46),
         (0, -24), (35, -46), (23, -12), (50, 16), (14, 16))

turtle.register_shape("star", star_shape)

t.shape("star")
t.color("green")

turtle.done()

Output

3. Hexagon

A hexagon is a 6-sided polygon with 6 angles. It is found in honeycombs or in chemistry benzene rings. Here too, the coordinates needed to be changed and mentioned carefully.

Example

import turtle

t = turtle.Turtle()
hexagon_shape = ((-50, 0), (-25, 43.3), (25, 43.3), (50, 0), (25, -43.3), (-25, -43.3))

turtle.register_shape("hexagon", hexagon_shape)

t.shape("hexagon")
t.color("red")

turtle.done()

Output

4. Cross

The cross symbol is often used by medical facilities like chemists and hospitals. It is also the logo of the red cross society which provides assistance in times of crisis and emergencies. The cross might sometimes also be referred to as the “plus” sign.

Example

import turtle

t = turtle.Turtle()
Cross_shape = ((-50, 0), (-15, 0), (-15, -50), (15, -50), (15, 0), (50, 0), (50, 50),
         (15, 50), (15, 100), (-15, 100), (-15, 50), (-50, 50))

turtle.register_shape("Cross", Cross_shape)

t.shape("Cross")
t.color("red")

turtle.done()

Output

5. Cubes

Cubes are a 3D shape which consist of 6 square faces which meet at right angles and they are also referred to as a regular hexahedron. Every face of the cube is congruent in shape and size and all lengths of edges are the same. Cubes are widely studied in Mathematics, physics and engineering due to their shape and properties. While our screens represent 2D images, we have attempted to create a 3D visual or give a 3 dimensional look to a 2D figure. In this example, the creation is done with the help of animations of turtle, however the animation has been turned off by using the hideturtle() and tracer(0) function and by modifying these statements the animations can be seen as well. The animation is done in the background and the screen is updated once it is done.

Example

import turtle

t = turtle.Turtle()
t.hideturtle()
turtle.tracer(0)

# Draw the front side of the square
t.begin_fill()
t.color("red")
t.goto(100, 0)
t.goto(100, 100)
t.goto(0, 100)
t.goto(0, 0)
t.end_fill()

# Draw the right side of the square
t.begin_fill()
t.color("orange")
t.goto(100, 0)
t.goto(120, 20)
t.goto(120, 120)
t.goto(100, 100)
t.end_fill()

# Draw the top side of the square
t.begin_fill()
t.color("yellow")
t.goto(0, 100)
t.goto(20, 120)
t.goto(120, 120)
t.goto(100, 100)
t.end_fill()

turtle.update()
turtle.done()

Output

6. Koch Snowflake

Koch snowflakes are a kind of fractal curve. They are created by using equilateral triangles dividing each of its sides into equal segments and then replacing their middle segments with 2 segments that form an equilateral bump or protuberance. This process is continued for all of the 4 lie segments created in the previous steps. Here too, the animation is required and is hidden for the sake of simple outputs and can be enabled by modifying the functions as mentioned in the previous example.

Example

import turtle

t = turtle.Turtle()
# Hide turtle object
t.hideturtle()
turtle.tracer(0)

# Draw the Koch snowflake
def koch_snowflake(length, depth):
    if depth == 0:
        t.forward(length)
    else:
        for angle in [60, -120, 60, 0]:
            koch_snowflake(length / 3, depth - 1)
            t.left(angle)

# Set turtle position and angle
t.penup()
t.goto(-150, 150)
t.pendown()
t.setheading(0)

# Draw the Koch snowflake
t.color("blue")
t.begin_fill()
for i in range(3):
    koch_snowflake(300, 4)
    t.right(120)
t.end_fill()

turtle.done()

Output

Conclusion

The turtle library is really great for creating visual graphics with an added level of personalization to your projects. By using the shape_register() function we can create a shape of any type and use the .shape() function to implement/draw the shape. Any kind of shape irrespective of whether it is known to humans or not, can be created and all that is required is the coordinates. Creating custom shapes is an easy task and only requires a bit of creativity and programming knowledge. We have implemented multiple types of shapes ranging from a simple square to 3D cubes and Koch snowflakes.

Updated on: 18-Aug-2023

993 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements