Bạn có thể lặp Python rùa không?

Đồ họa con rùa là một cách phổ biến để giới thiệu lập trình cho trẻ em. Nó là một phần của ngôn ngữ lập trình Logo ban đầu được phát triển bởi Wally Feurzeig, Seymour Papert và Cynthia Solomon vào năm 1967

Imagine a robotic turtle starting at [0, 0] in the x-y plane. After an

>>> turtle.setheading[90]
>>> turtle.heading[]
90.0
9, give it the command
>>> turtle.heading[]
90.0
>>> turtle.position[]
[0.00,-10.00]
>>> turtle.home[]
>>> turtle.position[]
[0.00,0.00]
>>> turtle.heading[]
0.0
0, and it moves [on-screen. ] 15 pixel theo hướng mà nó hướng tới, vẽ một đường khi nó di chuyển. Give it the command
>>> turtle.heading[]
90.0
>>> turtle.position[]
[0.00,-10.00]
>>> turtle.home[]
>>> turtle.position[]
[0.00,0.00]
>>> turtle.heading[]
0.0
1, and it rotates in-place 25 degrees clockwise

Turtle star

Turtle can draw intricate shapes using programs that repeat simple moves

from turtle import *
color['red', 'yellow']
begin_fill[]
while True:
    forward[200]
    left[170]
    if abs[pos[]] >> turtle.heading[]
90.0
>>> turtle.position[]
[0.00,-10.00]
>>> turtle.home[]
>>> turtle.position[]
[0.00,0.00]
>>> turtle.heading[]
0.0
3 switch

The turtle module provides turtle graphics primitives, in both object-oriented and procedure-oriented ways. Because it uses for the underlying graphics, it needs a version of Python installed with Tk support

The object-oriented interface uses essentially two+two classes

  1. The class defines graphics windows as a playground for the drawing turtles. Its constructor needs a

    >>> turtle.heading[]
    90.0
    >>> turtle.position[]
    [0.00,-10.00]
    >>> turtle.home[]
    >>> turtle.position[]
    [0.00,0.00]
    >>> turtle.heading[]
    0.0
    
    6 or a as argument. It should be used when is used as part of some application

    The function returns a singleton object of a subclass. This function should be used when is used as a standalone tool for doing graphics. As a singleton object, inheriting from its class is not possible

    All methods of TurtleScreen/Screen also exist as functions, i. e. as part of the procedure-oriented interface

  2. [alias. ] defines Turtle objects which draw on a . Its constructor needs a Canvas, ScrolledCanvas or TurtleScreen as argument, so the RawTurtle objects know where to draw

    Derived from RawTurtle is the subclass [alias.

    >>> turtle.position[]
    [0.00,0.00]
    >>> turtle.forward[25]
    >>> turtle.position[]
    [25.00,0.00]
    >>> turtle.forward[-75]
    >>> turtle.position[]
    [-50.00,0.00]
    
    06], which draws on “the” instance which is automatically created, if not already present

    All methods of RawTurtle/Turtle also exist as functions, i. e. part of the procedure-oriented interface

The procedural interface provides functions which are derived from the methods of the classes and . They have the same names as the corresponding methods. A screen object is automatically created whenever a function derived from a Screen method is called. An [unnamed] turtle object is automatically created whenever any of the functions derived from a Turtle method is called

To use multiple turtles on a screen one has to use the object-oriented interface

Note

In the following documentation the argument list for functions is given. Methods, of course, have the additional first argument self which is omitted here

Overview of available Turtle and Screen methods

Turtle methods

Turtle motionMove and draw

.

. .

.

.

. .

.

Tell Turtle’s state

.

Setting and measurement

Pen controlDrawing state

. .

. .

.

Color control

Filling

More drawing control

Turtle stateVisibility

.

.

Appearance

.

Using events

Special Turtle methods

.

Methods of TurtleScreen/Screen

Window control

Animation control

Sử dụng các sự kiện màn hình

.

.

.

Settings and special methods

.

Input methods

Methods specific to Screen

Methods of RawTurtle/Turtle and corresponding functions

Most of the examples in this section refer to a Turtle instance called

>>> turtle.setheading[90]
>>> turtle.heading[]
90.0
8

Turtle motion

turtle. forward[distance]turtle. fd[distance] Parameters

distance – a number [integer or float]

Move the turtle forward by the specified distance, in the direction the turtle is headed

>>> turtle.position[]
[0.00,0.00]
>>> turtle.forward[25]
>>> turtle.position[]
[25.00,0.00]
>>> turtle.forward[-75]
>>> turtle.position[]
[-50.00,0.00]

turtle. back[distance]turtle. bk[distance]turtle. backward[distance] Parameters

distance – a number

Move the turtle backward by distance, opposite to the direction the turtle is headed. Do not change the turtle’s heading

>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]

turtle. right[angle]turtle. rt[angle] Parameters

angle – a number [integer or float]

Turn turtle right by angle units. [Units are by default degrees, but can be set via the and functions. ] Angle orientation depends on the turtle mode, see

>>> turtle.heading[]
22.0
>>> turtle.right[45]
>>> turtle.heading[]
337.0

turtle. left[angle]turtle. lt[angle] Parameters

angle – a number [integer or float]

Turn turtle left by angle units. [Units are by default degrees, but can be set via the and functions. ] Angle orientation depends on the turtle mode, see

>>> turtle.heading[]
22.0
>>> turtle.left[45]
>>> turtle.heading[]
67.0

turtle. goto[x , y=None]turtle. setpos[x , y=None]turtle. setposition[x , y=None] Parameters
  • x – a number or a pair/vector of numbers

  • y – a number or

    >>> turtle.position[]
    [0.00,0.00]
    >>> turtle.backward[30]
    >>> turtle.position[]
    [-30.00,0.00]
    
    29

If y is

>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
29, x must be a pair of coordinates or a [e. g. as returned by ]

Move turtle to an absolute position. Nếu bút xuống, vẽ đường. Do not change the turtle’s orientation

 >>> tp = turtle.pos[]
 >>> tp
 [0.00,0.00]
 >>> turtle.setpos[60,30]
 >>> turtle.pos[]
 [60.00,30.00]
 >>> turtle.setpos[[20,80]]
 >>> turtle.pos[]
 [20.00,80.00]
 >>> turtle.setpos[tp]
 >>> turtle.pos[]
 [0.00,0.00]

turtle. setx[x] Tham số

x – một số [số nguyên hoặc số thực]

Đặt tọa độ đầu tiên của rùa thành x, giữ nguyên tọa độ thứ hai

>>> turtle.position[]
[0.00,240.00]
>>> turtle.setx[10]
>>> turtle.position[]
[10.00,240.00]

rùa. sety[y] Tham số

y – một số [số nguyên hoặc số thực]

Đặt tọa độ thứ hai của rùa thành y, giữ nguyên tọa độ đầu tiên

>>> turtle.position[]
[0.00,40.00]
>>> turtle.sety[-10]
>>> turtle.position[]
[0.00,-10.00]

rùa. tiêu đề[to_angle]rùa. seth[to_angle] Tham số

to_angle – một số [số nguyên hoặc số thực]

Đặt hướng của rùa thành to_angle. Dưới đây là một số hướng phổ biến theo độ

chế độ căn bản

chế độ logo

0 - đông

0 - bắc

90 - north

90 - east

180 - west

180 - south

270 - south

270 - west

>>> turtle.setheading[90]
>>> turtle.heading[]
90.0

turtle. home[]

Move turtle to the origin – coordinates [0,0] – and set its heading to its start-orientation [which depends on the mode, see ]

>>> turtle.heading[]
90.0
>>> turtle.position[]
[0.00,-10.00]
>>> turtle.home[]
>>> turtle.position[]
[0.00,0.00]
>>> turtle.heading[]
0.0

turtle. circle[radius , extent=None , steps=None] Parameters
  • radius – a number

  • extent – a number [or

    >>> turtle.position[]
    [0.00,0.00]
    >>> turtle.backward[30]
    >>> turtle.position[]
    [-30.00,0.00]
    
    29]

  • steps – an integer [or

    >>> turtle.position[]
    [0.00,0.00]
    >>> turtle.backward[30]
    >>> turtle.position[]
    [-30.00,0.00]
    
    29]

Draw a circle with given radius. The center is radius units left of the turtle; extent – an angle – determines which part of the circle is drawn. If extent is not given, draw the entire circle. If extent is not a full circle, one endpoint of the arc is the current pen position. Draw the arc in counterclockwise direction if radius is positive, otherwise in clockwise direction. Finally the direction of the turtle is changed by the amount of extent

As the circle is approximated by an inscribed regular polygon, steps determines the number of steps to use. If not given, it will be calculated automatically. May be used to draw regular polygons

>>> turtle.position[]
[0.00,0.00]
>>> turtle.forward[25]
>>> turtle.position[]
[25.00,0.00]
>>> turtle.forward[-75]
>>> turtle.position[]
[-50.00,0.00]
0

turtle. dot[size=None , *color] Parameters
  • size – an integer >= 1 [if given]

  • color – a colorstring or a numeric color tuple

Draw a circular dot with diameter size, using color. If size is not given, the maximum of pensize+4 and 2*pensize is used

>>> turtle.position[]
[0.00,0.00]
>>> turtle.forward[25]
>>> turtle.position[]
[25.00,0.00]
>>> turtle.forward[-75]
>>> turtle.position[]
[-50.00,0.00]
1

turtle. stamp[]

Stamp a copy of the turtle shape onto the canvas at the current turtle position. Return a stamp_id for that stamp, which can be used to delete it by calling

>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
36

>>> turtle.position[]
[0.00,0.00]
>>> turtle.forward[25]
>>> turtle.position[]
[25.00,0.00]
>>> turtle.forward[-75]
>>> turtle.position[]
[-50.00,0.00]
2

turtle. clearstamp[stampid] Parameters

stampid – an integer, must be return value of previous call

Delete stamp with given stampid

>>> turtle.position[]
[0.00,0.00]
>>> turtle.forward[25]
>>> turtle.position[]
[25.00,0.00]
>>> turtle.forward[-75]
>>> turtle.position[]
[-50.00,0.00]
3

turtle. clearstamps[n=None] Parameters

n – an integer [or

>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
29]

Delete all or first/last n of turtle’s stamps. Nếu n là

>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
29, xóa tất cả tem, nếu n > 0 xóa n tem đầu tiên, ngược lại nếu n < 0 xóa n tem cuối cùng

>>> turtle.position[]
[0.00,0.00]
>>> turtle.forward[25]
>>> turtle.position[]
[25.00,0.00]
>>> turtle.forward[-75]
>>> turtle.position[]
[-50.00,0.00]
4

rùa. hoàn tác[]

Hoàn tác [nhiều lần] [các] hành động rùa cuối cùng. Số lượng hành động hoàn tác khả dụng được xác định bởi kích thước của bộ đệm hoàn tác

>>> turtle.position[]
[0.00,0.00]
>>> turtle.forward[25]
>>> turtle.position[]
[25.00,0.00]
>>> turtle.forward[-75]
>>> turtle.position[]
[-50.00,0.00]
5

rùa. tốc độ[tốc độ=Không]Parameters

tốc độ – một số nguyên trong phạm vi 0. 10 hoặc dây tốc độ [xem bên dưới]

Đặt tốc độ của rùa thành một giá trị nguyên trong phạm vi 0. 10. Nếu không có đối số nào được đưa ra, hãy trả về tốc độ hiện tại

Nếu đầu vào là một số lớn hơn 10 hoặc nhỏ hơn 0. 5, tốc độ được đặt thành 0. Speedstrings are mapped to speedvalues as follows

  • “nhanh nhất”. 0

  • "Nhanh". 10

  • "thông thường". 6

  • "chậm". 3

  • “chậm nhất”. 1

Tốc độ từ 1 đến 10 thực thi hoạt ảnh vẽ đường thẳng và xoay rùa ngày càng nhanh hơn

Chú ý. tốc độ = 0 có nghĩa là không có hoạt ảnh nào diễn ra. tiến/lùi khiến rùa nhảy và tương tự trái/phải khiến rùa quay ngay lập tức

>>> turtle.position[]
[0.00,0.00]
>>> turtle.forward[25]
>>> turtle.position[]
[25.00,0.00]
>>> turtle.forward[-75]
>>> turtle.position[]
[-50.00,0.00]
6

Tell Turtle’s state

rùa. vị trí[]rùa. tư thế[]

Trả về vị trí hiện tại của rùa [x,y] [dưới dạng vectơ]

>>> turtle.position[]
[0.00,0.00]
>>> turtle.forward[25]
>>> turtle.position[]
[25.00,0.00]
>>> turtle.forward[-75]
>>> turtle.position[]
[-50.00,0.00]
7

rùa. về phía[x , y=None]Parameters
  • x – một số hoặc một cặp/vec-tơ số hoặc một cá thể rùa

  • y – một số nếu x là một số, ngược lại

    >>> turtle.position[]
    [0.00,0.00]
    >>> turtle.backward[30]
    >>> turtle.position[]
    [-30.00,0.00]
    
    29

Trả lại góc giữa đường từ vị trí con rùa đến vị trí được chỉ định bởi [x,y], vectơ hoặc con rùa khác. Điều này phụ thuộc vào hướng bắt đầu của rùa, tùy thuộc vào chế độ - “tiêu chuẩn”/”thế giới” hoặc “logo”

>>> turtle.position[]
[0.00,0.00]
>>> turtle.forward[25]
>>> turtle.position[]
[25.00,0.00]
>>> turtle.forward[-75]
>>> turtle.position[]
[-50.00,0.00]
8

rùa. xcor[]

Trả về tọa độ x của con rùa

>>> turtle.position[]
[0.00,0.00]
>>> turtle.forward[25]
>>> turtle.position[]
[25.00,0.00]
>>> turtle.forward[-75]
>>> turtle.position[]
[-50.00,0.00]
9

rùa. ycor[]

Trả về tọa độ y của rùa

>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
0

rùa. tiêu đề[]

Trả về tiêu đề hiện tại của rùa [giá trị tùy thuộc vào chế độ rùa, xem phần ]

>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
1

rùa. khoảng cách[x , y=None]Parameters
  • x – một số hoặc một cặp/vec-tơ số hoặc một cá thể rùa

  • y – một số nếu x là một số, ngược lại

    >>> turtle.position[]
    [0.00,0.00]
    >>> turtle.backward[30]
    >>> turtle.position[]
    [-30.00,0.00]
    
    29

Trả về khoảng cách từ con rùa đến [x,y], vectơ đã cho hoặc con rùa đã cho khác, theo đơn vị bước rùa

>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
2

Cài đặt để đo lường

rùa. độ[vòng tròn=360. 0] Tham số

hình tròn – một số

Đặt đơn vị đo góc i. e. đặt số "độ" cho một vòng tròn đầy đủ. Giá trị mặc định là 360 độ

>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
3

rùa. radian[]

Đặt đơn vị đo góc thành radian. Tương đương với

>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
44

>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
4

điều khiển bút

Trạng thái bản vẽ

rùa. xuống dốc[]rùa. pd[]rùa. xuống[]

Kéo bút xuống – vẽ khi di chuyển

rùa. bút chì[]rùa. pu[]rùa. lên[]

Kéo bút lên – không vẽ khi di chuyển

rùa. kích thước[chiều rộng=Không có . ]turtle.chiều rộng[chiều rộng=Không có]Parameters

chiều rộng – một số dương

Đặt độ dày của đường thành chiều rộng hoặc trả lại. Nếu chế độ thay đổi kích thước được đặt thành “tự động” và hình con rùa là một đa giác, thì đa giác đó được vẽ với cùng độ dày nét vẽ. Nếu không có đối số nào được đưa ra, thì pensize hiện tại được trả về

>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
5

rùa. bút[bút=Không có, **pendict]Parameters
  • bút – một từ điển với một số hoặc tất cả các phím được liệt kê bên dưới

  • pendict – một hoặc nhiều đối số từ khóa với các khóa được liệt kê bên dưới làm từ khóa

Trả lại hoặc đặt các thuộc tính của bút trong “từ điển bút” với các cặp khóa/giá trị sau

  • "cho xem". Đúng sai

  • "đặt bút xuống". Đúng sai

  • “màu bút”. chuỗi màu hoặc bộ màu

  • "tô màu". chuỗi màu hoặc bộ màu

  • “bù”. số dương

  • "tốc độ". số trong phạm vi 0. 10

  • "chế độ thay đổi kích thước". “tự động” hoặc “người dùng” hoặc “noresize”

  • "máy kéo dài". [số dương, số dương]

  • "đề cương". số dương

  • “nghiêng”. số

Từ điển này có thể được sử dụng làm đối số cho lệnh gọi tiếp theo để khôi phục trạng thái bút cũ. Ngoài ra, một hoặc nhiều thuộc tính này có thể được cung cấp dưới dạng đối số từ khóa. Điều này có thể được sử dụng để đặt một số thuộc tính bút trong một câu lệnh

>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
6

rùa. ngừng hoạt động[]

Trả lại

>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
46 nếu bút xuống,
>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
47 nếu bút lên

>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
7

Color control

rùa. bút màu[*args]

Trả lại hoặc đặt màu bút

Bốn định dạng đầu vào được cho phép

>>> turtle.position[]
[0.00,0.00]
>>> turtle.forward[25]
>>> turtle.position[]
[25.00,0.00]
>>> turtle.forward[-75]
>>> turtle.position[]
[-50.00,0.00]
54

Trả về pencolor hiện tại dưới dạng chuỗi đặc tả màu hoặc dưới dạng bộ [xem ví dụ]. Có thể được sử dụng làm đầu vào cho lệnh gọi color/pencolor/fillcolor khác

>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
49

Đặt pencolor thành chuỗi màu, là chuỗi đặc tả màu Tk, chẳng hạn như

>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
50,
>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
51 hoặc
>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
52

>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
53

Đặt pencolor thành màu RGB được biểu thị bằng bộ của r, g và b. Mỗi r, g và b phải nằm trong phạm vi 0. colormode, trong đó colormode là 1. 0 hoặc 255 [xem ]

>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
55

Đặt pencolor thành màu RGB được biểu thị bằng r, g và b. Mỗi r, g và b phải nằm trong phạm vi 0. chế độ màu

Nếu hình con rùa là một đa giác, thì đường viền của đa giác đó được vẽ bằng bút màu mới được đặt

>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
8

rùa. màu tô[*args]

Trả lại hoặc đặt màu tô

Bốn định dạng đầu vào được cho phép

>>> turtle.position[]
[0.00,0.00]
>>> turtle.forward[25]
>>> turtle.position[]
[25.00,0.00]
>>> turtle.forward[-75]
>>> turtle.position[]
[-50.00,0.00]
55

Trả về màu tô hiện tại dưới dạng chuỗi đặc tả màu, có thể ở định dạng tuple [xem ví dụ]. Có thể được sử dụng làm đầu vào cho lệnh gọi color/pencolor/fillcolor khác

>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
57

Đặt màu tô thành chuỗi màu, là chuỗi đặc tả màu Tk, chẳng hạn như

>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
50,
>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
51 hoặc
>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
52

>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
61

Đặt màu tô thành màu RGB được biểu thị bằng bộ của r, g và b. Mỗi r, g và b phải nằm trong phạm vi 0. colormode, trong đó colormode là 1. 0 hoặc 255 [xem ]

>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
63

Đặt màu tô thành màu RGB được biểu thị bằng r, g và b. Mỗi r, g và b phải nằm trong phạm vi 0. chế độ màu

Nếu hình con rùa là một đa giác, phần bên trong của đa giác đó được vẽ bằng màu tô mới được đặt

>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
9

rùa. màu[*args]

Trả lại hoặc đặt pencolor và fillcolor

Một số định dạng đầu vào được cho phép. Họ sử dụng 0 đến 3 đối số như sau

>>> turtle.position[]
[0.00,0.00]
>>> turtle.forward[25]
>>> turtle.position[]
[25.00,0.00]
>>> turtle.forward[-75]
>>> turtle.position[]
[-50.00,0.00]
53

Trả về màu bút hiện tại và màu tô hiện tại dưới dạng một cặp chuỗi hoặc bộ đặc tả màu khi được trả về bởi và

>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
67,
>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
68,
>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
69

Các đầu vào như trong , đặt cả hai, màu tô và màu bút, thành giá trị đã cho

>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
71,
>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
72

Tương đương với

>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
73 và
>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
74 và tương tự nếu định dạng đầu vào khác được sử dụng

Nếu hình con rùa là một đa giác, đường viền và phần bên trong của đa giác đó được vẽ bằng các màu mới được đặt

>>> turtle.heading[]
22.0
>>> turtle.right[45]
>>> turtle.heading[]
337.0
0

Xem thêm. phương pháp màn hình

Filling

rùa. điền[]

Return fillstate [

>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
46 if filling,
>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
47 else]

>>> turtle.heading[]
22.0
>>> turtle.right[45]
>>> turtle.heading[]
337.0
1

rùa. begin_fill[]

Được gọi ngay trước khi vẽ một hình cần tô

rùa. end_fill[]

Tô màu cho hình được vẽ sau lần gọi cuối cùng đến

Việc các vùng chồng lấp cho đa giác tự giao nhau hoặc nhiều hình có được lấp đầy hay không tùy thuộc vào đồ họa của hệ điều hành, loại chồng lấp và số lượng chồng lấp. Ví dụ: ngôi sao Rùa ở trên có thể toàn màu vàng hoặc có một số vùng màu trắng

>>> turtle.heading[]
22.0
>>> turtle.right[45]
>>> turtle.heading[]
337.0
2

More drawing control

rùa. đặt lại[]

Xóa hình vẽ của rùa khỏi màn hình, căn giữa lại rùa và đặt các biến về giá trị mặc định

>>> turtle.heading[]
22.0
>>> turtle.right[45]
>>> turtle.heading[]
337.0
3

rùa. xóa[]

Xóa hình vẽ của rùa khỏi màn hình. Đừng di chuyển rùa. Trạng thái và vị trí của rùa cũng như hình vẽ của các loài rùa khác không bị ảnh hưởng

rùa. viết[arg , di chuyển=False, align='left', font=['Arial', 8, 'normal']]Parameters
  • arg – đối tượng được ghi vào TurtleScreen

  • di chuyển – Đúng/Sai

  • căn chỉnh – một trong các chuỗi “trái”, “trung tâm” hoặc phải”

  • phông chữ – một bộ ba [tên phông chữ, cỡ chữ, loại phông chữ]

Viết văn bản - biểu diễn chuỗi của arg - tại vị trí con rùa hiện tại theo căn chỉnh [“trái”, “giữa” hoặc “phải”] và với phông chữ đã cho. Nếu di chuyển là đúng, bút sẽ được di chuyển đến góc dưới cùng bên phải của văn bản. Theo mặc định, di chuyển là

>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
47

>>> turtle.heading[]
22.0
>>> turtle.right[45]
>>> turtle.heading[]
337.0
4

bang con rùa

Hiển thị

rùa. con rùa ẩn nấp[]con rùa. ht[]

Làm cho con rùa vô hình. Bạn nên làm điều này khi đang thực hiện một số bản vẽ phức tạp, bởi vì việc ẩn con rùa sẽ tăng tốc độ vẽ một cách đáng kể

>>> turtle.heading[]
22.0
>>> turtle.right[45]
>>> turtle.heading[]
337.0
5

rùa. rùa chiếu[]rùa. st[]

Làm cho con rùa có thể nhìn thấy

>>> turtle.heading[]
22.0
>>> turtle.right[45]
>>> turtle.heading[]
337.0
6

rùa. hiển thị[]

Quay trở lại

>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
46 nếu Rùa được hiển thị,
>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
47 nếu nó bị ẩn

>>> turtle.heading[]
22.0
>>> turtle.right[45]
>>> turtle.heading[]
337.0
7

Appearance

rùa. hình dạng[tên=Không có]Parameters

tên - một chuỗi là một shapename hợp lệ

Đặt hình dạng con rùa thành hình dạng với tên đã cho hoặc, nếu tên không được đặt, hãy trả về tên của hình dạng hiện tại. Hình dạng có tên phải tồn tại trong từ điển hình dạng của TurtleScreen. Ban đầu có các hình đa giác sau. “mũi tên”, “con rùa”, “hình tròn”, “hình vuông”, “hình tam giác”, “cổ điển”. Để tìm hiểu về cách xử lý các hình, hãy xem Phương pháp màn hình

>>> turtle.heading[]
22.0
>>> turtle.right[45]
>>> turtle.heading[]
337.0
8

rùa. resizemode[rmode=Không có]Parameters

rmode – một trong các chuỗi “auto”, “user”, “noresize”

Đặt resizemode thành một trong các giá trị. “tự động”, “người dùng”, “noresize”. Nếu rmode không được cung cấp, hãy trả về resizemode hiện tại. Các chế độ thay đổi kích thước khác nhau có các hiệu ứng sau

  • "Tự động". điều chỉnh sự xuất hiện của con rùa tương ứng với giá trị của pensize

  • "người sử dụng". điều chỉnh sự xuất hiện của con rùa theo các giá trị của hệ số kéo dài và chiều rộng đường viền [đường viền], được đặt bởi

  • “noresize”. không có sự thích nghi nào về ngoại hình của con rùa diễn ra

>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
84 được gọi bởi khi được sử dụng với các đối số

>>> turtle.heading[]
22.0
>>> turtle.right[45]
>>> turtle.heading[]
337.0
9

rùa. kích thước hình dạng[stretch_wid=Không có . , stretch_len=None, outline=None]turtle.kích thước rùa[stretch_wid=Không có, stretch_len=None, outline=None]Parameters
  • stretch_wid – số dương

  • stretch_len – positive number

  • outline – positive number

Trả lại hoặc đặt các thuộc tính x/y-stretchfactors và/hoặc phác thảo của bút. Đặt resizemode thành “người dùng”. Khi và chỉ khi chế độ thay đổi kích thước được đặt thành "người dùng", con rùa sẽ được hiển thị kéo dài theo các yếu tố kéo dài của nó. stretch_wid là hệ số giãn vuông góc với hướng của nó, stretch_len là hệ số giãn theo hướng của hướng của nó, đường viền xác định chiều rộng của đường viền của hình dạng

>>> turtle.heading[]
22.0
>>> turtle.left[45]
>>> turtle.heading[]
67.0
0

rùa. máy cắt[máy cắt=Không có]Parameters

cắt - số [tùy chọn]

Đặt hoặc trả lại shearfactor hiện tại. Cắt hình rùa theo lực cắt shearfactor đã cho, là tiếp tuyến của góc cắt. Không thay đổi tiêu đề của rùa [hướng di chuyển]. Nếu cắt không được đưa ra. trả lại shearfactor hiện tại, tôi. e. tiếp tuyến của góc cắt, theo đó các đường song song với tiêu đề của con rùa bị cắt

>>> turtle.heading[]
22.0
>>> turtle.left[45]
>>> turtle.heading[]
67.0
1

rùa. độ nghiêng[góc] Tham số

góc – một số

Xoay hình con rùa theo góc so với góc nghiêng hiện tại của nó, nhưng không thay đổi hướng của con rùa [hướng di chuyển]

>>> turtle.heading[]
22.0
>>> turtle.left[45]
>>> turtle.heading[]
67.0
2

rùa. settiltangle[góc] Tham số

góc – một số

Xoay hình con rùa để chỉ theo hướng được chỉ định theo góc, bất kể góc nghiêng hiện tại của nó là gì. Không thay đổi tiêu đề của rùa [hướng di chuyển]

>>> turtle.heading[]
22.0
>>> turtle.left[45]
>>> turtle.heading[]
67.0
3

Không dùng nữa kể từ phiên bản 3. 1

rùa. nghiêng[góc=Không có]Parameters

góc – một số [tùy chọn]

Đặt hoặc trả lại góc nghiêng hiện tại. Nếu góc được đưa ra, hãy xoay hình con rùa để chỉ theo hướng được chỉ định bởi góc, bất kể góc nghiêng hiện tại của nó là gì. Không thay đổi tiêu đề của rùa [hướng di chuyển]. Nếu góc không được đưa ra. trả lại góc nghiêng hiện tại, tôi. e. góc giữa hướng của hình con rùa và hướng của con rùa [hướng di chuyển của nó]

>>> turtle.heading[]
22.0
>>> turtle.left[45]
>>> turtle.heading[]
67.0
4

rùa. shapetransform[t11=Không, t12=None, t21=None, t22=None]Parameters
  • t11 – một số [tùy chọn]

  • t12 – một số [tùy chọn]

  • t21 – một số [tùy chọn]

  • t12 – một số [tùy chọn]

Đặt hoặc trả về ma trận biến đổi hiện tại của hình con rùa

Nếu không có phần tử nào của ma trận được đưa ra, hãy trả về ma trận biến đổi dưới dạng một bộ gồm 4 phần tử. Mặt khác, đặt các phần tử đã cho và biến đổi hình con rùa theo ma trận bao gồm hàng đầu tiên t11, t12 và hàng thứ hai t21, t22. Định thức t11 * t22 - t12 * t21 không được bằng 0, nếu không sẽ xảy ra lỗi. Chỉnh sửa stretchfactor, shearfactor và tiltangle theo ma trận đã cho

>>> turtle.heading[]
22.0
>>> turtle.left[45]
>>> turtle.heading[]
67.0
5

rùa. get_shapepoly[]

Trả về đa giác hình dạng hiện tại dưới dạng bộ cặp tọa độ. Điều này có thể được sử dụng để xác định một hình dạng mới hoặc các thành phần của một hình dạng ghép

>>> turtle.heading[]
22.0
>>> turtle.left[45]
>>> turtle.heading[]
67.0
6

Using events

rùa. title[vui , btn=1, add=None]Parameters
  • fun – một hàm có hai đối số sẽ được gọi với tọa độ của điểm được nhấp trên khung vẽ

  • btn – số nút chuột, mặc định là 1 [nút chuột trái]

  • thêm –

    >>> turtle.position[]
    [0.00,0.00]
    >>> turtle.backward[30]
    >>> turtle.position[]
    [-30.00,0.00]
    
    46 hoặc
    >>> turtle.position[]
    [0.00,0.00]
    >>> turtle.backward[30]
    >>> turtle.position[]
    [-30.00,0.00]
    
    47 – nếu
    >>> turtle.position[]
    [0.00,0.00]
    >>> turtle.backward[30]
    >>> turtle.position[]
    [-30.00,0.00]
    
    46, một đóng gáy mới sẽ được thêm vào, nếu không nó sẽ thay thế một đóng gáy cũ

Liên kết thú vị với các sự kiện nhấp chuột trên con rùa này. Nếu niềm vui là

>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
29, các ràng buộc hiện có sẽ bị xóa. Ví dụ cho con rùa vô danh, tôi. e. cách thủ tục

>>> turtle.heading[]
22.0
>>> turtle.left[45]
>>> turtle.heading[]
67.0
7

rùa. ra mắt[vui , btn=1, add=None]Parameters
  • fun – một hàm có hai đối số sẽ được gọi với tọa độ của điểm được nhấp trên khung vẽ

  • btn – số nút chuột, mặc định là 1 [nút chuột trái]

  • thêm –

    >>> turtle.position[]
    [0.00,0.00]
    >>> turtle.backward[30]
    >>> turtle.position[]
    [-30.00,0.00]
    
    46 hoặc
    >>> turtle.position[]
    [0.00,0.00]
    >>> turtle.backward[30]
    >>> turtle.position[]
    [-30.00,0.00]
    
    47 – nếu
    >>> turtle.position[]
    [0.00,0.00]
    >>> turtle.backward[30]
    >>> turtle.position[]
    [-30.00,0.00]
    
    46, một đóng gáy mới sẽ được thêm vào, nếu không nó sẽ thay thế một đóng gáy cũ

Liên kết thú vị với các sự kiện nhả nút chuột trên con rùa này. Nếu niềm vui là

>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
29, các ràng buộc hiện có sẽ bị xóa

>>> turtle.heading[]
22.0
>>> turtle.left[45]
>>> turtle.heading[]
67.0
8

rùa. ondrag[vui , btn=1, add=None]Parameters
  • fun – một hàm có hai đối số sẽ được gọi với tọa độ của điểm được nhấp trên khung vẽ

  • btn – số nút chuột, mặc định là 1 [nút chuột trái]

  • thêm –

    >>> turtle.position[]
    [0.00,0.00]
    >>> turtle.backward[30]
    >>> turtle.position[]
    [-30.00,0.00]
    
    46 hoặc
    >>> turtle.position[]
    [0.00,0.00]
    >>> turtle.backward[30]
    >>> turtle.position[]
    [-30.00,0.00]
    
    47 – nếu
    >>> turtle.position[]
    [0.00,0.00]
    >>> turtle.backward[30]
    >>> turtle.position[]
    [-30.00,0.00]
    
    46, một đóng gáy mới sẽ được thêm vào, nếu không nó sẽ thay thế một đóng gáy cũ

Bind fun to mouse-move events on this turtle. Nếu niềm vui là

>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
29, các ràng buộc hiện có sẽ bị xóa

Nhận xét. Mỗi chuỗi sự kiện di chuyển chuột trên một con rùa được bắt đầu bằng một sự kiện nhấp chuột trên con rùa đó

>>> turtle.heading[]
22.0
>>> turtle.left[45]
>>> turtle.heading[]
67.0
9

Sau đó, nhấp và kéo Rùa sẽ di chuyển nó trên màn hình do đó tạo ra các hình vẽ tay [nếu bút bị hỏng]

Special Turtle methods

rùa. begin_poly[]

Bắt đầu ghi các đỉnh của một đa giác. Vị trí con rùa hiện tại là đỉnh đầu tiên của đa giác

rùa. end_poly[]

Dừng ghi các đỉnh của một đa giác. Vị trí con rùa hiện tại là đỉnh cuối cùng của đa giác. Điều này sẽ được kết nối với đỉnh đầu tiên

rùa. get_poly[]

Trả lại đa giác được ghi cuối cùng

 >>> tp = turtle.pos[]
 >>> tp
 [0.00,0.00]
 >>> turtle.setpos[60,30]
 >>> turtle.pos[]
 [60.00,30.00]
 >>> turtle.setpos[[20,80]]
 >>> turtle.pos[]
 [20.00,80.00]
 >>> turtle.setpos[tp]
 >>> turtle.pos[]
 [0.00,0.00]
0

rùa. nhân bản[]

Tạo và trả về một bản sao của con rùa với cùng vị trí, tiêu đề và thuộc tính của con rùa

 >>> tp = turtle.pos[]
 >>> tp
 [0.00,0.00]
 >>> turtle.setpos[60,30]
 >>> turtle.pos[]
 [60.00,30.00]
 >>> turtle.setpos[[20,80]]
 >>> turtle.pos[]
 [20.00,80.00]
 >>> turtle.setpos[tp]
 >>> turtle.pos[]
 [0.00,0.00]
1

rùa. con rùa[]con rùa. getpen[]

Trả lại chính đối tượng Rùa. Chỉ sử dụng hợp lý. như một chức năng để trả về "rùa ẩn danh"

 >>> tp = turtle.pos[]
 >>> tp
 [0.00,0.00]
 >>> turtle.setpos[60,30]
 >>> turtle.pos[]
 [60.00,30.00]
 >>> turtle.setpos[[20,80]]
 >>> turtle.pos[]
 [20.00,80.00]
 >>> turtle.setpos[tp]
 >>> turtle.pos[]
 [0.00,0.00]
2

rùa. nhận màn hình[]

Trả lại đối tượng mà con rùa đang vẽ. Các phương thức TurtleScreen sau đó có thể được gọi cho đối tượng đó

 >>> tp = turtle.pos[]
 >>> tp
 [0.00,0.00]
 >>> turtle.setpos[60,30]
 >>> turtle.pos[]
 [60.00,30.00]
 >>> turtle.setpos[[20,80]]
 >>> turtle.pos[]
 [20.00,80.00]
 >>> turtle.setpos[tp]
 >>> turtle.pos[]
 [0.00,0.00]
3

rùa. setundobuffer[kích thước] Tham số

kích thước – một số nguyên hoặc

>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
29

Đặt hoặc vô hiệu hóa bộ đệm. Nếu kích thước là một số nguyên, một bộ hoàn tác trống có kích thước đã cho sẽ được cài đặt. size đưa ra số lượng hành động rùa tối đa có thể hoàn tác bằng phương thức/hàm. Nếu kích thước là

>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
29, bộ hoàn tác bị vô hiệu hóa

 >>> tp = turtle.pos[]
 >>> tp
 [0.00,0.00]
 >>> turtle.setpos[60,30]
 >>> turtle.pos[]
 [60.00,30.00]
 >>> turtle.setpos[[20,80]]
 >>> turtle.pos[]
 [20.00,80.00]
 >>> turtle.setpos[tp]
 >>> turtle.pos[]
 [0.00,0.00]
4

rùa. hủy bỏ bộ đệm[]

Trả về số mục nhập trong bộ hoàn tác

 >>> tp = turtle.pos[]
 >>> tp
 [0.00,0.00]
 >>> turtle.setpos[60,30]
 >>> turtle.pos[]
 [60.00,30.00]
 >>> turtle.setpos[[20,80]]
 >>> turtle.pos[]
 [20.00,80.00]
 >>> turtle.setpos[tp]
 >>> turtle.pos[]
 [0.00,0.00]
5

hình dạng hợp chất

Để sử dụng các hình con rùa phức hợp, bao gồm một số đa giác có màu khác nhau, bạn phải sử dụng lớp trình trợ giúp một cách rõ ràng như được mô tả bên dưới

  1. Tạo một đối tượng Shape rỗng kiểu “compound”

  2. Thêm bao nhiêu thành phần vào đối tượng này tùy thích, sử dụng phương thức

    >>> turtle.heading[]
    22.0
    >>> turtle.right[45]
    >>> turtle.heading[]
    337.0
    
    03

    Ví dụ

     >>> tp = turtle.pos[]
     >>> tp
     [0.00,0.00]
     >>> turtle.setpos[60,30]
     >>> turtle.pos[]
     [60.00,30.00]
     >>> turtle.setpos[[20,80]]
     >>> turtle.pos[]
     [20.00,80.00]
     >>> turtle.setpos[tp]
     >>> turtle.pos[]
     [0.00,0.00]
    
    6

  3. Bây giờ hãy thêm Hình dạng vào danh sách hình dạng của Màn hình và sử dụng nó

     >>> tp = turtle.pos[]
     >>> tp
     [0.00,0.00]
     >>> turtle.setpos[60,30]
     >>> turtle.pos[]
     [60.00,30.00]
     >>> turtle.setpos[[20,80]]
     >>> turtle.pos[]
     [20.00,80.00]
     >>> turtle.setpos[tp]
     >>> turtle.pos[]
     [0.00,0.00]
    
    7

Note

Lớp được phương thức sử dụng nội bộ theo những cách khác nhau. Người lập trình ứng dụng chỉ phải xử lý lớp Hình dạng khi sử dụng các hình dạng phức hợp như được hiển thị ở trên

