본문 바로가기
프로그램

[파이썬] 문제 : 터틀(turtle) 30개의 임의 색과 크기의 원 그리기

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

import turtle
import random

s_w = 600
s_h = 400
turtle.setup (width=s_w, height=s_h)
turtle.colormode(255)       # 색을 RGB로 하도록 지정
t=turtle.Turtle()           # 터틀
# t.shape('classic')
t.hideturtle()
t.speed(0)                  # 속도 빠르게

def draw(x, y, rad):        # 그리는 함수. x,y 좌표. rad:반지름. co:칼라
    t.penup()               # 펜 들기
    t.goto(x, y)            # 이동
    t.pendown()             # 펜 내리기
    r = random.randint(0,255)    # 색을 0~255의 값을 임의의로 지정하도록 함
    g = random.randint(0,255)
    b = random.randint(0,255)
    t.begin_fill()
    t.color(r,g,b)           # 색 지정
    # t.width(20)              # 선 두께
    t.circle(rad)            # 원 그리기
    t.end_fill()

    
for i in range(30):              # 30개의 원을 그리기 위한 반복
    w = (s_w / 2)                # x 좌표의 범위를 정해주기 위해 화면의 width 값의 절반을 구함
    h = (s_h / 2)                # y 좌표의 범위를 정해주기 위해 화면의 height 값의 절반을 구함
    x = random.randint(-w, w)    # x 좌표를 임의의 값으로 구함. 범위는 가눙데가 0 이기 때문에 -w에서 +w로 함
    y = random.randint(-h, h)
    radius = random.randint(3, 100)
    draw(x, y, radius)             # 원 그리기 함수

turtle.done()

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

728x90
반응형

댓글