在本教程中,我們將看到如何在Turtle PythonMove the turtle in the program.Turtles represent invisible pens or markers,It is used to draw all the lines on the canvas、形狀和顏色.To move the turtle,You first need to make sure you've started a blank canvas.
To keep the turtle inPython中移動,我們可以使用**forward()**函數.在下面的代碼片段中,我們添加了對forward()函數的調用,同時傳入一個75的整數值.This tells the turtle to start moving from the middle of the canvas75步.One step is equivalent to one pixel.默認情況下,The turtle is actually an arrow shape,它向右移動,除非你[Change the direction of the turtle].如果願意的話,可以用fd()Functions serve as a shorthand tool.
from turtle import *
drawing_area = Screen()
drawing_area.setup(width=750, height=500)
forward(75)
done()
復制代碼
We can change the pen from an arrow to something else.This is a turtle program after all,So let's turn the pen into a real turtle.要做到這一點,我們可以使用shape()函數.shape()Functions can turn the default arrow into something else,如方形、圓形,even turtles!Let's try a few different variations.Let's try some different variations here.
from turtle import *
drawing_area = Screen()
drawing_area.setup(width=750, height=500)
shape('turtle')
forward(75)
done()
復制代碼
from turtle import *
drawing_area = Screen()
drawing_area.setup(width=750, height=500)
shape('square')
forward(75)
done()
復制代碼
from turtle import *
drawing_area = Screen()
drawing_area.setup(width=750, height=500)
shape('circle')
forward(75)
done()
復制代碼
backward()The function works with forward()函數一樣,但是是反向的.Imagine yourself walking forward,Or go backwards.這對你的Python TurtleIt's the same thing.backward()There are two shorthand versions,即bk()和back().
from turtle import *
drawing_area = Screen()
drawing_area.setup(width=750, height=500)
shape('circle')
backward(75)
done()
復制代碼
Python Turtle screen is x 和 y The axis is divided into four quadrants,Turtle always from 0,0 開始,That is, the exact center of the canvas.
We can use this code to draw four quadrants on the turtle canvas.當調用home()函數時,The turtle was moved back0,0,也就是畫布的中心.
from turtle import *
drawing_area = Screen()
drawing_area.setup(width=500, height=500)
forward(250)
back(500)
home()
left(90)
forward(250)
back(500)
home()
done()
復制代碼
Once you understand what's on the canvasx和yHow the coordinates exist,你就可以使用goto()function to move to a specific location on the screen.We just divided the screen into four quadrants,Now the following program will move the turtle one by one to the exact center of each quadrant,thus forming a square.Each corner of the square is exactly at th1、2、3、4the center of the quadrant.setpos()和setposition()The effect of the function and goto()相同.
from turtle import *
drawing_area = Screen()
drawing_area.setup(width=500, height=500)
forward(250)
back(500)
home()
left(90)
forward(250)
back(500)
home()
goto(125, 125)
goto(-125, 125)
goto(-125, -125)
goto(125, -125)
goto(125, 125)
done()
復制代碼
Move the turtle toXThe position provided on the axis.
from turtle import *
drawing_area = Screen()
drawing_area.setup(width=500, height=500)
setx(100)
done()
復制代碼
Move the turtle toYthe specified position on the axis.
from turtle import *
drawing_area = Screen()
drawing_area.setup(width=500, height=500)
sety(100)
done()
復制代碼
To make the turtle draw faster or slower,你可以使用speed()函數.默認值是3,可能的值是1到10,10是最快的.The speed is passed as an integer value.You can also use the slowest、較慢、正常、Fast and fastest strings to control the speed.
[shape()函數]Of course part of the turtle module itself.The parameter we pass to it is a description of the shape of the turtle[python字符串].優秀的工作!We now know how to make the turtle move,Also know how to change the appearance of the turtle.我們可以使用forward()function to move the turtle forward,或者使用backward()function to move the turtle backwards.These commands also have shortcut keysfd()代表前進(),或bk()代表後退().