문제
문제
성벽을 둘러쌓고 'ㅊ' 충장군이 가운데서 성을 지키는 모습을 그린다. 반복문과 상대적 위치, 이동을 배우게 된다. 'ㅊ'을 좌우상하로 미러링하고, 성벽으로 둘러쌓는다.
해결
'ㅊ'은 사람모양으로 그리면 편리하다. 머리가 팔다리와 연결되는 위치를 돌아갈 수 있도록 저장한다. 간단한 수학식을 사용할 수 있지만 단순하게 저장하기로 한다. 성벽을 둘러쌓기 위해서도 반복문을 사용해야 한다. 'ㄱ', 'ㄴ' 함수를 사용한다. 벽을 끝까지 그리기와 동서남북 4면을 그리기 위해 중첩반복문을 사용할 수 있다.
for 동서남북
for 벽을 끝까지 그리기
from IPython.display import Image
import os
dir=os.getcwd()
Image(filename=os.path.join(dir,'img/chieutInCastle.png'))
# %load src/chieutInCastle.py
import turtle
win=turtle.Screen()
t1=turtle.Turtle()
t1.shape('turtle')
t1.shapesize(1)
def chieut(size):
"""
draw chieut in shape of head and body
Parameters
----------
arg1 size
Returns
-------
none
Examples
--------
chieut(100)
"""
posHead=t1.pos() # head origin
t1.fd(size/2) # head
posBody=t1.pos() # body origin
t1.right(45)
t1.fd(size*.7) # left leg
t1.setpos(posBody)
t1.left(90)
t1.fd(size*.7) # right leg
t1.setpos(posBody)
t1.left(45)
t1.fd(size/2) # right arm
t1.bk(size) # full arm
t1.penup()
t1.setpos(posHead) # back to head without drawing
t1.pendown()
# to learn for-loop, relative size
def chieutMirror():
for i in range(4):
chieut(100)
def giyuk(size):
t1.fd(size)
t1.right(90)
t1.fd(size)
def nieun(size):
t1.fd(size)
t1.left(90)
t1.fd(size)
xHalf=win.window_width()/2
yHalf=win.window_height()/2
xLT=-xHalf+xHalf*.1 # 10 percent away
yLT=yHalf-yHalf*.1
xRT=-xLT
yRT=yLT
xLB=xLT
yLB=-yLT
xRB=xRT
yRB=yLB
def drawSquareWinFull():
# draw window square
t1.penup()
t1.goto(xLT,yLT)
t1.pendown()
t1.goto(xRT,yRT)
t1.goto(xRB,yRB)
t1.goto(xLB,yLB)
t1.goto(xLT,yLT)
def drawCastle(begin,end,size):
for i in range(begin,end,size*2):
giyuk(size)
t1.left(90)
nieun(size)
t1.right(90)
# to learn for-loop, relative size
def drawCastleAround(size):
"""
draw castle symbols all around the full sized window
Parameters
----------
arg1 size
Returns
-------
none
Examples
--------
drawCastleAround(30)
"""
t1.penup()
t1.goto(xLT,yLT)
t1.pendown()
begin=0
endHorizontal=int(win.window_width()*.9-size)
endVertical=int(win.window_height()*.9-size)
drawCastle(begin,endHorizontal,size)
t1.right(90)
drawCastle(begin,endVertical,size)
t1.right(90)
drawCastle(begin,endHorizontal,size)
t1.right(90)
drawCastle(begin,endVertical,size)
def main():
chieutMirror()
#drawSquareWinFull()
size=30
drawCastleAround(size)
win.exitonclick()
if __name__=="__main__":
main()