博客
关于我
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/

    你可能感兴趣的文章
    Python3-selenium自动化测试框架,结合HTMLTestRunner测试报告!
    查看>>
    Python3-如何自我更新作为服务运行的应用程序
    查看>>
    Python3.1-网格模拟概念性问题
    查看>>
    Python3.12 解决conda环境libstdc++版本过低问题
    查看>>
    python3.2批量查询IP地址区域
    查看>>
    Python3.5中协程和未来/任务之间的区别?
    查看>>
    python3.5实现socket通讯(TCP)
    查看>>
    python3.6 ubuntu部署nginx、 uwsgi、 django
    查看>>
    Python3.6.3中,functools似乎不能用
    查看>>
    python3.6.5+cuda9+cudnn7.1+win10+tensorflow-gpu1.9.0下载配置
    查看>>
    python3.6使用pickle序列化class
    查看>>
    python3.6入门到高阶(全栈) day027 网络通信协议 osi七层模型
    查看>>
    Python3.6属性错误:模块‘;异步没有属性‘;运行‘;
    查看>>
    python3.6版本中的python-weka-wrapper安装失败,错误代码为%1
    查看>>
    Python3.7安装cv2踩过的坑
    查看>>
    python写txt文件
    查看>>
    python关于sorted排序函数详细解析 附实战代码(全)
    查看>>
    python3.8调用m文件,调用matlab
    查看>>
    python3.9新特性_Python 3.9 新特性速览
    查看>>
    Python3.x:遍历select下拉框获取value值
    查看>>