博客
关于我
Python学习打卡—海龟画图
阅读量:634 次
发布时间:2019-03-14

本文共 1906 字,大约阅读时间需要 6 分钟。

Python学习第一天——turtle画国旗

由于已有C语言基础,故不再重申基础知识,直接上代码!

一、画国旗背景

import turtleturtle.up()turtle.goto(-200, 200)turtle.down()turtle.begin_fill()turtle.fillcolor("red")turtle.pencolor("red")turtle.color('red', 'red')for i in range(2):    turtle.forward(300)    turtle.right(90)    turtle.forward(200)    turtle.right(90)turtle.end_fill()
常用函数解析:
  • turtle.forward(distance):向当前画笔方向移动distance像素长度。
  • 画国旗背景:
    • 初始化并移动画笔至(-200, 200),开始绘制。
    • 使用循环绘制两块横向长条,颜色均为红色。
    • 填充功能:
      • turtle.begin_fill():准备开始填充图形。
      • turtle.fillcolor("red"):设置填充颜色为红色。
      • turtle.end_fill():填充完成,生成填充色的图形。

    二、绘制五角星

    在国旗背景的基础上,依次绘制五个黄色五角星。

    第一颗五角星
    turtle.up()turtle.goto(-170, 145)turtle.down()turtle.begin_fill()turtle.fillcolor("yellow")turtle.pencolor("yellow")for x in range(5):    turtle.forward(45)    turtle.right(144)turtle.end_fill()
    第二颗五角星
    turtle.up()turtle.goto(-115, 175)turtle.down()turtle.begin_fill()turtle.fillcolor("yellow")turtle.pencolor("yellow")turtle.right(30)for x in range(5):    turtle.forward(18)    turtle.right(144)turtle.end_fill()
    第三颗五角星
    turtle.up()turtle.goto(-90, 157)turtle.down()turtle.begin_fill()turtle.fillcolor("yellow")turtle.pencolor("yellow")turtle.right(30)for x in range(5):    turtle.forward(18)    turtle.right(144)turtle.end_fill()
    第四颗五角星
    turtle.up()turtle.goto(-87, 130)turtle.down()turtle.begin_fill()turtle.fillcolor("yellow")turtle.pencolor("yellow")turtle.right(30)for x in range(5):    turtle.forward(18)    turtle.right(144)turtle.end_fill()
    第五颗五角星
    turtle.up()turtle.goto(-106, 106)turtle.down()turtle.begin_fill()turtle.fillcolor("yellow")turtle.pencolor("yellow")turtle.right(30)for x in range(5):    turtle.forward(18)    turtle.right(144)turtle.end_fill()turtle.hideturtle()
    常用函数解析:
    • turtle.hideturtle():隐藏画笔的turtle形状。
    • turtle.showturtle():显示画笔的turtle形状。

    效果展示

    请参考以下图片描述链接查看完整效果图:

    [图片描述链接,存于mailto:image图片由于无法嵌入,建议参考源图片]


    以上代码基于turtle库编写,适合用于生成图形。通过循环调用turtle的_basic方法,实现了国旗背景的绘制以及五角星的放置。每个五角星的位置和大小均经过精心调试,以确保最终效果符合预期。

    转载地址:http://tbeoz.baihongyu.com/

    你可能感兴趣的文章
    Python在def函数中使用for循环
    查看>>
    Python在Conda环境中,但在Windows虚拟环境中没有激活
    查看>>
    python系列【仅供参考】:python pip 错误 ModuleNotFoundError: No module named pip._internal 解决办法
    查看>>
    python图像条状状噪声,使用PYTHON PIL从验证码图像中删除背景嘈杂的线条
    查看>>
    Python图像处理:从内存加载jpeg
    查看>>
    python固定后缀(名物化词汇)词频统计:抽取+统计+可视化
    查看>>
    python商品评论数据采集与分析可视化系统 Flask框架 requests爬虫 NLP情感分析 毕业设计 源码
    查看>>
    python商品数据分析可视化系统(带爬虫)京东销售数据分析 计算机毕业设计 源码下载
    查看>>
    python商品库存管理系统 django框架 商品网站 MySQL数据库 源码下载 计算机毕业设计
    查看>>
    Python哪个版本最稳定好用2023.10.19
    查看>>
    Python和黑客技术的渊源
    查看>>
    Python和RF编写接口自动化
    查看>>
    python和js哪个难_【Python】记一次学习,Python 与 js 在实现闭包时的差异
    查看>>
    python和java哪个更值得学——来自知乎高赞回答
    查看>>
    python命名空间更改_Python模块xml.etree.ElementTree自动修改xml命名空间键
    查看>>
    Python命名中尾随下划线有什么好处?
    查看>>
    Python启动脚本
    查看>>
    Python听写汇总结果
    查看>>
    python吊打Excel?屁!那是你不会用!
    查看>>
    Python合并两张图片 | 先叠透明度再合并 (附Demo)
    查看>>