海龟图

创建一个20*20的格子,用命令让一只海龟在格子上画线。可以前进、左转、右转,拿起或放下笔等等。复杂一点的话,允许程序从文件中读取命令列表。

import turtle

#创建画线的海龟
t = turtle.Pen()



#创建20*20的格子,以15个像素点韦格子边长
def creatScreen():
    #构建20*20的正方形
    t.forward(20*15)
    t.right(90)
    t.forward(20*15)
    t.right(90)
    t.forward(20*15)
    t.right(90)
    t.forward(20*15)
    #绘制竖制的19条线
    i = 0
    j = 0
    while i<10 :
        t.right(90)
        t.forward(15)
        t.right(90)
        t.forward(20*15)
        t.left(90)
        t.forward(15)
        t.left(90)
        t.forward(20*15)
        i = i+1
    t.right(180)
    while j<10 :
        t.forward(15)
        t.right(90)
        t.forward(20*15)
        t.left(90)
        t.forward(15)
        t.left(90)
        t.forward(15*20)
        t.right(90)
        j = j+1


#前进
def forword(distance):
    t.forward(15*distance)

#向左转
def left():
    t.left(9)

#向右转
def right():
    t.right(90)

#拿起画笔
def up():
    t.up()

#放下画笔
def down():
    t.down()

#主函数
def main():
    creatScreen()
    print("请输入对海龟的命令,以End为退出标志")
    comand = input()
    while comand!="End" :
        if comand == "Forword":
            print("前进格子数目")
            distance = int(input())
            forword(distance)
            comand = input()
        elif comand == "Left":
            left()
            comand = input()
        elif comand == "Right":
            right()
            comand = input()
        elif comand == "Up":
            up()
            comand = input()
        elif comand == "Down":
            down()
            comand = input()




if __name__ == '__main__':
    main()

你可能感兴趣的:(Python)