AI学习第五天-python的基础使用-趣味图形

一、Turtle 基础命令速查表

命令 功能 示例
forward(distance) 向前移动指定距离 turtle.forward(100)
backward(distance) 向后移动指定距离 turtle.backward(50)
left(angle) 左转指定角度 turtle.left(90)
right(angle) 右转指定角度 turtle.right(45)
goto(x, y) 移动到坐标 (x,y) turtle.goto(0, 0)
circle(radius) 绘制半径为 radius 的圆 turtle.circle(30)
penup() 抬起画笔(不绘制) turtle.penup()
pendown() 放下画笔(开始绘制) turtle.pendown()
pensize(size) 设置画笔粗细 turtle.pensize(3)
pencolor(color) 设置画笔颜色 turtle.pencolor("red")
fillcolor(color) 设置填充颜色 turtle.fillcolor("blue")
begin_fill() 开始填充形状 turtle.begin_fill()
end_fill() 结束填充形状 turtle.end_fill()
speed(speed) 设置绘图速度(0-10) turtle.speed(5)
done() 保持窗口不关闭 turtle.done()

二、绘图流程模板

收起

python

复制代码
import turtle

# 初始化画布
turtle.setup(width=600, height=400)  # 设置画布大小
turtle.title("Turtle Demo")  # 设置窗口标题

# 创建海龟对象
t = turtle.Turtle()  # 也可以直接使用turtle函数

# 绘图代码
t.forward(100)
t.left(90)
t.forward(100)

# 保持窗口
turtle.done()

三、基础案例

案例 1:绘制正方形

收起

python

复制代码
import turtle

t = turtle.Turtle()
for _ in range(4):
    t.forward(100)
    t.left(90)
turtle.done()
案例 2:绘制圆形

收起

python

复制代码
import turtle

t = turtle.Turtle()
t.circle(50)  # 半径50
turtle.done()
案例 3:绘制三角形

注意:转动的角度是外角

收起

python

复制代码
import turtle

t = turtle.Turtle()
for _ in range(3):
    t.forward(120)
    t.left(120)
turtle.done()
案例 4:绘制螺旋线

收起

python

复制代码
import turtle

t = turtle.Turtle()
length = 10
angle = 89

for _ in range(50):
    t.forward(length)
    t.left(angle)
    length += 2  # 逐渐增加长度
turtle.done()

四、进阶案例

案例 5:绘制花朵

收起

python

复制代码
import turtle
import random

t = turtle.Turtle()
t.speed(0)
colors = ["red", "orange", "yellow", "green", "blue"]

for _ in range(36):
    t.color(random.choice(colors))
    t.circle(100)
    t.left(10)
turtle.done()
案例 6:绘制五角星

收起

python

复制代码
import turtle

t = turtle.Turtle()
t.color("red")
t.begin_fill()

for _ in range(5):
    t.forward(150)
    t.left(144)  # 内角计算:180 - 360/(2*5) = 144

t.end_fill()
turtle.done()

五、注意事项

  1. 坐标系:画布中心为 (0,0),向右为 x 轴正方向,向上为 y 轴正方向。
  2. 角度单位:默认以度数为单位(如左转 90 度)。
  3. 颜色设置:支持颜色名称(如 "red")或 RGB 值(如 (255, 0, 0))。
  4. 循环优化:复杂图形建议用循环减少重复代码。
相关推荐
陈敬雷-充电了么-CEO兼CTO11 分钟前
复杂任务攻坚:多模态大模型推理技术从 CoT 数据到 RL 优化的突破之路
人工智能·python·神经网络·自然语言处理·chatgpt·aigc·智能体
YOLO大师37 分钟前
华为OD机试 2025B卷 - 小明减肥(C++&Python&JAVA&JS&C语言)
c++·python·华为od·华为od机试·华为od2025b卷·华为机试2025b卷·华为od机试2025b卷
一切顺势而行1 小时前
vue总结2
学习
xiao5kou4chang6kai41 小时前
【Python-GEE】如何利用Landsat时间序列影像通过调和回归方法提取农作物特征并进行分类
python·gee·森林监测·洪涝灾害·干旱评估·植被变化
kaikaile19951 小时前
使用Python进行数据可视化的初学者指南
开发语言·python·信息可视化
Par@ish1 小时前
【网络安全】恶意 Python 包“psslib”仿冒 passlib,可导致 Windows 系统关闭
windows·python·web安全
今天背单词了吗9801 小时前
算法学习笔记:11.冒泡排序——从原理到实战,涵盖 LeetCode 与考研 408 例题
java·学习·算法·排序算法·冒泡排序
意疏1 小时前
【Python篇】PyCharm 安装与基础配置指南
开发语言·python·pycharm
Brookty1 小时前
【操作系统】进程(二)内存管理、通信
java·linux·服务器·网络·学习·java-ee·操作系统
java攻城狮k1 小时前
【跟着PMP学习项目管理】项目管理 之 成本管理知识点
经验分享·笔记·学习·产品经理