Các phương thức của TurtleScreen/Screen và các chức năng tương ứng

Hầu hết các ví dụ trong phần này đề cập đến một phiên bản TurtleScreen có tên là

>>> turtle.heading[]
22.0
>>> turtle.right[45]
>>> turtle.heading[]
337.0
06

Window control

rùa. bgcolor[*args]Parameters

args – một chuỗi màu hoặc ba số trong phạm vi 0. colormode hoặc một bộ 3 số như vậy

Đặt hoặc trả về màu nền của TurtleScreen

 >>> tp = turtle.pos[]
 >>> tp
 [0.00,0.00]
 >>> turtle.setpos[60,30]
 >>> turtle.pos[]
 [60.00,30.00]
 >>> turtle.setpos[[20,80]]
 >>> turtle.pos[]
 [20.00,80.00]
 >>> turtle.setpos[tp]
 >>> turtle.pos[]
 [0.00,0.00]
8

rùa. bgpic[picname=Không có]Parameters

picname – một chuỗi, tên của tệp gif hoặc

>>> turtle.heading[]
22.0
>>> turtle.right[45]
>>> turtle.heading[]
337.0
07 hoặc
>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
29

Đặt hình nền hoặc trả về tên của hình nền hiện tại. Nếu picname là tên tệp, hãy đặt hình ảnh tương ứng làm nền. Nếu picname là

>>> turtle.heading[]
22.0
>>> turtle.right[45]
>>> turtle.heading[]
337.0
07, hãy xóa hình nền, nếu có. Nếu picname là
>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
29, hãy trả về tên tệp của ảnh nền hiện tại

 >>> tp = turtle.pos[]
 >>> tp
 [0.00,0.00]
 >>> turtle.setpos[60,30]
 >>> turtle.pos[]
 [60.00,30.00]
 >>> turtle.setpos[[20,80]]
 >>> turtle.pos[]
 [20.00,80.00]
 >>> turtle.setpos[tp]
 >>> turtle.pos[]
 [0.00,0.00]
9

rùa. xóa[]

Note

Phương thức TurtleScreen này chỉ khả dụng dưới dạng hàm toàn cầu dưới tên

>>> turtle.heading[]
22.0
>>> turtle.right[45]
>>> turtle.heading[]
337.0
11. Hàm toàn cục
>>> turtle.heading[]
22.0
>>> turtle.right[45]
>>> turtle.heading[]
337.0
12 là một hàm khác bắt nguồn từ phương thức Rùa
>>> turtle.heading[]
22.0
>>> turtle.right[45]
>>> turtle.heading[]
337.0
12

rùa. clearscreen[]

Xóa tất cả các bản vẽ và tất cả rùa khỏi TurtleScreen. Đặt lại TurtleScreen hiện đang trống về trạng thái ban đầu. nền trắng, không có hình nền, không có ràng buộc sự kiện và theo dõi trên

rùa. đặt lại[]

Note

Phương thức TurtleScreen này chỉ khả dụng dưới dạng hàm toàn cầu dưới tên

>>> turtle.heading[]
22.0
>>> turtle.right[45]
>>> turtle.heading[]
337.0
14. Hàm toàn cục
>>> turtle.heading[]
22.0
>>> turtle.right[45]
>>> turtle.heading[]
337.0
15 là một hàm khác bắt nguồn từ phương thức Rùa
>>> turtle.heading[]
22.0
>>> turtle.right[45]
>>> turtle.heading[]
337.0
15

rùa. đặt lại màn hình[]

Đặt lại tất cả Rùa trên Màn hình về trạng thái ban đầu

rùa. kích thước màn hình[canvwidth=Không có, canvheight=None, bg=None]Parameters
  • canvwidth – số nguyên dương, chiều rộng mới của canvas tính bằng pixel

  • canvheight – số nguyên dương, chiều cao mới của canvas tính bằng pixel

  • bg – chuỗi màu hoặc bộ màu, màu nền mới

Nếu không có đối số nào được đưa ra, hãy trả về giá trị hiện tại [canvaswidth, canvasheight]. Khác thay đổi kích thước canvas mà rùa đang vẽ trên đó. Không thay đổi cửa sổ bản vẽ. Để quan sát các phần ẩn của canvas, hãy sử dụng thanh cuộn. Với phương pháp này, người ta có thể hiển thị các phần của bản vẽ nằm ngoài canvas trước đó.

>>> turtle.position[]
[0.00,240.00]
>>> turtle.setx[10]
>>> turtle.position[]
[10.00,240.00]
0

e. g. để tìm kiếm một con rùa trốn thoát nhầm ;-]

rùa. tọa độ setworld[llx , lly, urx, ury]Parameters
  • llx – một số, tọa độ x của góc dưới bên trái của canvas

  • lly – một số, tọa độ y của góc dưới bên trái của canvas

  • urx – một số, tọa độ x của góc trên bên phải của canvas

  • ury – một số, tọa độ y của góc trên bên phải của canvas

Thiết lập hệ tọa độ do người dùng định nghĩa và chuyển sang chế độ “thế giới” nếu cần. Điều này thực hiện một

>>> turtle.heading[]
22.0
>>> turtle.right[45]
>>> turtle.heading[]
337.0
17. Nếu chế độ “thế giới” đã hoạt động, tất cả các bản vẽ được vẽ lại theo tọa độ mới

CHÚ Ý. trong các góc của hệ tọa độ do người dùng xác định có thể bị biến dạng

>>> turtle.position[]
[0.00,240.00]
>>> turtle.setx[10]
>>> turtle.position[]
[10.00,240.00]
1

Animation control

rùa. độ trễ[độ trễ=Không có]Parameters

độ trễ – số nguyên dương

Đặt hoặc trả về độ trễ bản vẽ tính bằng mili giây. [Đây là khoảng thời gian xấp xỉ giữa hai lần cập nhật canvas liên tiếp. ] Độ trễ vẽ càng lâu, hoạt ảnh càng chậm

Đối số tùy chọn

>>> turtle.position[]
[0.00,240.00]
>>> turtle.setx[10]
>>> turtle.position[]
[10.00,240.00]
2

rùa. trình theo dõi[n=Không có, delay=None]Parameters
  • n – số nguyên không âm

  • độ trễ – số nguyên không âm

Bật/tắt hoạt hình rùa và đặt độ trễ cho bản vẽ cập nhật. Nếu n được cung cấp, chỉ mỗi lần cập nhật màn hình thông thường thứ n thực sự được thực hiện. [Có thể được sử dụng để tăng tốc độ vẽ đồ họa phức tạp. ] Khi được gọi mà không có đối số, trả về giá trị hiện được lưu trữ của n. Đối số thứ hai đặt giá trị độ trễ [xem]

>>> turtle.position[]
[0.00,240.00]
>>> turtle.setx[10]
>>> turtle.position[]
[10.00,240.00]
3

rùa. cập nhật[]

Thực hiện cập nhật TurtleScreen. Được sử dụng khi tắt trình theo dõi

Xem thêm phương pháp RawTurtle/Turtle

Sử dụng các sự kiện màn hình

rùa. nghe này[xdummy=Không, ydummy=None]

Đặt trọng tâm vào TurtleScreen [để thu thập các sự kiện quan trọng]. Các đối số giả được cung cấp để có thể chuyển sang phương thức title

rùa. onkey[vui , key . ]turtle.onkeyrelease[thú vị , phím]Parameters
  • fun – một hàm không có đối số hoặc

    >>> turtle.position[]
    [0.00,0.00]
    >>> turtle.backward[30]
    >>> turtle.position[]
    [-30.00,0.00]
    
    29

  • chìa khóa - một chuỗi. chìa khóa [e. g. “a”] hoặc phím-ký hiệu [e. g. "không gian"]

Liên kết thú vị với sự kiện phát hành khóa của khóa. Nếu niềm vui là

>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
29, các ràng buộc sự kiện sẽ bị xóa. Nhận xét. để có thể đăng ký các sự kiện chính, TurtleScreen phải có tiêu điểm. [Xem phương pháp. ]

>>> turtle.position[]
[0.00,240.00]
>>> turtle.setx[10]
>>> turtle.position[]
[10.00,240.00]
4

rùa. nhấn phím[vui , phím=None]Parameters
  • fun – một hàm không có đối số hoặc

    >>> turtle.position[]
    [0.00,0.00]
    >>> turtle.backward[30]
    >>> turtle.position[]
    [-30.00,0.00]
    
    29

  • chìa khóa - một chuỗi. chìa khóa [e. g. “a”] hoặc phím-ký hiệu [e. g. "không gian"]

Liên kết niềm vui với sự kiện nhấn phím của phím nếu phím được cung cấp hoặc với bất kỳ sự kiện nhấn phím nào nếu không có phím nào được cung cấp. Nhận xét. để có thể đăng ký các sự kiện chính, TurtleScreen phải có tiêu điểm. [See method . ]

>>> turtle.position[]
[0.00,240.00]
>>> turtle.setx[10]
>>> turtle.position[]
[10.00,240.00]
5

rùa. title[vui , btn . =1, add=None]turtle.nhấp chuột trên màn hình[thú vị , btn=1, add=None]Parameters
  • fun – một hàm có hai đối số sẽ được gọi với tọa độ của điểm được nhấp trên khung vẽ

  • btn – số nút chuột, mặc định là 1 [nút chuột trái]

  • thêm –

    >>> turtle.position[]
    [0.00,0.00]
    >>> turtle.backward[30]
    >>> turtle.position[]
    [-30.00,0.00]
    
    46 hoặc
    >>> turtle.position[]
    [0.00,0.00]
    >>> turtle.backward[30]
    >>> turtle.position[]
    [-30.00,0.00]
    
    47 – nếu
    >>> turtle.position[]
    [0.00,0.00]
    >>> turtle.backward[30]
    >>> turtle.position[]
    [-30.00,0.00]
    
    46, một đóng gáy mới sẽ được thêm vào, nếu không nó sẽ thay thế một đóng gáy cũ

