turtle.stamp() function in Python

The turtle.stamp() function in Python places an invisible “stamp” of the turtle’s current shape onto the canvas at its current position. Returns a stamp_id for later deletion.

Syntax: turtle.stamp()
Parameters: None.

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

Demo12.py

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

import turtle

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

t.stamp()           # Leave an impression of the turtle shape
t.forward(100)
t.left(90)
t.stamp()           # Leave another impression
t.forward(100)

window.exitonclick()

The following is the output:

turtle.stamp() function in Python

In the above code, we followed the below steps:

  1. Import the module: import turtle loads Python’s turtle graphics library.
  2. Create the screen: window = turtle.Screen() opens the drawing window.
  3. Create the turtle: t = turtle.Turtle() creates the turtle (the pen) you’ll control.
  4. Stamp the turtle: t.stamp() leaves a copy of the turtle’s current shape at its current position and orientation; the turtle doesn’t move. The stamp uses the turtle’s current color settings.
  5. Move forward: t.forward(100) draws a 100-unit line in the current heading.
  6. Turn left: t.left(90) rotates the turtle 90° counterclockwise without moving.
  7. Stamp again: t.stamp() leaves another imprint at the new location and orientation.
  8. Move forward again: t.forward(100) draws another 100-unit line.
  9. Close on click: window.exitonclick() keeps the window open until you click inside it, then closes.

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.undo() function in Python
turtle.clearstamp() function in Python
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment