猫史档案馆


【Python作品分享】行程【作业帖】

用户:不爱学英语!不爱学英语!查看:0 回复:1 评论:0 创建时间:2023-02-03T15:20:06


【作品展示】

center_image

 

【作品介绍】

画笔

 

【作品源代码】

import turtle


#开始进入Python的世界
start = 0
# 画笔初始化
x1=-200
x2=200
x3=200
小狗走的距离=0
男孩1 = turtle.Pen()
男孩2 = turtle.Pen()
dog = turtle.Pen()
男孩1.pencolor("#cc66cc")
男孩2.pencolor("#ff0000")
dog.pencolor("#ffff00")
男孩1.penup()
男孩2.penup()
dog.penup()
# 移到指定位置
男孩1.speed(1000)
男孩2.speed(1000)
dog.speed(1000)
男孩1.goto(x1, 0)
男孩2.goto(x2, 0)
dog.goto(x3, 0)
男孩2.right(180)
dog.right(180)
# 开始
start, 小狗方向 = 1, (-1)
print('开始')

while start:
    # x1x2往中间走
    x1 += 0.04
    男孩1.forward(0.04)
    x2 -= 0.06
    男孩2.forward(0.06)
    # 小狗走
    if (小狗方向 < 0):
        # 往左走
        x3 -= 0.2
        dog.forward(0.2)
        # 碰到男孩1就反弹
        if (x3 <= x1):
            小狗方向 = 1
            dog.right(180)
    else:
        # 往右走
        x3 += 0.2
        dog.forward(0.2)
        # 碰到男孩2就反弹
        if (x3 >= x2):
            小狗方向 = (-1)
            dog.right(180)
    # 计距离
    小狗走的距离 += 0.2
    # 两人相逢,退出循环
    if (x1 >= x2):
        break
# 输出距离
print('小狗走了',int(小狗走的距离), '米')

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
AokenAoken

点赞0


评论