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

在 Python 编程学习过程中,turtle库是一个非常有趣且实用的工具,它可以帮助我们轻松绘制各种图形。结合for循环、random模块以及自定义方法等知识点,能够创作出丰富多彩的图案。下面就来分享一下相关的学习笔记。

一、基础知识点回顾

(一)for循环

for循环是 Python 中用于遍历可迭代对象(如列表、字符串、范围等)的一种控制结构。其基本语法为:

收起

python

复制代码
for 变量 in 可迭代对象:
    循环体代码

turtle绘图中,常利用for循环来重复执行绘图动作,比如绘制多边形时重复向前移动和旋转的操作。

(二)random模块

random模块用于生成随机数。在绘图时,它能为图形添加随机性,比如随机选择颜色。常用的函数有random.choice(),用于从序列(如列表)中随机选择一个元素。例如:

收起

python

复制代码
import random
colors = ['red', 'green', 'blue']
random_color = random.choice(colors)

(三)方法与main入口

自定义方法(函数)可以将重复的代码块封装起来,提高代码的复用性。在 Python 中,定义方法的语法如下:

收起

python

复制代码
def 方法名(参数列表):
    方法体代码
    return 返回值(可选)

if __name__ == '__main__':是 Python 中的一个特殊语句,它确保在当前脚本作为主程序运行时,其下的代码才会被执行。当该脚本被其他模块导入时,这部分代码不会被执行,方便进行模块的测试和封装。

二、使用turtle库绘制多边形花朵

(一)代码实现

收起

python

复制代码
import random
import turtle


def draw_flower(count, length):
    colors = ['red', 'green', 'blue', 'yellow', 'orange', 'purple']
    t = turtle.Turtle()
    t.shape("turtle")
    for i in range(count):
        t.color(random.choice(colors))
        t.forward(length)
        t.left(360 / count)
    turtle.done()


if __name__ == '__main__':
    draw_flower(8, 100)

(二)代码解析

  1. 导入模块 :使用import randomimport turtle分别导入random模块和turtle库。
  2. 定义draw_flower方法 :该方法接受两个参数count(表示多边形的边数,即花朵的花瓣数)和length(表示边长) 。
  3. 设置画笔属性 :创建turtle.Turtle()对象t,并设置画笔形状为turtle
  4. 绘制花朵 :利用for循环,循环次数为count。在每次循环中,通过random.choice(colors)随机选择一种颜色,然后让画笔向前移动length的距离,再向左旋转360/count度,这样就能绘制出一个由不同颜色多边形组成的花朵形状。
  5. 主程序入口 :在if __name__ == '__main__':代码块中,调用draw_flower(8, 100),绘制一个有 8 片花瓣,边长为 100 的花朵。

通过花朵绘制方式的学习,不仅深入了解了turtle库的绘图功能,还熟练掌握了for循环、random模块以及自定义方法的使用。在后续的学习中,可以继续探索turtle库的其他功能,创作出更复杂、更精美的图形。

python 复制代码
import random
import turtle
def draw_flower( length):
    colors = ['red', 'green', 'blue', 'yellow', 'orange', 'purple']
    t = turtle.Turtle()
    t.shape("turtle")
    #t.color(random.choice(colors))
    t.fillcolor("yellow")
    t.begin_fill()
    t.circle(length)
    t.end_fill()
    t.penup()
    t.goto(-25,70)
    t.pendown()

    t.fillcolor("black")
    t.begin_fill()
    t.circle(5)
    t.end_fill()

    t.penup()
    t.goto(25, 70)
    t.pendown()
    t.fillcolor("black")
    t.begin_fill()
    t.circle(5)
    t.end_fill()
    t.penup()
    t.goto(-25, 45)

    t.rt(90)
    t.pendown()
    t.width(5)
    t.circle(25,160)


    turtle.done()


if __name__ == '__main__':
    draw_flower( 60)
python 复制代码
import turtle
def draw_flower( length):

    t = turtle.Turtle()
    t.shape("turtle")
    t.penup()
    t.goto(0, 63)
    t.pendown()
    t.left(180)
    t.fillcolor("black")
    t.begin_fill()
    t.circle(length * 2, 180)
    t.end_fill()
    t.fillcolor("white")
    t.begin_fill()
    t.circle(length * 2, 180)
    t.end_fill()
    t.penup()
    t.goto(0, 63)
    t.pendown()
    t.fillcolor("white")
    t.begin_fill()
    t.circle(length,180)
    t.end_fill()
    t.penup()
    t.goto(0, -63*3)
    t.pendown()
    t.fillcolor("black")
    t.begin_fill()
    t.circle(length , 180)
    t.end_fill()
    t.penup()
    t.goto(0, length/3)
    t.fillcolor("black")
    t.begin_fill()
    t.circle(length/3)
    t.end_fill()
    t.penup()

    t.goto(0, (-63*2)+length/3)
    t.fillcolor("white")
    t.begin_fill()
    t.circle(length / 3)
    t.end_fill()
    t.penup()



    turtle.done()


if __name__ == '__main__':
    draw_flower( 63)
相关推荐
ZTLJQ8 分钟前
序列化的艺术:Python JSON处理完全解析
开发语言·python·json
H5css�海秀29 分钟前
今天是自学大模型的第一天(sanjose)
后端·python·node.js·php
Z兽兽30 分钟前
React@18+Vite项目配置env文件
前端·react.js·前端框架
SuniaWang37 分钟前
《Spring AI + 大模型全栈实战》学习手册系列 · 专题六:《Vue3 前端开发实战:打造企业级 RAG 问答界面》
java·前端·人工智能·spring boot·后端·spring·架构
阿贵---1 小时前
使用XGBoost赢得Kaggle比赛
jvm·数据库·python
nap-joker1 小时前
【多模态解耦】DecAlign:用于解耦多模态表示学习的分层跨模态对齐
学习·多模态融合·最优传输·多模态表征学习·特征解耦·音频+图像+文本·原型引导
无敌昊哥战神1 小时前
【LeetCode 257】二叉树的所有路径(回溯法/深度优先遍历)- Python/C/C++详细题解
c语言·c++·python·leetcode·深度优先
551只玄猫1 小时前
【数据库原理 实验报告1】创建和管理数据库
数据库·sql·学习·mysql·课程设计·实验报告·数据库原理
A_nanda1 小时前
根据AI提示排查vue前端项目
前端·javascript·vue.js
IDZSY04302 小时前
AI社交平台进阶指南:如何用AI社交提升工作学习效率
人工智能·学习