turtle.shape() function in Python

The turtle.shape() function sets the shape of the turtle. Built-in shapes include: “arrow”, “turtle”, “circle”, “square”, “triangle”, and “classic”.

Syntax: turtle.shape(name=None)
Parameters: name – a string which is a valid shapename

Let us see an example to implement the turtle.shape() function in Python:

Demo41.py

# turtle.shape() function in Python
# Code by Studyopedia

import turtle

window = turtle.Screen()
t = turtle.Turtle()

# Available shapes: "arrow", "turtle", "circle", "square", "triangle", "classic"
t.shape("turtle")   # Set shape to turtle
t.forward(100)
t.shape("circle")   # Set shape to circle
t.forward(100)

window.exitonclick()

The following is the output:

turtle.shape() function in Python

In the above code, we followed the below steps:

  • Import module: import turtle loads the turtle graphics library.
  • Create screen: window = turtle.Screen() opens the drawing window.
  • Create turtle: t = turtle.Turtle() creates the turtle you’ll control.
  • Set shape to “turtle”: t.shape(“turtle”) changes the turtle’s appearance to a turtle icon.
  • Move forward: t.forward(100) draws a 100-unit line with the turtle shape.
  • Set shape to “circle”: t.shape(“circle”) changes the turtle’s appearance to a circle.
  • Move forward again: t.forward(100) draws another 100-unit line with the new shape.
  • Close on click: window.exitonclick() waits for a mouse click, then closes the window.

If you liked the tutorial, spread the word and share the link and our website, Studyopedia, with others.


For Videos, Join Our YouTube Channel: Join Now


Read More:

turtle.isvisible() function in Python
turtle.turtlesize() function in Python
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment