写字和画圆

更多有趣的功能

除了画线,海龟还可以写字和画圆!让我们学习这些有趣的功能。

画圆

使用 circle(radius) 方法可以画圆:

import turtle

t = turtle.Turtle()

# 画一个半径为50的圆
t.circle(50)

画不同大小的圆

让我们画一系列不同大小的圆:

import turtle

t = turtle.Turtle()

# 画不同大小的圆
for i in range(5):
    t.circle(20 + i * 20)  # 半径从20到100

画彩色圆环

让我们画一个彩色圆环:

import turtle

t = turtle.Turtle()

# 彩虹的颜色
colors = ["red", "orange", "yellow", "green", "blue", "purple"]

# 画彩色圆环
for color in colors:
    t.pencolor(color)
    t.fillcolor(color)
    t.begin_fill()
    t.circle(50)
    t.end_fill()
    t.right(60)  # 移动到下一个圆的位置

画笑脸

让我们用圆和线画一个笑脸:

import turtle

t = turtle.Turtle()

# 画脸(黄色圆形)
t.penup()
t.goto(0, -50)
t.pendown()
t.fillcolor("yellow")
t.pencolor("black")
t.begin_fill()
t.circle(50)
t.end_fill()

# 画左眼
t.penup()
t.goto(-20, 20)
t.pendown()
t.fillcolor("black")
t.begin_fill()
t.circle(5)
t.end_fill()

# 画右眼
t.penup()
t.goto(20, 20)
t.pendown()
t.fillcolor("black")
t.begin_fill()
t.circle(5)
t.end_fill()

# 画嘴巴
t.penup()
t.goto(-25, -10)
t.pendown()
t.setheading(-60)  # 设置角度
t.circle(40, 120)  # 画弧形

写字

使用 write(text) 方法可以写字:

import turtle

t = turtle.Turtle()

# 设置写字属性
t.pencolor("blue")
t.write("Hello, Turtle!", font=("Arial", 16, "normal"))

写彩色字

让我们写彩色字:

import turtle

t = turtle.Turtle()

# 要写的字
text = "Python Turtle"

# 彩虹的颜色
colors = ["red", "orange", "yellow", "green", "blue", "purple"]

# 写彩色字
for i, char in enumerate(text):
    t.pencolor(colors[i % len(colors)])
    t.write(char, font=("Arial", 20, "bold"))
    t.forward(15)  # 移动到下一个字符的位置

画彩色气球

让我们画一些彩色气球:

import turtle

t = turtle.Turtle()

# 气球颜色和位置
colors = ["red", "blue", "green", "yellow"]
positions = [(-100, 0), (0, 0), (100, 0), (-50, 100)]

# 画气球
for color, pos in zip(colors, positions):
    # 移动到气球位置
    t.penup()
    t.goto(pos[0], pos[1])
    t.pendown()

    # 画气球(圆形)
    t.fillcolor(color)
    t.pencolor(color)
    t.begin_fill()
    t.circle(30)
    t.end_fill()

    # 画气球线
    t.pencolor("black")
    t.right(90)
    t.forward(100)
    t.left(90)

画太阳和云朵

让我们画一个太阳和云朵的场景:

import turtle

t = turtle.Turtle()

# 设置背景颜色
turtle.bgcolor("lightblue")

# 画太阳
t.penup()
t.goto(150, 100)
t.pendown()
t.fillcolor("yellow")
t.pencolor("orange")
t.begin_fill()
t.circle(30)
t.end_fill()

# 画云朵
def draw_cloud(x, y):
    t.penup()
    t.goto(x, y)
    t.pendown()
    t.fillcolor("white")
    t.pencolor("white")
    t.begin_fill()
    t.circle(20)
    t.end_fill()
    t.penup()
    t.goto(x + 15, y + 10)
    t.pendown()
    t.begin_fill()
    t.circle(15)
    t.end_fill()
    t.penup()
    t.goto(x + 30, y)
    t.pendown()
    t.begin_fill()
    t.circle(18)
    t.end_fill()

# 画多个云朵
draw_cloud(-100, 100)
draw_cloud(-50, 120)
draw_cloud(0, 90)

练习

  1. 画一个彩色奥运五环
  2. 画一个生日蛋糕,上面写"生日快乐"
  3. 画一个你自己的创意场景
  4. 尝试用圆和字创建一个故事

小提示

  • 使用 t.circle(radius, extent) 可以画圆的一部分
  • 使用 t.dot(size) 可以画一个点
  • 使用 t.write(text, align="center") 可以居中对齐文字
  • 使用 font=(font_name, size, style) 设置字体样式

思考题

如果你想画一个半圆,应该怎么做呢?


下一课我们将学习如何创建动画效果!🐢

Python 工具/在线模拟器/Python海龟绘图可视化
Python海龟绘图可视化 - https://www.cnbbx.com/python-editor/