프로그램

[파이썬] 문제 : 터틀(turtle) 3x4 개의 가로,세로 원들을 그릴때 가장자리 원들의 색만 바꾸기

오디세이99 2023. 5. 18. 00:36
728x90
반응형

(문제)

가로 3개, 세로 4개의 원을 그릴때
가장자리에 있는 원들만 색깔을 바꾸기

 

(방법)

import turtle
import random

# turtle.colormode(255)       # 색을 RGB로 하도록 지정
# turtle.bgcolor('blue')
t = turtle.Turtle()           # 터틀
t.shape('classic')
t.hideturtle()
t.speed(0)                  # 속도 빠르게

def draw(x, y, rad, color):    # 그리는 함수. x,y 좌표. rad:원의 반지름, 색
    t.penup()
    t.goto(x, y)
    t.pendown()
    t.pencolor('black')
    t.fillcolor(color)
    t.begin_fill()
    t.circle(rad)
    t.end_fill()

    
radius = 20                       # 원 반지름 지정
x_init = -100                     # x좌표. 줄바꿀때마다 초기 좌표로 가야하기 때문에 사용
x = x_init
y = 100
for i in range(4):                # 세로
    for j in range(3):            # 가로
        color = 'white'            # 기본색 지정
        if i == 0 or i == 3 or j == 0 or j == 2:   # 가장자리면
            color = 'blue'         # 파란색
        draw(x, y, radius, color)  # 그리기 함수
        x += 50                    # x 조표 +50 씩 이동
    y -= 50                        # y 좌표 -50 씩 이동
    x  = x_init                    # 줄 바꾸면 x 좌표 초기화

turtle.done()

try:
    turtle.bye()
except:
    print("bye")

728x90
반응형