본문 바로가기
프로그램

[파이썬] 문제 : 터틀 색 채우기 방법

by 오디세이99 2022. 12. 13.
728x90
반응형

색 채우기

 

1) 리스트에서 순서대로 색 선택

import turtle
t=turtle.Turtle()

color_list=['red', 'blue', 'gold', 'silver', 'pink', 'skyblue']

for count in range(6):
    t.begin_fill()                     # 채우기 시작
    t.fillcolor(color_list[count])     # 색 리스트에서 인덱스로 순서대로 선택
    t.circle(100)                      # 원 그리기
    t.end_fill()                       # 채우기 끝
    t.left(360/6)

turtle.done()

 

 

2) 색 리스트에서 임의의 색 선택

import turtle
import random

t=turtle.Turtle()

color_list=['red', 'blue', 'gold', 'silver', 'pink', 'skyblue']

for count in range(6):
    t.begin_fill()
    t.fillcolor(color_list[random.randint(0,5)])    # 색 리스트에서 임의의 인덱스 번호로 선택
    t.circle(100)
    t.end_fill()    
    t.left(360/6)

turtle.done()

 

3) RGB 색으로 임의의 색 지정

import turtle
import random

t=turtle.Turtle()

color_list=['red', 'blue', 'gold', 'silver', 'pink', 'skyblue']

for count in range(6):
    t.begin_fill()
    r = random.random()            # R 색의 번호를 임의로 선택
    g = random.random()
    b = random.random()
    t.fillcolor((r, g, b))         # RGB 컬러를 임의로 지정
    t.circle(100)
    t.end_fill()    
    t.left(360/6)

turtle.done()

728x90
반응형

댓글