본문 바로가기
프로그램

[파이썬] 터틀. 육각형 도형 원형으로 배열하기

by 오디세이99 2022. 10. 27.
728x90
반응형

 

# 클릭 위치에 6각형, 원형으로 그리기
import turtle as t
import random

t.shape('turtle')
t.speed(4)
color=['blue','green','pink','yellow','red','white']
length=50
n = 6                    # int(input())

def draw(x, y):
    t.penup()
    t.goto(x,y)
    t.pendown()
    
    t.color(color[0])
    pos1 = []
    t.begin_fill()          # 색칠할 영역 시작
    t.pencolor('black')
    for i in range(n):      # 6각형 그리기
        pos1.append([t.xcor(), t.ycor()])
        t.forward(length)
        t.right(360 / n)    # 360을 n으로 나누어서 외각을 구함
    t.end_fill()            # 색칠할 영역 끝    

    t.up()
    t.goto(pos1[2][0],pos1[2][1])
    t.down()
    t.color(color[1])

    pos2 = []
    t.begin_fill()          # 색칠할 영역 시작
    t.pencolor('black')
    for i in range(n):      # 6각형 그리기
        pos2.append([t.xcor(), t.ycor()])
        t.forward(length)
        t.right(360 / n)    # 360을 n으로 나누어서 외각을 구함
    t.end_fill()            # 색칠할 영역 끝

    t.up()
    t.goto(pos2[4][0],pos2[4][1])
    t.down()
    t.color(color[2])

    pos3 = []
    t.begin_fill()          # 색칠할 영역 시작
    t.pencolor('black')
    for i in range(n):      # 6각형 그리기
        pos3.append([t.xcor(), t.ycor()])
        t.forward(length)
        t.right(360 / n)    # 360을 n으로 나누어서 외각을 구함
    t.end_fill()            # 색칠할 영역 끝

    t.up()
    t.goto(pos3[5][0]-length,pos3[5][1])
    t.down()
    t.color(color[3])

    # pos = []
    t.begin_fill()          # 색칠할 영역 시작
    t.pencolor('black')
    for i in range(n):      # 6각형 그리기
        # pos.append([t.xcor(), t.ycor()])
        t.forward(length)
        t.right(360 / n)    # 360을 n으로 나누어서 외각을 구함
    t.end_fill()            # 색칠할 영역 끝

    # 5번째
    t.up()
    t.goto(pos3[0][0]-(pos3[2][0] - pos3[5][0])-length,pos3[0][1])
    t.down()
    t.color(color[4])

    # pos = []
    t.begin_fill()          # 색칠할 영역 시작
    t.pencolor('black')
    for i in range(n):      # 6각형 그리기
        # pos.append([t.xcor(), t.ycor()])
        t.forward(length)
        t.right(360 / n)    # 360을 n으로 나누어서 외각을 구함
    t.end_fill()            # 색칠할 영역 끝

    # 6번째
    t.up()
    t.goto(pos1[5][0]-length, pos1[2][1])
    t.down()
    t.color(color[5])

    # pos = []
    t.begin_fill()          # 색칠할 영역 시작
    t.pencolor('black')
    for i in range(n):      # 6각형 그리기
        # pos.append([t.xcor(), t.ycor()])
        t.forward(length)
        t.right(360 / n)    # 360을 n으로 나누어서 외각을 구함
    t.end_fill()            # 색칠할 영역 끝

t.onscreenclick(draw)

t.listen()

t.done()

728x90
반응형

댓글