Python基础学习笔记——用海龟绘图实现两两相连

学习目标:

使用海龟绘图。输入多个点,将这些点都两两相连。

代码:

def connect_dot(*args):
    for i in range(len(args) - 1):
        for j in range(i + 1, len(args)):
            import turtle as t  #利用海龟绘图两两相连
            t.speed(3)
            t.penup()
            t.goto(*args[i])
            t.pendown()
            t.goto(*args[j])
    t.done()

n = int(input("请输入n个点(n>1):"))    
list = [tuple(map(int, input(f'请输入第{i + 1}个点坐标:').split())) for i in range(n)]
connect_dot(*list)

运行结果:

在这里插入图片描述
在这里插入图片描述

利用元组向函数传递不同的点,最后利用海龟绘图可成功使各点相连。
如有错误,请多多指出.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然可以!使用Python海龟绘图库可以让我们绘制出皮卡丘的图像。下面是一个简单的示例代码: ```python import turtle as t # 设置画布大小和背景色 t.setup(800, 600) t.bgcolor('yellow') # 绘制皮卡丘的头部 t.penup() t.goto(-100, 100) t.pendown() t.fillcolor('yellow') t.begin_fill() t.circle(100) t.end_fill() # 绘制皮卡丘的眼睛 t.penup() t.goto(-60, 120) t.pendown() t.fillcolor('white') t.begin_fill() t.circle(20) t.end_fill() t.penup() t.goto(-40, 120) t.pendown() t.fillcolor('black') t.begin_fill() t.circle(10) t.end_fill() t.penup() t.goto(-140, 120) t.pendown() t.fillcolor('white') t.begin_fill() t.circle(20) t.end_fill() t.penup() t.goto(-120, 120) t.pendown() t.fillcolor('black') t.begin_fill() t.circle(10) t.end_fill() # 绘制皮卡丘的嘴巴 t.penup() t.goto(-80, 30) t.pendown() t.setheading(-60) t.circle(80, 120) t.fillcolor('black') t.begin_fill() t.circle(80, 120) t.setheading(0) t.circle(80, 120) t.end_fill() # 绘制皮卡丘的脚 t.penup() t.goto(-70, -70) t.pendown() t.setheading(-30) t.forward(80) t.setheading(0) t.forward(40) t.setheading(90) t.fillcolor('black') t.begin_fill() t.circle(40, 180) t.end_fill() t.setheading(180) t.forward(40) t.setheading(-90) t.circle(40, 180) t.setheading(-60) t.forward(80) # 绘制皮卡丘的手臂 t.penup() t.goto(30, -70) t.pendown() t.setheading(-30) t.forward(80) t.setheading(0) t.forward(40) t.setheading(90) t.fillcolor('black') t.begin_fill() t.circle(40, 180) t.end_fill() t.setheading(180) t.forward(40) t.setheading(-90) t.circle(40, 180) t.setheading(-60) t.forward(80) # 隐藏画笔 t.hideturtle() # 完成绘制 t.done() ``` 运行以上代码,即可在窗口中看到绘制出的皮卡丘图像。你也可以根据需要调整代码中的坐标和尺寸来绘制更精确的图像。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值