반응형
    
    
    
  다음 코드는 터틀 그래픽으로 사각형을 그리는 코드입니다.
터틀 그래픽을 위한 간단한 함수는 아래의 링크를 참고해주십시오.
https://coding-abc.tistory.com/187
import turtle as t
t.shape('turtle')
for i in range(4):
    t.forward(200)
    t.right(90)
위 코드를 수정해서 사각형을 그리는 모듈을 만들었습니다.
# rectangle_module.py
import turtle
def draw_rectangle(x, y, width, height, color="black"):
    """
    Draws a rectangle using the turtle module.
    
    Parameters:
    - x, y: Top-left corner of the rectangle.
    - width: Width of the rectangle.
    - height: Height of the rectangle.
    - color: Fill color of the rectangle (default is "black").
    """
    turtle.penup()
    turtle.goto(x, y)
    turtle.pendown()
    turtle.fillcolor(color)
    turtle.begin_fill()
    for _ in range(2):
        turtle.forward(width)
        turtle.right(90)
        turtle.forward(height)
        turtle.right(90)
    turtle.end_fill()
draw_rectangle(-100,0, 100, 100)
draw_rectangle(10,0, 100, 200, "red")
'Python > 파이썬 기초 강의(2024)★' 카테고리의 다른 글
| 파이썬, 표준 모듈 목록, 모듈 내의 함수 목록 알아보기 (0) | 2024.11.26 | 
|---|---|
| 파이썬, 클래스 생성자 오보로딩 상속 class __init__ self (0) | 2024.11.25 | 
| 파이썬: 모듈 사용하기 (0) | 2024.11.23 | 
| 파이썬, 지역변수 전역변수 Local & Global Variable, global (1) | 2024.11.22 | 
| 파이썬: 사용자 정의 함수 (3) | 2024.11.20 |