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

    你可能感兴趣的文章
    Netwox网络工具使用详解
    查看>>
    Net与Flex入门
    查看>>
    Net任意String格式转换为DateTime类型
    查看>>
    net包之IPConn
    查看>>
    net发布的dll方法和类显示注释信息(字段说明信息)[图解]
    查看>>
    Net和T-sql中的日期函数操作
    查看>>
    Net处理html页面元素工具类(HtmlAgilityPack.dll)的使用
    查看>>
    Net操作Excel(终极方法NPOI)
    查看>>
    Net操作配置文件(Web.config|App.config)通用类
    查看>>
    net网络查看其参数state_dict,data,named_parameters
    查看>>
    Net连接mysql的公共Helper类MySqlHelper.cs带MySql.Data.dll下载
    查看>>
    NeurIPS(神经信息处理系统大会)-ChatGPT4o作答
    查看>>
    neuroph轻量级神经网络框架
    查看>>
    Neutron系列 : Neutron OVS OpenFlow 流表 和 L2 Population(7)
    查看>>
    new Blob()实现不同类型的文件下载功能
    查看>>
    New Concept English three (35)
    查看>>
    NEW DATE()之参数传递
    查看>>
    New Journey--工作五年所思所感小记
    查看>>
    new Queue(REGISTER_DELAY_QUEUE, true, false, false, params)
    查看>>
    New Relic——手机应用app开发达人的福利立即就到啦!
    查看>>