Liên kết thú vị với các sự kiện nhấp chuột trên màn hình này. Nếu niềm vui là

>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
29, các ràng buộc hiện có sẽ bị xóa

Ví dụ cho một phiên bản TurtleScreen có tên là

>>> turtle.heading[]
22.0
>>> turtle.right[45]
>>> turtle.heading[]
337.0
06 và một phiên bản Rùa có tên là
>>> turtle.setheading[90]
>>> turtle.heading[]
90.0
8

>>> turtle.position[]
[0.00,240.00]
>>> turtle.setx[10]
>>> turtle.position[]
[10.00,240.00]
6

Note

Phương thức TurtleScreen này chỉ khả dụng dưới dạng hàm toàn cầu dưới tên

>>> turtle.heading[]
22.0
>>> turtle.right[45]
>>> turtle.heading[]
337.0
32. Hàm toàn cục
>>> turtle.heading[]
22.0
>>> turtle.right[45]
>>> turtle.heading[]
337.0
33 là một hàm khác bắt nguồn từ phương thức Rùa
>>> turtle.heading[]
22.0
>>> turtle.right[45]
>>> turtle.heading[]
337.0
33

rùa. đúng giờ[vui , t=0]Parameters
  • fun – một hàm không có đối số

  • t – một số >= 0

Cài đặt bộ hẹn giờ gọi vui sau t mili giây

>>> turtle.position[]
[0.00,240.00]
>>> turtle.setx[10]
>>> turtle.position[]
[10.00,240.00]
7

rùa. vòng lặp chính[]rùa. xong[]

Bắt đầu vòng lặp sự kiện - gọi hàm mainloop của Tkinter. Phải là câu lệnh cuối cùng trong chương trình đồ họa con rùa. Không được sử dụng nếu tập lệnh được chạy từ bên trong IDLE ở chế độ -n [Không có quy trình phụ] - để sử dụng tương tác đồ họa con rùa

>>> turtle.position[]
[0.00,240.00]
>>> turtle.setx[10]
>>> turtle.position[]
[10.00,240.00]
8

Input methods

rùa. văn bản nhập[tiêu đề , nhắc]Parameters
  • tiêu đề - chuỗi

  • dấu nhắc - chuỗi

Bật lên một cửa sổ hộp thoại để nhập chuỗi. Tiêu đề tham số là tiêu đề của cửa sổ hộp thoại, dấu nhắc là văn bản chủ yếu mô tả thông tin cần nhập. Trả lại đầu vào chuỗi. Nếu hộp thoại bị hủy, hãy quay lại

>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
29

>>> turtle.position[]
[0.00,240.00]
>>> turtle.setx[10]
>>> turtle.position[]
[10.00,240.00]
9

rùa. số[tiêu đề , nhắc, default=None, minval=None, maxval=None]Parameters
  • tiêu đề - chuỗi

  • dấu nhắc - chuỗi

  • mặc định – số [tùy chọn]

  • minval – số [tùy chọn]

  • maxval – số [tùy chọn]

Bật lên một cửa sổ hộp thoại để nhập số. tiêu đề là tiêu đề của cửa sổ hộp thoại, dấu nhắc là văn bản chủ yếu mô tả thông tin số cần nhập. vỡ nợ. giá trị mặc định, minval. giá trị tối thiểu cho đầu vào, maxval. giá trị tối đa cho đầu vào. Đầu vào số phải nằm trong phạm vi minval. maxval nếu chúng được đưa ra. Nếu không, một gợi ý sẽ được đưa ra và hộp thoại vẫn mở để chỉnh sửa. Trả về số đầu vào. Nếu hộp thoại bị hủy, hãy quay lại

>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
29

>>> turtle.position[]
[0.00,40.00]
>>> turtle.sety[-10]
>>> turtle.position[]
[0.00,-10.00]
0

Settings and special methods

rùa. chế độ[chế độ=Không có]Parameters

chế độ – một trong các chuỗi “tiêu chuẩn”, “logo” hoặc “thế giới”

Đặt chế độ rùa [“tiêu chuẩn”, “logo” hoặc “thế giới”] và thực hiện đặt lại. Nếu chế độ không được cung cấp, chế độ hiện tại được trả về

Chế độ “tiêu chuẩn” tương thích với cũ. Chế độ “logo” tương thích với hầu hết đồ họa Logo rùa. Chế độ “thế giới” sử dụng “tọa độ thế giới” do người dùng xác định. Chú ý. ở chế độ này, các góc có vẻ bị biến dạng nếu

>>> turtle.heading[]
22.0
>>> turtle.right[45]
>>> turtle.heading[]
337.0
38 tỷ lệ đơn vị không bằng 1

Chế độ

tiêu đề rùa ban đầu

góc dương

"Tiêu chuẩn"

bên phải [phía đông]

ngược chiều kim đồng hồ

"Logo"

hướng lên trên [bắc]

theo chiều kim đồng hồ

>>> turtle.position[]
[0.00,40.00]
>>> turtle.sety[-10]
>>> turtle.position[]
[0.00,-10.00]
1

rùa. chế độ màu[cmode=Không có]Parameters

cmode – một trong các giá trị 1. 0 hoặc 255

Trả về colormode hoặc đặt thành 1. 0 hoặc 255. Sau đó, các giá trị r, g, b của bộ ba màu phải nằm trong khoảng 0. *cmode*

>>> turtle.position[]
[0.00,40.00]
>>> turtle.sety[-10]
>>> turtle.position[]
[0.00,-10.00]
2

rùa. getcanvas[]

Trả lại Canvas của TurtleScreen này. Hữu ích cho những người trong cuộc biết phải làm gì với Tkinter Canvas

>>> turtle.position[]
[0.00,40.00]
>>> turtle.sety[-10]
>>> turtle.position[]
[0.00,-10.00]
3

rùa. hình dạng[]

Trả về danh sách tên của tất cả các hình con rùa hiện có

>>> turtle.position[]
[0.00,40.00]
>>> turtle.sety[-10]
>>> turtle.position[]
[0.00,-10.00]
4

rùa. register_shape[tên , hình . =None]turtle.thêm hình dạng[tên , hình dạng=None]

Có ba cách khác nhau để gọi chức năng này

  1. tên là tên của tệp gif và hình dạng là

    >>> turtle.position[]
    [0.00,0.00]
    >>> turtle.backward[30]
    >>> turtle.position[]
    [-30.00,0.00]
    
    29. Cài đặt hình ảnh tương ứng

    >>> turtle.position[]
    [0.00,40.00]
    >>> turtle.sety[-10]
    >>> turtle.position[]
    [0.00,-10.00]
    
    5

    Note

    Các hình ảnh không xoay khi xoay rùa nên không hiển thị tiêu đề của rùa

  2. tên là một chuỗi tùy ý và hình dạng là một bộ các cặp tọa độ. Cài đặt hình đa giác tương ứng

    >>> turtle.position[]
    [0.00,40.00]
    >>> turtle.sety[-10]
    >>> turtle.position[]
    [0.00,-10.00]
    
    6

  3. tên là một chuỗi tùy ý và hình dạng là một đối tượng [ghép]. Cài đặt hình ghép tương ứng

Thêm hình con rùa vào danh sách hình dạng của TurtleScreen. Chỉ có thể sử dụng các hình dạng đã đăng ký như vậy bằng cách ban hành lệnh

>>> turtle.heading[]
22.0
>>> turtle.right[45]
>>> turtle.heading[]
337.0
41

rùa. rùa[]

Trả về danh sách rùa trên màn hình

>>> turtle.position[]
[0.00,40.00]
>>> turtle.sety[-10]
>>> turtle.position[]
[0.00,-10.00]
7

rùa. window_height[]

Trả về chiều cao của cửa sổ con rùa

>>> turtle.position[]
[0.00,40.00]
>>> turtle.sety[-10]
>>> turtle.position[]
[0.00,-10.00]
8

rùa. window_width[]

Trả về chiều rộng của cửa sổ con rùa

>>> turtle.position[]
[0.00,40.00]
>>> turtle.sety[-10]
>>> turtle.position[]
[0.00,-10.00]
9

Các phương thức dành riêng cho Màn hình, không được kế thừa từ TurtleScreen

rùa. tạm biệt[]

Đóng cửa sổ đồ họa con rùa

rùa. nhấp chuột thoát[]

Ràng buộc phương thức

>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
18 để nhấp chuột trên Màn hình

Nếu giá trị “using_IDLE” trong từ điển cấu hình là

>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
47 [giá trị mặc định], cũng nhập vòng lặp chính. Nhận xét. Nếu IDLE với công tắc
>>> turtle.heading[]
90.0
>>> turtle.position[]
[0.00,-10.00]
>>> turtle.home[]
>>> turtle.position[]
[0.00,0.00]
>>> turtle.heading[]
0.0
3 [không có quy trình phụ] được sử dụng, thì giá trị này phải được đặt thành
>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
46 trong
>>> turtle.heading[]
22.0
>>> turtle.right[45]
>>> turtle.heading[]
337.0
46. Trong trường hợp này, vòng lặp chính của IDLE cũng hoạt động cho tập lệnh máy khách

rùa. thiết lập[chiều rộng=_CFG, height=_CFG['height'], startx=_CFG['leftright'], starty=_CFG['topbottom']]

Đặt kích thước và vị trí của cửa sổ chính. Giá trị mặc định của đối số được lưu trữ trong từ điển cấu hình và có thể được thay đổi thông qua tệp

>>> turtle.heading[]
22.0
>>> turtle.right[45]
>>> turtle.heading[]
337.0
46

