본문 바로가기
프로그램

[파이썬] 문제 : turtle-임의의 위치에 원,삼각형,사각형 등 그리기

by 오디세이99 2023. 12. 5.
728x90
반응형

"무작위로 이동하여 임의의 점에서 삼각형 또는 사각형 또는 오각형 또는 육각형 또는 원"

from turtle import *
from random import *

x, y, radius = 0,0,0
colorList = ['red','yellow','green','orange','blue','violet','tan','brown','navy','cyan']
shapeList = [0, 3, 4, 5, 6]       # 도형.

setup(1200, 800)
bgcolor('black')
speed(0)

def draw_circle(r):
    circle(radius)
    
def draw_shape(n, length):
    print(n, length)
    for i in range(n):
        forward(length)
        right(int(360/n))
    

for i in range(30):
    x = randint(-500, 500)
    y = randint(-400, 300)
    radius = randint(80, 130)
    length = randint(20, 200)          # 도형 변의 길이
    penup()
    goto(x, y)
    penup()
    color(choice(colorList))
    shape = choice(shapeList)           # 도형 선탹 
    begin_fill()
    if shape == 0:                      # 0이면 원
        draw_circle(radius)
    else:
        draw_shape(shape, length)       # 3,4,5,6 각형 및 한변 길이
    end_fill()
    

done()

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

728x90
반응형

댓글