📜  Python中的 turtle.setworldcoordinates()函数

📅  最后修改于: 2022-05-13 01:54:52.833000             🧑  作者: Mango

Python中的 turtle.setworldcoordinates()函数

turtle 模块以面向对象和面向过程的方式提供海龟图形原语。因为它使用 tkinter 作为底层图形,所以它需要安装一个支持 Tk 的Python版本。

turtle.setworldcoordinates()

该函数用于建立用户自定义坐标系。这将执行重置。如果模式“世界”已经激活,所有图纸都将根据新坐标重新绘制。

注意:在用户定义的坐标系中,角度可能会出现扭曲。

以下是上述方法的实现以及一些示例:

示例 1:

Python3
# importing package
import turtle
 
# make screen object and
# set screen mode to world
sc = turtle.Screen()
sc.mode('world')
 
# set world coordinates
turtle.setworldcoordinates(-20, -20, 20, 20)
 
# loop for some motion
for i in range(20):
    turtle.forward(1+1*i)
    turtle.right(90)


Python3
# importing package
import turtle
 
# make screen object and
# set screen mode to world
sc = turtle.Screen()
sc.mode('world')
 
# set world coordinates
turtle.setworldcoordinates(-40, -40, 40, 40)
 
# loop for some motion
for i in range(20):
    turtle.forward(1+1*i)
    turtle.right(90)


Python3
# importing package
import turtle
 
# make screen object and
# set mode to world
sc = turtle.Screen()
sc.mode('world')
 
# set world coordinates
turtle.setworldcoordinates(-50, -50, 50, 50)
 
# do some motion
for i in range(16):
    turtle.forward(1+1*i)
    turtle.right(90)
 
# set world coordinates
turtle.setworldcoordinates(-40, -40, 40, 40)
 
# do some motion
for i in range(16):
    turtle.forward(1+1*(i+16))
    turtle.right(90)
 
# set world coordinates
turtle.setworldcoordinates(-30, -30, 30, 30)
 
# do some motion
for i in range(16):
    turtle.forward(1+1*(i+32))
    turtle.right(90)


输出:

A部分

示例 2:

Python3

# importing package
import turtle
 
# make screen object and
# set screen mode to world
sc = turtle.Screen()
sc.mode('world')
 
# set world coordinates
turtle.setworldcoordinates(-40, -40, 40, 40)
 
# loop for some motion
for i in range(20):
    turtle.forward(1+1*i)
    turtle.right(90)

输出 :

在上面的两个例子中,代码是相同的,只是世界坐标不同,输出不同,如下所示:

示例 3:

Python3

# importing package
import turtle
 
# make screen object and
# set mode to world
sc = turtle.Screen()
sc.mode('world')
 
# set world coordinates
turtle.setworldcoordinates(-50, -50, 50, 50)
 
# do some motion
for i in range(16):
    turtle.forward(1+1*i)
    turtle.right(90)
 
# set world coordinates
turtle.setworldcoordinates(-40, -40, 40, 40)
 
# do some motion
for i in range(16):
    turtle.forward(1+1*(i+16))
    turtle.right(90)
 
# set world coordinates
turtle.setworldcoordinates(-30, -30, 30, 30)
 
# do some motion
for i in range(16):
    turtle.forward(1+1*(i+32))
    turtle.right(90)

输出 :

在这里,我们可以看到之前的所有绘图都设置为新的世界坐标(绘图放大)。