turtle.seth() function in Python

The turtle.seth() function in Python sets the turtle’s heading (direction it is facing) to the specified angle. 0 is east, 90 is north, 180 is west, 270 is south.

Syntax: turtle.seth(to_angle)
Parameters: to_angle: a number (integer or float) representing the new heading in degrees.

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

Demo7.py

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

import turtle

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

t.seth(45)      # Set heading to 45 degrees (northeast)
t.forward(100)
t.seth(180)     # Set heading to 180 degrees (west)
t.forward(100)

window.exitonclick()

The following is the output:

turtle.seth() in Python Turtle

In the above code, we followed the below steps:

  1. Import the module: import turtle loads Python’s turtle graphics library for drawing.
  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. Set absolute heading to 45°: t.seth(45) points the turtle northeast. This rotates without moving; headings are absolute (0° east, 90° north, 180° west, 270° south).
  5. Move forward 100: t.forward(100) draws a 100-unit line along the 45° direction.
  6. Set absolute heading to 180°: t.seth(180) reorients the turtle to face west, again without moving its position.
  7. Move forward 100: t.forward(100) draws a 100-unit line to the left from the current point.
  8. 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.sety() function in Python
turtle.home() function in Python
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment