海龟图

创建一个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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值