> For the complete documentation index, see [llms.txt](https://krootee.gitbook.io/python-turtle/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://krootee.gitbook.io/python-turtle/1.example01-10/10.-1.md).

# 10.ต้นไม้สามต้น

{% code lineNumbers="true" fullWidth="true" %}

```python
from turtle import *  # นำเข้าทุกฟังก์ชันจากโมดูล turtle
def tree(x, y, s, c):  # กำหนดฟังก์ชัน tree ที่รับพารามิเตอร์ x, y, s, และ c
    color('brown')  # ตั้งค่าสีของปากกาเป็นสีน้ำตาลสำหรับลำต้น

    begin_fill()  # เริ่มการเติมสี

    pu()  # ยกปากกาเพื่อไม่ให้วาดเส้น
    goto(x, y)  # ย้ายตำแหน่งกระดองเต่าไปยัง (x, y)
    pd()  # วางปากกาเพื่อเริ่มวาด

    for k in range(2):  # ลูป 2 ครั้งเพื่อวาดรูปสี่เหลี่ยมสำหรับลำต้น
        fd(30)  # เคลื่อนที่ไปข้างหน้า 30 หน่วย
        lt(90)  # หมุนไปทางซ้าย 90 องศา
        fd(100)  # เคลื่อนที่ไปข้างหน้า 100 หน่วย
        lt(90)  # หมุนไปทางซ้าย 90 องศา

    end_fill()  # สิ้นสุดการเติมสี

    for j in range(s):  # ลูปตามค่าที่กำหนดในพารามิเตอร์ s เพื่อวาดใบไม้
        pu()  # ยกปากกาเพื่อไม่ให้วาดเส้น
        goto(x - 85, y + 100 + j * 50)  # ย้ายตำแหน่งกระดองเต่าไปยังตำแหน่งที่กำหนดสำหรับแต่ละชั้นของใบไม้
        pd()  # วางปากกาเพื่อเริ่มวาด
        color(c)  # ตั้งค่าสีของปากกาเป็นสีที่ระบุในพารามิเตอร์ c
        begin_fill()  # เริ่มการเติมสี

        for i in range(3):  # ลูป 3 ครั้งเพื่อวาดรูปสามเหลี่ยมสำหรับใบไม้
            fd(200)  # เคลื่อนที่ไปข้างหน้า 200 หน่วย
            lt(120)  # หมุนไปทางซ้าย 120 องศา

        end_fill()  # สิ้นสุดการเติมสี

tree(-200, -100, 4, 'green')  # เรียกใช้ฟังก์ชัน tree เพื่อวาดต้นไม้ที่ตำแหน่ง (-200, -100) มี 4 ชั้นใบไม้ สีเขียว
tree(0, -100, 2, 'green')  # เรียกใช้ฟังก์ชัน tree เพื่อวาดต้นไม้ที่ตำแหน่ง (0, -100) มี 2 ชั้นใบไม้ สีเขียว
tree(200, -100, 3, 'green')  # เรียกใช้ฟังก์ชัน tree เพื่อวาดต้นไม้ที่ตำแหน่ง (200, -100) มี 3 ชั้นใบไม้ สีเขียว

mainloop()  # ทำให้หน้าจอยังคงเปิดอยู่จนกว่าผู้ใช้จะปิดมัน

```

{% endcode %}

<figure><img src="/files/n6fxBpt5jCxJUzvQdAn0" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://krootee.gitbook.io/python-turtle/1.example01-10/10.-1.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
