파이썬 터틀 그래픽스
import turtle as t
t.shape('turtle')
t.mainloop()
import turtle as t
t.shape('turtle')
for i in range(4) :
t.forward(100) # 앞으로 100픽셀 만큼 이동
t.right(90) # 오른쪽으로 90도 회전
t.mainloop()
공식문서 예제
from turtle import *
color('red', 'yellow')
begin_fill()
while True:
forward(200)
left(170)
if abs(pos()) < 1:
break
end_fill()
done()
다양한 색으로 원 그리기
import turtle as t
# 전역변수 - 그림판 크기
swidth, sheight = 500, 500
# 윈도우창 설정
t.title('무지개색 원그리기')
t.shape('classic') # turtle, classic, triangle
t.setup(width = swidth + 50, height = sheight + 50)
t.screensize(swidth, sheight) # 500, 500 픽셀
# 거북이 이동
t.penup() # 선 그리지 않기
t.goto(0, -sheight / 2) # 세로로 1/4 지점으로 이동
t.pendown() # 선 그릴 준비
t.speed(10) # 빠르기 1 ~ 10
# 색, 원 반지름 지정
for radius in range(1, 250) :
if radius % 7 == 1 :
t.pencolor('red')
elif radius % 7 == 2 :
t.pencolor('orange')
elif radius % 7 == 3 :
t.pencolor('yellow')
elif radius % 7 == 4 :
t.pencolor('green')
elif radius % 7 == 5 :
t.pencolor('blue')
elif radius % 7 == 6 :
t.pencolor('navyblue')
else :
t.pencolor('purple')
t.circle(radius)
t.done()
마우스 클릭시 선 그리기
import sys
import turtle
def draw(x, y) : # 이동할 좌표
t.goto(x, y)
def windows_quit() : # 창 종료
sys.exit(0)
t = turtle.Turtle()
t.shape("turtle")
t.pensize(10) # 선 두께
s = turtle.Screen()
s.onscreenclick(draw) # 클릭하면 좌표 출력
s.onkey(t.penup, "Up") # 그림 안그리기
s.onkey(t.pendown, "Down") # 그림 그리기
s.onkey(windows_quit, "q") # 창 종료
s.listen() # 입력 키에 반응하기
turtle.done()