In this Python tutorial, we are going to learn the use of Python Turtle Setworldcoordinates. And to illustrate this tutorial, we will build a small project.
We are building a project in which we will learn How to set up the setworldcoordinate with the help of the Python turtle library. In this, we split the code and explain how we can set up the coordinates by using functions and methods.
Python Turtle Setworldcoordinates
The setworldcoordinates() function is used as a reset. It reset the user-defined coordinates. If the mode of setworldcoordinate is active then all the drawings can be redrawn again according to the new coordinate given on the screen.
Syntax
turtle.setworldcoordinates(llx,lly,urx,ury)
- lly: a number, y-coordinate of the lower-left corner of the canvas.
- llx: a number,x-coordinate of the lower-left corner of the canvas.
- ury: a number,y-coordinate of the upper right corner of the canvas.
- urx: a number,x-coordinates of the upper left corner of the canvas.
Now we are moving forward to start this Python turtle project. Here we are just importing the libraries.
from turtle import *
import turtle as tur
Hereafter importing the library now we are creating an object for this turtle.
tur = tur.Turtle()
Now we are using the screen() function to make a screen object and also using ws.mode(‘world’) to set the mode of the word.
ws = tur.Screen()
ws.mode('world')
Now we are using the setworldcoordinate() function to reset the user-defined coordinates. If the mode of setwordcoordinate is active then all drawings can be redrawn according to the new coordinates.
tur.setworldcoordinates(-50, -50, 50, 50)
Now we are giving some motion to the turtle from which we set the coordinates.
- tur.forward(1+1*i) is used to move the turtle in the forward direction.
- tur.right(90) is used to move the turtle in the right direction.
for i in range(16):
tur.forward(1+1*i)
tur.right(90)
Now we again use the setworldcoordinates() function to reset the user define coordinate. In this, if the mode of setworldcoordinates is already active then all drawings can be redrawn according to the new coordinates.
tur.setworldcoordinates(-40, -40, 40, 40)
After set the coordinates we give some motion to the turtle to join the coordinate to draw a shape.
- tur.forward(1+1*(i+16)) is used to move the turtle in the forward direction to move to the new coordinate.
- tur.right(90) is used to move the turtle in the right direction.
for i in range(16):
tur.forward(1+1*(i+16))
tur.right(90)
Now we again use the setworldcoordinates() function to reset the user define coordinate. In this, if the mode of setworldcoordinates is already active then all drawings can be redrawn according to the new coordinates. And after redrawing the new coordinate the turtle can move on the new coordinates.
tur.setworldcoordinates(-30, -30, 30, 30)
After set the new coordinates we give some motion to the turtle to join the coordinate to draw a shape.
- tur.forward(1+1*(i+32)) is used to move the turtle in the forward direction.
- tur.right(90) is used to move the turtle in the right direction.
for i in range(16):
tur.forward(1+1*(i+32))
tur.right(90)
tur.done()
Hereafter splitting the code and explain how we can set the coordinates using python turtle. Now we will see how the output looks like after running the whole code and also see how all the drawings were done by turtle redrawn according to the new coordinates if the mode of setworldcoordinates is already active.
from turtle import *
import turtle as tur
tur = tur.Turtle()
# make screen object and
# set mode to world
ws = tur.Screen()
ws.mode('world')
# set world coordinates
tur.setworldcoordinates(-50, -50, 50, 50)
# do some motion
for i in range(16):
tur.forward(1+1*i)
tur.right(90)
# set world coordinates
tur.setworldcoordinates(-40, -40, 40, 40)
# do some motion
for i in range(16):
tur.forward(1+1*(i+16))
tur.right(90)
# set world coordinates
tur.setworldcoordinates(-30, -30, 30, 30)
# do some motion
for i in range(16):
tur.forward(1+1*(i+32))
tur.right(90)
tur.done()
After running the above code we get the following output in which we can see that the mode of setworldcoordinate is active and all the drawings can be redrawn according to the new coordinate.
So, in this project, we have illustrated How to draw setworldcoordinates in the python turtle. Moreover, we have also discussed the whole code used in this tutorial including the use of Python Turtle setworldcoordinates.
Also, take a look at some more tutorials on Python Turtle.
- Python Turtle Polygon
- Python Turtle 3d Shapes
- Python Turtle Window
- Python Turtle Random
- Python Turtle Background
- Python Turtle Mouse
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.