파이썬의 터틀 그래픽은 다양한 기능을 이용가능하다. 그 중에 그림을 그려주는 기능은 파이썬 입문자에게는 쉽게 접하면서 재미있게 배울 수 있는 기능 중에 하나이다. 그림을 그리기 위해서는 도형의 각도 및 길이를 계산하여 그려야 하는 조금 복잡한 과정이 포함되어진다. 피카츄 얼굴 그리기 또한 누군가의 계산으로 그려진 그림 중 하나이다.
터틀 그래픽으로 피카츄 얼굴 그리기
함수를 정의하여 각각의 얼굴과 눈 귀 볼 등 각각의 내용을 정의하여 그림을 그린다.
# 여기서 배워야 할 기능
penup(), pendown(), circle(), goto()
# 피카츄 얼굴 그리기 파이썬 코드
import turtle as t
t.pensize(4)
t.speed(10)
#t.tracer(0)
body_color = "#f8d849"
cheek_color = "#d85d2d"
t.color("black", body_color)
# 얼굴 그리기
def drawface():
t.penup()
t.goto(-50, -120) # 왼쪽 턱 시작위치
t.pendown()
t.begin_fill()
t.seth(160)
t.circle(-160, 30) # 얼굴 왼쪽 그리기
t.right(10)
t.circle(-80, 60)
t.circle(90, 30)
t.circle(-60, 70)
t.circle(-200, 40) # 정수리
t.circle(-60, 70)
t.circle(90, 30)
t.circle(-80, 60)
t.right(10)
t.circle(-160, 30) # 얼굴 왼쪽 그리기
t.goto(-50, -120) # 왼쪽 턱 시작위치
t.end_fill()
# 귀 그리기 d : 왼쪽 -1, 오른쪽 1
def drawear(d):
t.color("black", body_color)
t.penup()
t.goto(85*d, 90) # 왼쪽 오른쪽 x좌표 대칭 왼-85, 오85
t.pendown()
t.begin_fill()
t.seth(90+60*(-d)) # 왼150, 오30
t.circle(240*d, 50)
x,y = t.pos() # 귀 맨위 꼭지점
t.right(180+60*d) # 왼120, 오240
t.circle(240*d, 50)
t.end_fill()
# 귀 까만색 칠하기
t.color("black", "black")
t.penup()
t.goto(x,y) # 맨 위 꼭지점
t.pendown()
t.begin_fill()
t.goto(t.xcor()+31*(-d), t.ycor()-83) # 꼭지점
t.goto(t.xcor()+30*(-d), t.ycor()+50) # 꼭지점
t.goto(x,y) # 맨 위 꼭지점
t.end_fill()
# 눈 그리기 d : 왼쪽 -1, 오른쪽 1
def draweye(d):
t.penup()
t.goto(60*d, 30)
t.dot(40, "black") # 검정색 눈
t.penup()
t.goto(50*d, 30)
t.dot(15, "white") # 흰색 눈
# 코 그리기
def drawnose():
t.color("black", "black")
t.penup()
t.goto(-5, -10)
t.pendown()
t.begin_fill()
t.seth(270)
t.circle(5, 180) # 코 반원 그리기
t.goto(-5, -10)
t.end_fill()
# 입 그리기
def drawmouth():
t.penup()
t.goto(-40, -30)
t.pendown()
t.seth(315)
t.circle(30, 90)
t.right(90)
t.circle(30, 90)
# 빨간 볼 그리기 d : 왼쪽 -1, 오른쪽 1
def drawcheek(d):
t.penup()
t.goto(90*d, -40)
t.dot(60, cheek_color)
drawface() # 얼굴 그리기
drawear(-1) # 왼쪽 귀
drawear(1) # 오른쪽 귀
draweye(-1) # 왼쪽 눈
draweye(1) # 오른쪽 눈
drawnose() # 코
drawmouth() # 입
drawcheek(-1) # 왼쪽 볼
drawcheek(1) # 오른쪽 볼
t.hideturtle()
t.update()
t.mainloop()
