프로그램
[파이썬] 문제 : turtle 색상,크기,좌표가 모두 난수로 별 그리기
오디세이99
2023. 11. 21. 06:35
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) # 색 지정
size = random.randint(10, 100) # 한 변의 크기 10~200 으로
while True:
t.forward(size)
t.left(144) # 144도
if abs(t.distance(x, y)) < 1:
break
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
반응형