python turtle画求逃生路线图

最近有个人要用turtle求生路线图,我这里画了一下,觉得挺烦的,不过最终还是画出来了。

import turtle 
turtle.title('逃生路线')

turtle.tracer(0)  # Turns off screen updates

grid = turtle.Turtle()
STEP = 20
LENGTH = 300
split_num=15


def draw_rectangle(width,height):
    for x in range(1, 5):
        if x % 2 == 1:
            n = width
        else:
            n = height
        grid.forward(n)
        grid.right(90)


def draw_name(x,y):  #给红色区域位置加入文字
    turtle.up()  
    turtle.goto(x+12, y-15)
    turtle.write('楼梯', align="center", font=("Courier", 10, "bold"))
    turtle.down()

def draw_room_name(x,y,word):  #给红色区域位置加入文字
    turtle.up()  
    turtle.goto(x+12, y-15)
    turtle.write(word, align="center", font=("Courier", 8, "bold"))
    turtle.down()


def draw_floor(floor,gap): # 画楼层的形状
    grid.penup()
    grid.setpos(-LENGTH/2-STEP, (LENGTH-gap*STEP))
    x=-LENGTH/2-STEP
    y=LENGTH-gap*STEP
    draw_room_name(x,y,'俱')
    draw_room_name(x,y-10,'乐')
    draw_room_name(x,y-20,'部')
    grid.pendown()
    for x in range(1, 5): # 画每个楼层最左边的矩形,宽为20,高为60
        if x % 2 == 1:
            n = 20
        else:
            n = 60
        grid.forward(n)
        grid.right(90)

    turtle.up() # 画楼层的名字
    turtle.goto(-LENGTH/2-2*STEP, LENGTH-STEP*1.5-gap*STEP)
    turtle.write('第{}层'.format(floor), align="center", font=("Courier", 14, "bold"))
    turtle.down()
    turtle.hideturtle()

    for i in range(0, LENGTH+STEP, STEP):  # 画楼层的结构
        if(i!=LENGTH):
            if(i==STEP or i==12*STEP): # 其中楼梯区域要填充红色
                grid.begin_fill()
                grid.fillcolor('#FF0000')
                grid.penup()
                grid.setpos((-LENGTH/2 + i), LENGTH-gap*STEP)
                x=-LENGTH/2 + i
                y=LENGTH-gap*STEP
                draw_name(x,y)
                grid.pendown()
                draw_rectangle(20,20)
                
                grid.end_fill()
            else:  # 其他地方只需要画矩形
                grid.penup()
                grid.setpos((-LENGTH/2 + i), LENGTH-gap*STEP)
                x=-LENGTH/2 + i
                y=LENGTH-gap*STEP
                draw_room_name(x,y,'房间')
                grid.pendown()
                draw_rectangle(20,20)
        else: # 画从左往右最后一个矩形,其中最后一个矩形的还要包括右边界
            grid.penup()
            grid.setpos((-LENGTH/2 + i), LENGTH-gap*STEP) # go to start vertical line possition
            grid.pendown()
            grid.setpos((-LENGTH / 2 + i), LENGTH-2*STEP-gap*STEP)

    grid.penup() 
    grid.setpos(-LENGTH/2, (-0-2*STEP+LENGTH-gap*STEP)) # go to start hoz line possition
    grid.pendown()
    grid.setpos(LENGTH/2, (0-2*STEP+LENGTH-gap*STEP))

    for i in range(0, LENGTH, STEP): # 画这层楼的箭头下面的矩形区域
        grid.penup()
        grid.setpos((-LENGTH/2 + i), LENGTH-2*STEP-gap*STEP) # go to start vertical line possition
        x=-LENGTH/2 + i
        y=LENGTH-2*STEP-gap*STEP
        draw_room_name(x,y,'房间')
        grid.pendown()
        draw_rectangle(20,20)
    grid.hideturtle()

