Python中的turtle.setx()函数 | 您所在的位置:网站首页 › python中turtle库函数怎么设置坐标 › Python中的turtle.setx()函数 |
Python中的turtle.setx()函数
turtle 模块以面向对象和面向过程的方式提供了Turtle图形原语。因为它使用 tkinter 作为底层图形,它需要安装一个支持 Tk 的 Python 版本。 turtle.setx()这个方法是用来设置Turtle的第一个坐标为x,保持第二个坐标不变。在这里,无论Turtle的位置是什么,它都会将x坐标设置为给定的输入值,保持y坐标不变。 语法: turtle.setx(x) 参数: x:一个数字(整数或浮点数),它是唯一需要的一个参数。 下面是上述方法的实现和一些例子。 例子1 : # import package import turtle # check the turtle position print(turtle.position()) # set the x coordinate turtle.setx(30) # check the turtle position print(turtle.position()) # set the x coordinate turtle.setx(-50) # check the turtle position print(turtle.position())输出 : (0.0, 0.0) (30.0, 0.0) (-50.0, 0.0)例子2 : # import package import turtle # set turtle direction turtle.left(90) # loop for pattern for i in range(4): # motion turtle.forward(100) turtle.right(90) turtle.forward(20) turtle.right(90) turtle.forward(100) # set the x coordinate turtle.up() turtle.setx(40*(i+1)) turtle.down() # change the direction turtle.left(180)输出 : |
CopyRight 2018-2019 实验室设备网 版权所有 |