Python - 用 turtle 绘制圆形

大学计算机–Python算法实践
李金双李凤云张昱

  • Turtle 库是 Python 语言中一个很流行的绘制图像的函数库

  • Turtle 库用于绘制线、圆、其他形状或者文本

  • 显示小乌龟的爬行轨迹,初始小乌龟在(0, 0),前进方向为 x 轴正方向

  • 绘制圆形

    import turtle
    turtle.color('red')
    turtle.circle(39)
    turtle.done()
  • turtle.circle(radius,extent,step)

    • radius 是必需的,表示半径,正值时逆时针旋转
    • extent 表示度数,用于绘制圆弧;
    • step 表示边数,可用于绘制正多边形;
    • extent 和 step 参数可有可无。
  • 实例1

    import turtle
    turtle.color('orange')
    turtle.pensize(3)
    turtle.circle(75)
    turtle.penup()
    turtle.goto(0, -200)
    turtle.pendown()
    turtle.circle(100)
    turtle.done()

    这里写图片描述

  • 实例2

    import turtle
    turtle.color('purple')
    
    for i in range(10):
      turtle.circle(i*10)
      turtle.left(126)
    
    turtle.done()

    这里写图片描述

  • 17
    点赞
  • 103
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,让我们开始吧!首先,我们需要导入 `turtle` 库: ```python import turtle ``` 接下来,我们需要设置窗口大小和背景色: ```python turtle.setup(600, 600) turtle.bgcolor("white") ``` 然后,我们可以开始绘制小库洛米了。首先,我们需要定义一个函数来绘制头部: ```python def draw_head(): turtle.penup() turtle.goto(0, 200) turtle.pendown() turtle.begin_fill() turtle.circle(150) turtle.end_fill() ``` 这个函数会绘制一个半径为 150 的圆形,作为小库洛米的头部。我们使用 `begin_fill()` 和 `end_fill()` 函数来填充内部颜色。 接下来,我们需要绘制眼睛。我们可以定义一个函数来绘制一个眼睛: ```python def draw_eye(x, y): turtle.penup() turtle.goto(x, y) turtle.pendown() turtle.begin_fill() turtle.circle(30) turtle.end_fill() ``` 这个函数会绘制一个半径为 30 的圆形,作为小库洛米的眼睛。我们使用 `begin_fill()` 和 `end_fill()` 函数来填充内部颜色。 现在,我们可以在头部上绘制两个眼睛了: ```python draw_eye(-60, 280) draw_eye(60, 280) ``` 接下来,我们需要绘制小库洛米的嘴巴。我们可以定义一个函数来绘制一个嘴巴: ```python def draw_mouth(): turtle.penup() turtle.goto(0, 100) turtle.pendown() turtle.setheading(-60) turtle.circle(80, 120) ``` 这个函数会绘制一个弧形,作为小库洛米的嘴巴。 现在,我们可以在头部上绘制嘴巴了: ```python draw_mouth() ``` 接下来,我们需要绘制小库洛米的身体。我们可以定义一个函数来绘制一个身体: ```python def draw_body(): turtle.penup() turtle.goto(0, -150) turtle.pendown() turtle.begin_fill() turtle.circle(200) turtle.end_fill() ``` 这个函数会绘制一个半径为 200 的圆形,作为小库洛米的身体。我们使用 `begin_fill()` 和 `end_fill()` 函数来填充内部颜色。 现在,我们可以在窗口中绘制小库洛米了: ```python draw_head() draw_eye(-60, 280) draw_eye(60, 280) draw_mouth() draw_body() ``` 最后,我们可以隐藏画笔,表示绘制完成: ```python turtle.hideturtle() ``` 完整代码如下: ```python import turtle def draw_head(): turtle.penup() turtle.goto(0, 200) turtle.pendown() turtle.begin_fill() turtle.circle(150) turtle.end_fill() def draw_eye(x, y): turtle.penup() turtle.goto(x, y) turtle.pendown() turtle.begin_fill() turtle.circle(30) turtle.end_fill() def draw_mouth(): turtle.penup() turtle.goto(0, 100) turtle.pendown() turtle.setheading(-60) turtle.circle(80, 120) def draw_body(): turtle.penup() turtle.goto(0, -150) turtle.pendown() turtle.begin_fill() turtle.circle(200) turtle.end_fill() turtle.setup(600, 600) turtle.bgcolor("white") draw_head() draw_eye(-60, 280) draw_eye(60, 280) draw_mouth() draw_body() turtle.hideturtle() turtle.done() ``` 运行代码,你会看到一个卡通小库洛米出现在窗口中。祝绘画愉快!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值