Thông số
  • chiều rộng – nếu là số nguyên, kích thước tính bằng pixel, nếu là số float, là một phần nhỏ của màn hình;

  • chiều cao – nếu là số nguyên, chiều cao tính bằng pixel, nếu là số float, là một phần nhỏ của màn hình;

  • startx – nếu dương, vị trí bắt đầu tính bằng pixel từ cạnh trái của màn hình, nếu âm từ cạnh phải, nếu

    >>> turtle.position[]
    [0.00,0.00]
    >>> turtle.backward[30]
    >>> turtle.position[]
    [-30.00,0.00]
    
    29, cửa sổ ở giữa theo chiều ngang

  • starty – nếu dương, vị trí bắt đầu tính bằng pixel từ cạnh trên của màn hình, nếu âm từ cạnh dưới, nếu

    >>> turtle.position[]
    [0.00,0.00]
    >>> turtle.backward[30]
    >>> turtle.position[]
    [-30.00,0.00]
    
    29, cửa sổ ở giữa theo chiều dọc

>>> turtle.setheading[90]
>>> turtle.heading[]
90.0
0

turtle. title[titlestring] Parameters

titlestring – a string that is shown in the titlebar of the turtle graphics window

Set title of turtle window to titlestring

>>> turtle.setheading[90]
>>> turtle.heading[]
90.0
1

Public classes

class turtle. RawTurtle[canvas]class turtle. RawPen[canvas] Parameters

canvas – a

>>> turtle.heading[]
90.0
>>> turtle.position[]
[0.00,-10.00]
>>> turtle.home[]
>>> turtle.position[]
[0.00,0.00]
>>> turtle.heading[]
0.0
6, a or a

Create a turtle. The turtle has all methods described above as “methods of Turtle/RawTurtle”

class turtle. Turtle

Subclass of RawTurtle, has the same interface but draws on a default object created automatically when needed for the first time

class turtle. TurtleScreen[cv] Parameters

cv – a

>>> turtle.heading[]
90.0
>>> turtle.position[]
[0.00,-10.00]
>>> turtle.home[]
>>> turtle.position[]
[0.00,0.00]
>>> turtle.heading[]
0.0
6

Provides screen oriented methods like

>>> turtle.heading[]
22.0
>>> turtle.right[45]
>>> turtle.heading[]
337.0
55 etc. that are described above

class turtle. Screen

Subclass of TurtleScreen, with

class turtle. ScrolledCanvas[master] Parameters

master – some Tkinter widget to contain the ScrolledCanvas, i. e. a Tkinter-canvas with scrollbars added

Used by class Screen, which thus automatically provides a ScrolledCanvas as playground for the turtles

class turtle. Shape[type_ , data] Parameters

type_ – one of the strings “polygon”, “image”, “compound”

Data structure modeling shapes. The pair

>>> turtle.heading[]
22.0
>>> turtle.right[45]
>>> turtle.heading[]
337.0
56 must follow this specification

type_

data

“polygon”

a polygon-tuple, i. e. a tuple of pairs of coordinates

“image”

an image [in this form only used internally. ]

“compound”

>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
29 [a compound shape has to be constructed using the method]

addcomponent[poly , fill , outline=None] Parameters
  • poly – a polygon, i. e. a tuple of pairs of numbers

  • fill – a color the poly will be filled with

  • outline – a color for the poly’s outline [if given]

Example

>>> turtle.setheading[90]
>>> turtle.heading[]
90.0
2

See

class turtle. Vec2D[x , y]

A two-dimensional vector class, used as a helper class for implementing turtle graphics. May be useful for turtle graphics programs too. Derived from tuple, so a vector is a tuple

Provides [for a, b vectors, k number]

  • >>> turtle.heading[]
    22.0
    >>> turtle.right[45]
    >>> turtle.heading[]
    337.0
    
    59 vector addition

  • >>> turtle.heading[]
    22.0
    >>> turtle.right[45]
    >>> turtle.heading[]
    337.0
    
    60 vector subtraction

  • >>> turtle.heading[]
    22.0
    >>> turtle.right[45]
    >>> turtle.heading[]
    337.0
    
    61 inner product

  • >>> turtle.heading[]
    22.0
    >>> turtle.right[45]
    >>> turtle.heading[]
    337.0
    
    62 and
    >>> turtle.heading[]
    22.0
    >>> turtle.right[45]
    >>> turtle.heading[]
    337.0
    
    63 multiplication with scalar

  • >>> turtle.heading[]
    22.0
    >>> turtle.right[45]
    >>> turtle.heading[]
    337.0
    
    64 absolute value of a

  • >>> turtle.heading[]
    22.0
    >>> turtle.right[45]
    >>> turtle.heading[]
    337.0
    
    65 rotation

Help and configuration

How to use help

The public methods of the Screen and Turtle classes are documented extensively via docstrings. So these can be used as online-help via the Python help facilities

  • When using IDLE, tooltips show the signatures and first lines of the docstrings of typed in function-/method calls

  • Calling on methods or functions displays the docstrings

    >>> turtle.setheading[90]
    >>> turtle.heading[]
    90.0
    
    3

  • The docstrings of the functions which are derived from methods have a modified form

    >>> turtle.setheading[90]
    >>> turtle.heading[]
    90.0
    
    4

These modified docstrings are created automatically together with the function definitions that are derived from the methods at import time

Translation of docstrings into different languages

There is a utility to create a dictionary the keys of which are the method names and the values of which are the docstrings of the public methods of the classes Screen and Turtle

turtle. write_docstringdict[filename='turtle_docstringdict'] Parameters

filename – a string, used as filename

Create and write docstring-dictionary to a Python script with the given filename. This function has to be called explicitly [it is not used by the turtle graphics classes]. The docstring dictionary will be written to the Python script

>>> turtle.heading[]
22.0
>>> turtle.right[45]
>>> turtle.heading[]
337.0
67. It is intended to serve as a template for translation of the docstrings into different languages

If you [or your students] want to use with online help in your native language, you have to translate the docstrings and save the resulting file as e. g.

>>> turtle.heading[]
22.0
>>> turtle.right[45]
>>> turtle.heading[]
337.0
69

If you have an appropriate entry in your

>>> turtle.heading[]
22.0
>>> turtle.right[45]
>>> turtle.heading[]
337.0
46 file this dictionary will be read in at import time and will replace the original English docstrings

At the time of this writing there are docstring dictionaries in German and in Italian. [Requests please to glingl @ aon . at. ]

How to configure Screen and Turtles

The built-in default configuration mimics the appearance and behaviour of the old turtle module in order to retain best possible compatibility with it

If you want to use a different configuration which better reflects the features of this module or which better fits to your needs, e. g. for use in a classroom, you can prepare a configuration file

>>> turtle.heading[]
22.0
>>> turtle.right[45]
>>> turtle.heading[]
337.0
46 which will be read at import time and modify the configuration according to its settings

The built in configuration would correspond to the following turtle. cfg

>>> turtle.setheading[90]
>>> turtle.heading[]
90.0
5

Short explanation of selected entries

  • Bốn dòng đầu tiên tương ứng với các đối số của phương thức

    >>> turtle.heading[]
    22.0
    >>> turtle.right[45]
    >>> turtle.heading[]
    337.0
    
    72

  • Line 5 and 6 correspond to the arguments of the method

    >>> turtle.heading[]
    22.0
    >>> turtle.right[45]
    >>> turtle.heading[]
    337.0
    
    73

  • shape can be any of the built-in shapes, e. g. arrow, turtle, etc. For more info try

    >>> turtle.heading[]
    22.0
    >>> turtle.right[45]
    >>> turtle.heading[]
    337.0
    
    74

  • If you want to use no fillcolor [i. e. make the turtle transparent], you have to write

    >>> turtle.heading[]
    22.0
    >>> turtle.right[45]
    >>> turtle.heading[]
    337.0
    
    75 [but all nonempty strings must not have quotes in the cfg-file]

  • If you want to reflect the turtle its state, you have to use

    >>> turtle.heading[]
    22.0
    >>> turtle.right[45]
    >>> turtle.heading[]
    337.0
    
    76

  • If you set e. g.

    >>> turtle.heading[]
    22.0
    >>> turtle.right[45]
    >>> turtle.heading[]
    337.0
    
    77 the docstringdict
    >>> turtle.heading[]
    22.0
    >>> turtle.right[45]
    >>> turtle.heading[]
    337.0
    
    78 will be loaded at import time [if present on the import path, e. g. in the same directory as ]

  • The entries exampleturtle and examplescreen define the names of these objects as they occur in the docstrings. The transformation of method-docstrings to function-docstrings will delete these names from the docstrings

  • using_IDLE. Set this to

    >>> turtle.position[]
    [0.00,0.00]
    >>> turtle.backward[30]
    >>> turtle.position[]
    [-30.00,0.00]
    
    46 if you regularly work with IDLE and its
    >>> turtle.heading[]
    90.0
    >>> turtle.position[]
    [0.00,-10.00]
    >>> turtle.home[]
    >>> turtle.position[]
    [0.00,0.00]
    >>> turtle.heading[]
    0.0
    
    3 switch [“no subprocess”]. This will prevent to enter the mainloop

There can be a

>>> turtle.heading[]
22.0
>>> turtle.right[45]
>>> turtle.heading[]
337.0
46 file in the directory where is stored and an additional one in the current working directory. The latter will override the settings of the first one

The

>>> turtle.heading[]
22.0
>>> turtle.right[45]
>>> turtle.heading[]
337.0
85 directory contains a
>>> turtle.heading[]
22.0
>>> turtle.right[45]
>>> turtle.heading[]
337.0
46 file. You can study it as an example and see its effects when running the demos [preferably not from within the demo-viewer]

— Demo scripts

The package includes a set of demo scripts. These scripts can be run and viewed using the supplied demo viewer as follows

>>> turtle.setheading[90]
>>> turtle.heading[]
90.0
6

Alternatively, you can run the demo scripts individually. For example,

>>> turtle.setheading[90]
>>> turtle.heading[]
90.0
7

