程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

How to move a turtle in a Turtle Python program

編輯:Python

在本教程中,我們將看到如何在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()
復制代碼


Change the turtle

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()
復制代碼


Turtle backwards()函數

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()
復制代碼


烏龜goto()函數

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()
復制代碼


烏龜setx()函數

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()
復制代碼


Turtle sety() 函數

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()
復制代碼


Turtle speed() 函數

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()代表後退().


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved