터틀 그래픽은 여러가지를 할 수 있도록 제공하고 있는데 그중에 텍스트를 출력할 수있다. 텍스트는 한글, 영어 등 다양하게 가능하지만 거기에 맞는 폰트가 필요하다. 우리가 쓰는 한글 윈도우 또는 맥에서는 기본 폰트를 제공하고 있고 별도의 폰트를 설치 할 경우에만 그 폰트를 사용할 수 있다.
Table of Contents
터틀 그래픽에서 텍스트 출력하기
형식 : turtle.write(“massage”, move=False, align=”center”, font=(“굴림”,20,”bold”))
명령어 설명 :
write() : 문자열을 터틀 그래픽에 출력하는 명령어
– “massage” : ” ” 안에 텍스트를 쓰면 그대로 출력을 함.
– move : turtle의 움직임이 필요할 경우 True, 필요없을 경우는 False
– align = 텍스트의 정렬 기능 “left”, “center”, “right”
– font : 폰트에 대한 정보 입력(“폰트이름”, 폰트 사이즈, 폰트타입(normal, bold)
move, align, font는 생략이 가능하다.
import turtle
turtle.hideturtle() # 터틀모양을 숨김
turtle.write("기본은 역시 굴림체", False, "center", ("굴림",20,"bold"))
텍스트 출력해보기
import turtle
turtle.hideturtle() # 터틀의 모양을 숨김
# move, align, font를 생략하여 작성 할 경우, 순서대로 값을 넣어야 한다.
turtle.write("기본은 역시 굴림체", False, "center", ("굴림체",20,"bold"))
turtle.mainloop()
![[파이썬] 터틀그래픽에서 텍스트 출력하기 2 텍스트 출력하기](https://www.shindeacon.co.kr/wp-content/uploads/2023/10/스크린샷-2023-10-10-오전-9.54.28-1021x1024-optimized.png)
여러줄의 텍스트를 작성 하려면?
goto() 함수를 사용하여 원하는 자표로 이동하여 작성 하면 된다.
import turtle
turtle.hideturtle() # 터틀의 모양을 숨김
# turtle.write("텍스트", move=False, align="center", font=("굴림",20,"bold"))
turtle.write("기본은 역시 굴림체", False, "center", ("굴림체",20,"bold"))
turtle.penup()
turtle.goto(0, -50) #(x, y)
turtle.pendown()
turtle.write("영어는 역시 Arial", False, "center", ("Arial",30,"bold"))
turtle.penup()
turtle.goto(0, -100)
turtle.pendown()
turtle.write("진지할 때는 궁서체", False, "right", ("궁서체",30,"bold"))
turtle.penup()
turtle.goto(0, -150)
turtle.pendown()
turtle.write("이것이 던파 비트비트", False, "center", ("던파 비트비트체 TTF",40,"normal"))
# 던파 비트비트체는 던파 홈페이지에서 다운받아 설치해야 사용 가능하다.
turtle.mainloop()
![[파이썬] 터틀그래픽에서 텍스트 출력하기 3 텍스트 출력](https://www.shindeacon.co.kr/wp-content/uploads/2023/10/텍스트-출력-optimized.png)
폰트 추가하기
내가 원하는 폰트를 찾고자 할 때는 아래 사이트를 통해서 하면 조금 쉽게 찾을 수 있다. 아래 사이트는 폰트를 정보를 모아둔 사이트로 폰트는 각각의 제작자 홈페이지로 이동하여 받을 수 있다.
import turtle
def draw_head(t):
# 머리 그리기
t.penup()
t.goto(0, 200)
t.pendown()
t.begin_fill()
t.fillcolor(“#4C4C4C”)
t.circle(50)
t.end_fill()
# 앞머리 그리기
t.penup()
t.goto(-20, 210)
t.pendown()
t.begin_fill()
t.fillcolor(“#4C4C4C”)
t.goto(-30, 230)
t.goto(30, 230)
t.goto(20, 210)
t.end_fill()
# 머리장식 그리기
t.penup()
t.goto(0, 230)
t.pendown()
t.begin_fill()
t.fillcolor(“#D4AF37”)
for _ in range(5):
t.forward(20)
t.right(144)
t.end_fill()
# 머리카락 그리기
t.penup()
t.goto(-25, 200)
t.pendown()
t.color(“#4C4C4C”)
t.width(2)
for _ in range(5):
t.left(90)
t.circle(-25, 45)
t.circle(-10, 90)
t.circle(-5, 90)
# 머리장식의 세부 디테일 그리기
t.penup()
t.goto(0, 230)
t.pendown()
t.color(“#D4AF37”)
t.width(3)
t.goto(0, 250)
t.penup()
t.goto(-15, 230)
t.pendown()
t.goto(-15, 250)
t.penup()
t.goto(15, 230)
t.pendown()
t.goto(15, 250)
# 눈 그리기
t.penup()
t.goto(-15, 230)
t.pendown()
t.begin_fill()
t.fillcolor(“#FFFFFF”)
t.circle(10)
t.end_fill()
t.penup()
t.goto(15, 230)
t.pendown()
t.begin_fill()
t.fillcolor(“#FFFFFF”)
t.circle(10)
t.end_fill()
# 눈동자 그리기
t.penup()
t.goto(-15, 235)
t.pendown()
t.begin_fill()
t.fillcolor(“#000000”)
t.circle(5)
t.end_fill()
t.penup()
t.goto(15, 235)
t.pendown()
t.begin_fill()
t.fillcolor(“#000000”)
t.circle(5)
t.end_fill()
# 코 그리기
t.penup()
t.goto(0, 220)
t.pendown()
t.width(2)
t.setheading(-90)
t.circle(10, 180)
# 입 그리기
t.penup()
t.goto(-15, 215)
t.pendown()
t.width(3)
t.setheading(0)
t.circle(15, 180)
# 귀 그리기
t.penup()
t.goto(-50, 200)
t.pendown()
t.begin_fill()
t.fillcolor(“#4C4C4C”)
for _ in range(2):
t.circle(20, 90)
t.circle(10, 90)
t.end_fill()
t.penup()
t.goto(50, 200)
t.pendown()
t.begin_fill()
t.fillcolor(“#4C4C4C”)
for _ in range(2):
t.circle(-20, 90)
t.circle(-10, 90)
t.end_fill()
def draw_body(t):
# 몸통 그리기
t.penup()
t.goto(-70, 0)
t.pendown()
t.begin_fill()
t.fillcolor(“#A52A2A”)
for _ in range(2):
t.forward(140)
t.left(90)
t.forward(180)
t.left(90)
t.end_fill()
# 몸통 장식 그리기
t.penup()
t.goto(-70, 0)
t.pendown()
t.begin_fill()
t.fillcolor(“#4C4C4C”)
t.goto(-90, 60)
t.goto(-50, 60)
t.goto(-70, 0)
t.end_fill()
# 갑옷 그리기
t.penup()
t.goto(-90, 60)
t.pendown()
t.begin_fill()
t.fillcolor(“#D4AF37”)
for _ in range(2):
t.forward(40)
t.left(90)
t.forward(30)
t.left(90)
t.end_fill()
# 갑옷의 세부 디테일 그리기
t.penup()
t.goto(-85, 70)
t.pendown()
t.color(“#4C4C4C”)
t.width(2)
t.goto(-55, 70)
def draw_legs(t):
# 다리 그리기
t.penup()
t.goto(-70, -180)
t.pendown()
t.begin_fill()
t.fillcolor(“#A52A2A”)
for _ in range(2):
t.forward(40)
t.left(90)
t.forward(80)
t.left(90)
t.end_fill()
# 다른 다리 그리기
t.penup()
t.goto(30, -180)
t.pendown()
t.begin_fill()
t.fillcolor(“#A52A2A”)
for _ in range(2):
t.forward(40)
t.left(90)
t.forward(80)
t.left(90)
t.end_fill()
# 신발 그리기
t.penup()
t.goto(-90, -180)
t.pendown()
t.begin_fill()
t.fillcolor(“#4C4C4C”)
for _ in range(2):
t.forward(40)
t.left(90)
t.forward(20)
t.left(90)
t.end_fill()
# 다른 신발 그리기
t.penup()
t.goto(-30, -180)
t.pendown()
t.begin_fill()
t.fillcolor(“#4C4C4C4C”)
t.end_fill()
for _ in range(2):
t.forward(40)
t.left(90)
t.forward(20)
t.left(90)
t.end_fill()
def draw_arms(t):
# 팔 그리기
t.penup()
t.goto(-70, 100)
t.pendown()
t.begin_fill()
t.fillcolor(“#A52A2A”)
for _ in range(2):
t.forward(100)
t.left(90)
t.forward(30)
t.left(90)
t.end_fill()
# 다른 팔 그리기
t.penup()
t.goto(-30, 100)
t.pendown()
t.begin_fill()
t.fillcolor(“#A52A2A”)
for _ in range(2):
t.forward(100)
t.left(90)
t.forward(30)
t.left(90)
t.end_fill()
# 손 그리기
t.penup()
t.goto(-70, 100)
t.pendown()
t.begin_fill()
t.fillcolor(“#4C4C4C”)
t.circle(15)
t.end_fill()
# 다른 손 그리기
t.penup()
t.goto(30, 100)
t.pendown()
t.begin_fill()
t.fillcolor(“#4C4C4C”)
t.circle(15)
t.end_fill()
# 무기 그리기
t.penup()
t.goto(-70, 70)
t.pendown()
t.color(“#4C4C4C”)
t.width(5)
t.goto(-30, 130)
# 무기 뾰족한 부분 그리기
t.width(3)
t.goto(-45, 100)
t.goto(-30, 130)
# 무기의 세부 디테일 그리기
t.penup()
t.goto(-65, 80)
t.pendown()
t.goto(-55, 100)
t.penup()
t.goto(-55, 100)
t.pendown()
t.goto(-30, 90)
# 터틀 설정
t = turtle.Turtle()
t.speed(3)
# 각 부분 그리기
draw_head(t)
draw_body(t)
draw_legs(t)
draw_arms(t)
# 그림 그리기 끝내기
t.hideturtle()
turtle.done()