The package directory contains

  • A demo viewer

    >>> turtle.heading[]
    22.0
    >>> turtle.right[45]
    >>> turtle.heading[]
    337.0
    
    90 which can be used to view the sourcecode of the scripts and run them at the same time

  • Multiple scripts demonstrating different features of the module. Examples can be accessed via the Examples menu. They can also be run standalone

  • A

    >>> turtle.heading[]
    22.0
    >>> turtle.right[45]
    >>> turtle.heading[]
    337.0
    
    46 file which serves as an example of how to write and use such files

The demo scripts are

Name

Description

Features

bytedesign

complex classical turtle graphics pattern

>>> turtle.position[]
[0.00,0.00]
>>> turtle.forward[25]
>>> turtle.position[]
[25.00,0.00]
>>> turtle.forward[-75]
>>> turtle.position[]
[-50.00,0.00]
96, delay,
>>> turtle.position[]
[0.00,0.00]
>>> turtle.forward[25]
>>> turtle.position[]
[25.00,0.00]
>>> turtle.forward[-75]
>>> turtle.position[]
[-50.00,0.00]
97

chaos

graphs Verhulst dynamics, shows that computer’s computations can generate results sometimes against the common sense expectations

tọa độ thế giới

clock

analog clock showing time of your computer

turtles as clock’s hands, ontimer

colormixer

experiment with r, g, b

>>> turtle.position[]
[0.00,0.00]
>>> turtle.forward[25]
>>> turtle.position[]
[25.00,0.00]
>>> turtle.forward[-75]
>>> turtle.position[]
[-50.00,0.00]
79

forest

3 cây theo chiều rộng

ngẫu nhiên hóa

fractalcurves

Hilbert & Koch curves

recursion

lindenmayer

ethnomathematics [indian kolams]

L-System

minimal_hanoi

Towers of Hanoi

Rectangular Turtles as Hanoi discs [shape, shapesize]

nim

play the classical nim game with three heaps of sticks against the computer

turtles as nimsticks, event driven [mouse, keyboard]

paint

super minimalistic drawing program

>>> turtle.position[]
[0.00,0.00]
>>> turtle.forward[25]
>>> turtle.position[]
[25.00,0.00]
>>> turtle.forward[-75]
>>> turtle.position[]
[-50.00,0.00]
77

hòa bình

elementary

turtle. appearance and animation

penrose

aperiodic tiling with kites and darts

>>> turtle.position[]
[0.00,0.00]
>>> turtle.forward[25]
>>> turtle.position[]
[25.00,0.00]
>>> turtle.forward[-75]
>>> turtle.position[]
[-50.00,0.00]
29

planet_and_moon

simulation of gravitational system

compound shapes,

>>> turtle.position[]
[0.00,0.00]
>>> turtle.backward[30]
>>> turtle.position[]
[-30.00,0.00]
31

round_dance

dancing turtles rotating pairwise in opposite direction

compound shapes, clone shapesize, tilt, get_shapepoly, update

sorting_animate

visual demonstration of different sorting methods

simple alignment, randomization

tree

a [graphical] breadth first tree [using generators]

>>> turtle.position[]
[0.00,0.00]
>>> turtle.forward[25]
>>> turtle.position[]
[25.00,0.00]
>>> turtle.forward[-75]
>>> turtle.position[]
[-50.00,0.00]
83

two_canvases

simple design

turtles on two canvases

wikipedia

a pattern from the wikipedia article on turtle graphics

>>> turtle.position[]
[0.00,0.00]
>>> turtle.forward[25]
>>> turtle.position[]
[25.00,0.00]
>>> turtle.forward[-75]
>>> turtle.position[]
[-50.00,0.00]
83,
>>> turtle.position[]
[0.00,0.00]
>>> turtle.forward[25]
>>> turtle.position[]
[25.00,0.00]
>>> turtle.forward[-75]
>>> turtle.position[]
[-50.00,0.00]
32

yinyang

another elementary example

>>> turtle.position[]
[0.00,0.00]
>>> turtle.forward[25]
>>> turtle.position[]
[25.00,0.00]
>>> turtle.forward[-75]
>>> turtle.position[]
[-50.00,0.00]
27

Have fun

Changes since Python 2. 6

  • The methods

    >>> turtle.heading[]
    22.0
    >>> turtle.left[45]
    >>> turtle.heading[]
    67.0
    
    03,
    >>> turtle.heading[]
    22.0
    >>> turtle.left[45]
    >>> turtle.heading[]
    67.0
    
    04 and
    >>> turtle.heading[]
    22.0
    >>> turtle.left[45]
    >>> turtle.heading[]
    67.0
    
    05 have been eliminated. Methods with these names and functionality are now available only as methods of
    >>> turtle.position[]
    [0.00,0.00]
    >>> turtle.forward[25]
    >>> turtle.position[]
    [25.00,0.00]
    >>> turtle.forward[-75]
    >>> turtle.position[]
    [-50.00,0.00]
    
    07. The functions derived from these remain available. [In fact already in Python 2. 6 these methods were merely duplications of the corresponding
    >>> turtle.heading[]
    90.0
    >>> turtle.position[]
    [0.00,-10.00]
    >>> turtle.home[]
    >>> turtle.position[]
    [0.00,0.00]
    >>> turtle.heading[]
    0.0
    
    5/
    >>> turtle.position[]
    [0.00,0.00]
    >>> turtle.forward[25]
    >>> turtle.position[]
    [25.00,0.00]
    >>> turtle.forward[-75]
    >>> turtle.position[]
    [-50.00,0.00]
    
    07-methods. ]

  • The method

    >>> turtle.heading[]
    22.0
    >>> turtle.left[45]
    >>> turtle.heading[]
    67.0
    
    09 has been eliminated. The behaviour of
    >>> turtle.position[]
    [0.00,0.00]
    >>> turtle.forward[25]
    >>> turtle.position[]
    [25.00,0.00]
    >>> turtle.forward[-75]
    >>> turtle.position[]
    [-50.00,0.00]
    
    57 and
    >>> turtle.position[]
    [0.00,0.00]
    >>> turtle.forward[25]
    >>> turtle.position[]
    [25.00,0.00]
    >>> turtle.forward[-75]
    >>> turtle.position[]
    [-50.00,0.00]
    
    58 have changed slightly. now every filling-process must be completed with an
    >>> turtle.position[]
    [0.00,0.00]
    >>> turtle.forward[25]
    >>> turtle.position[]
    [25.00,0.00]
    >>> turtle.forward[-75]
    >>> turtle.position[]
    [-50.00,0.00]
    
    58 call

  • A method

    >>> turtle.heading[]
    22.0
    >>> turtle.left[45]
    >>> turtle.heading[]
    67.0
    
    13 has been added. It returns a boolean value.
    >>> turtle.position[]
    [0.00,0.00]
    >>> turtle.backward[30]
    >>> turtle.position[]
    [-30.00,0.00]
    
    46 if a filling process is under way,
    >>> turtle.position[]
    [0.00,0.00]
    >>> turtle.backward[30]
    >>> turtle.position[]
    [-30.00,0.00]
    
    47 otherwise. This behaviour corresponds to a
    >>> turtle.heading[]
    22.0
    >>> turtle.left[45]
    >>> turtle.heading[]
    67.0
    
    16 call without arguments in Python 2. 6

Changes since Python 3. 0

  • The methods

    >>> turtle.heading[]
    22.0
    >>> turtle.left[45]
    >>> turtle.heading[]
    67.0
    
    17,
    >>> turtle.heading[]
    22.0
    >>> turtle.left[45]
    >>> turtle.heading[]
    67.0
    
    18 and
    >>> turtle.heading[]
    22.0
    >>> turtle.left[45]
    >>> turtle.heading[]
    67.0
    
    19 have been added. Thus the full range of regular linear transforms is now available for transforming turtle shapes.
    >>> turtle.heading[]
    22.0
    >>> turtle.left[45]
    >>> turtle.heading[]
    67.0
    
    20 has been enhanced in functionality. it now can be used to get or set the tiltangle.
    >>> turtle.heading[]
    22.0
    >>> turtle.left[45]
    >>> turtle.heading[]
    67.0
    
    21 has been deprecated

  • The method

    >>> turtle.heading[]
    22.0
    >>> turtle.left[45]
    >>> turtle.heading[]
    67.0
    
    22 has been added as a complement to
    >>> turtle.heading[]
    22.0
    >>> turtle.left[45]
    >>> turtle.heading[]
    67.0
    
    23 which in fact binds actions to the keyrelease event. Accordingly the latter has got an alias.
    >>> turtle.heading[]
    22.0
    >>> turtle.left[45]
    >>> turtle.heading[]
    67.0
    
    24

  • The method

    >>> turtle.heading[]
    22.0
    >>> turtle.left[45]
    >>> turtle.heading[]
    67.0
    
    25 has been added. So when working only with Screen and Turtle objects one must not additionally import
    >>> turtle.position[]
    [0.00,0.00]
    >>> turtle.backward[30]
    >>> turtle.position[]
    [-30.00,0.00]
    
    05 anymore

  • Two input methods has been added

    >>> turtle.heading[]
    22.0
    >>> turtle.left[45]
    >>> turtle.heading[]
    67.0
    
    27 and
    >>> turtle.heading[]
    22.0
    >>> turtle.left[45]
    >>> turtle.heading[]
    67.0
    
    28. These popup input dialogs and return strings and numbers respectively

    Can two turtles move at the same time in Python?

    Unfortunately, python turtle works by doing a step by step so there is no way for you to move 2 turtles at a time . Bạn cũng không thể sử dụng luồng để làm điều này.

    Can you loop a loop in Python?

    Nested for Loops in Python You can put a for loop inside a while, or a while inside a for, or a for inside a for, or a while inside a while. Or you can put a loop inside a loop inside a loop . You can go as far as you want. Hãy xem xét một số vòng lặp lồng nhau để in cùng một mẫu.

Chủ Đề