for i in range(7): # 循环遍历画每一层楼层
    gap=i*4
    floor=7-i
    draw_floor(floor,gap) 
    if(i==0 or i==2 or i==4): # 给第7,5,3层楼画箭头
        arraw1 = turtle.Turtle() # 画左边的箭头
        arraw1.penup()
        arraw1.setpos(-150,LENGTH-STEP-10-gap*STEP)
        arraw1.pendown()
        arraw1.forward(240)

        arraw2 = turtle.Turtle()
        arraw2.penup() # 画右边的箭头
        arraw2.setpos(150, LENGTH-STEP-10-gap*STEP)
        arraw2.pendown()
        arraw2.left(180)
        arraw2.forward(50)
    if(i==1 or i==3 or i==5): # 给第6,4,2层录画箭头
        arraw1 = turtle.Turtle()

        arraw1.penup() # 画左箭头
        arraw1.setpos(-150, LENGTH-STEP-10-gap*STEP)
        arraw1.pendown()
        arraw1.forward(20)

        arraw2 = turtle.Turtle()
        arraw2.penup() # 画右边的箭头
        arraw2.setpos(150, LENGTH-STEP-10-gap*STEP)
        arraw2.pendown()
        arraw2.left(180)
        arraw2.forward(260)
    elif(i==6): # 给第一层楼画箭头
        arraw1 = turtle.Turtle()

        arraw1.penup() # 最左边的箭头
        arraw1.setpos(-150, LENGTH-STEP-10-gap*STEP)
        arraw1.pendown()
        arraw1.forward(20) 


        arraw2 = turtle.Turtle() # 最右边的箭头
        arraw2.penup()
        arraw2.setpos(150, LENGTH-STEP-10-gap*STEP)
        arraw2.pendown()
        arraw2.left(180)
        arraw2.forward(50)

        arraw3 = turtle.Turtle() # 中间靠右的箭头
        arraw3.penup()
        arraw3.setpos(0, LENGTH-STEP-10-gap*STEP)
        arraw3.pendown()
        arraw3.forward(90)

        arraw4 = turtle.Turtle() # 中间靠左的箭头
        arraw4.penup()
        arraw4.setpos(0, LENGTH-STEP-10-gap*STEP)
        arraw4.pendown()
        arraw4.left(180)
        arraw4.forward(120)

def draw_description(x,y):  #给红色区域位置加入文字
    turtle.up()  
    turtle.goto(x+60, y-5)
    turtle.write('疏散路线', align="center", font=("Courier", 10, "bold"))
    turtle.down()

def draw_tuli(x,y):
    turtle.up()  
    turtle.goto(x, y-5)
    turtle.write('图例:', align="center", font=("Courier", 10, "bold"))
    turtle.down()

def draw_description_v2(x,y):  #给红色区域位置加入文字
    turtle.up()  
    turtle.goto(x+40, y-15)
    turtle.write('房间', align="center", font=("Courier", 10, "bold"))
    turtle.down()

arraw5 = turtle.Turtle() # 中间靠左的箭头
arraw5.penup()
x=-LENGTH/2
y=LENGTH-5*STEP-gap*STEP
arraw5.setpos(x, y)
draw_description(x,y)
draw_tuli(x-STEP,y)
arraw5.pendown()
arraw5.forward(30)

arraw6 = turtle.Turtle() # 中间靠左的箭头
arraw6.penup()
x=-LENGTH/2+5*STEP
y=LENGTH-4.5*STEP-gap*STEP
arraw6.setpos(x, y)
draw_description_v2(x,y)
arraw6.pendown()
for x in range(1, 5): # 画每个楼层最左边的矩形,宽为20,高为60
    if x % 2 == 1:
        n = 20
    else:
        n = 20
    arraw6.forward(n)
    arraw6.right(90)
arraw6.hideturtle()


def draw_description_v3(x,y):  #给红色区域位置加入文字
    turtle.up()  
    turtle.goto(x, y+25)
    turtle.write('N', align="center", font=("Courier", 10, "bold"))
    turtle.down()

arraw2 = turtle.Turtle() # 最右边的箭头
arraw2.penup()
x=200
y=LENGTH-STEP
arraw2.setpos(200, LENGTH-STEP)
draw_description_v3(x,y)
arraw2.pendown()
arraw2.left(90)
arraw2.forward(20)

turtle.update() 
turtle.exitonclick() # 按任意按键退出

展示

python turtle画求逃生路线图_第1张图片

 

你可能感兴趣的:(turtle)