# 02.วาดรูปสี่่เหลี่ยม

> วาดรูปสี่เหลี่ยมจัตุรัสโดยการให้เต่าเคลื่อนที่ไปข้างหน้าและหมุนไปทางซ้าย 90 องศาซ้ำๆ จนครบรอบกลับมาที่จุดเริ่มต้น

<figure><img src="https://1059722861-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWAMRQLhtfzfy8yv80Se5%2Fuploads%2FcXC2eu2p7shHZQNLXcf0%2Fexam02.png?alt=media&#x26;token=596c3c64-cfa3-4e24-a1f9-4e9fda1766e1" alt=""><figcaption></figcaption></figure>

{% code title="แบบที่ 1 ฝึกสร้างตัวแปร " overflow="wrap" lineNumbers="true" fullWidth="true" %}

```python
import turtle               # นำเข้าโมดูล turtle เพื่อใช้วาดภาพ
t = turtle.Turtle()         # สร้างออบเจ็กต์ Turtle
t.shape('turtle')           # ตั้งค่าให้รูปร่างของ turtle เป็นเต่า

t.forward(100)              # เคลื่อนไปข้างหน้า 100 หน่วย
t.left(90)                  # หมุนไปทางซ้าย 90 องศา
t.forward(100)              # เคลื่อนไปข้างหน้า 100 หน่วย
t.left(90)                  # หมุนไปทางซ้าย 90 องศา
t.forward(100)              # เคลื่อนไปข้างหน้า 100 หน่วย
t.left(90)                  # หมุนไปทางซ้าย 90 องศา
t.forward(100)              # เคลื่อนไปข้างหน้า 100 หน่วย
t.left(90)                  # หมุนไปทางซ้าย 90 องศา เพื่อกลับมาที่ตำแหน่งเริ่มต้น

turtle.done()               # สิ้นสุดการวาดภาพ
```

{% endcode %}

{% code title="แบบที่ 2 วน loop" overflow="wrap" lineNumbers="true" fullWidth="true" %}

```python
// Some code
import turtle               # นำเข้าโมดูล turtle สำหรับใช้วาดกราฟิก
t = turtle.Turtle()         # สร้างวัตถุ Turtle เพื่อใช้เป็นตัววาด
t.shape('turtle')           # กำหนดให้ตัววาดมีรูปร่างเป็นเต่า

for i in range(4):          # วนซ้ำ 4 ครั้งเพื่อวาดรูปสี่เหลี่ยม
    t.forward(100)          # เดินหน้าไป 100 หน่วย
    t.left(90)              # หมุนซ้าย 90 องศา

turtle.done()               # สิ้นสุดการทำงานของหน้าต่างกราฟิก
```

{% endcode %}

{% code title="แบบที่ 3 ฝึกสร้างตัวแปร " overflow="wrap" lineNumbers="true" fullWidth="true" %}

```python
import turtle              # นำเข้าโมดูล turtle

screen = turtle.Screen()  # สร้างหน้าจอสำหรับวาด
screen.bgcolor("white")   # ตั้งค่าสีพื้นหลังเป็นสีขาว

pen = turtle.Turtle()     # สร้างวัตถุเต่า
pen.color("black")        # ตั้งค่าสีของปากกาเป็นสีดำ
pen.pensize(3)            # ตั้งค่าความหนาของปากกาเป็น 3 หน่วย

for _ in range(4):        # วนลูป 4 ครั้งสำหรับการวาดสี่เหลี่ยมจัตุรัส
    pen.forward(100)      # เดินหน้า 100 หน่วย
    pen.left(90)          # หมุนซ้าย 90 องศา

pen.hideturtle()          # ซ่อนเต่าไม่ให้แสดงบนหน้าจอ
turtle.done()             # จบการวาดและค้างหน้าจอไว้
```

{% endcode %}


---

# Agent Instructions: 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/02..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.
