博客
关于我
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出现 unknown encoding: idna 的处理方法
    查看>>
    Python出现 RuntimeError: dictionary changed size during iteration 的解决方法
    查看>>
    Python-numpy(2)数组的操作
    查看>>
    Python-Numpy的tile函数用法
    查看>>
    python-open函数
    查看>>
    Python-Pandas-将特定函数应用于给定级别-多索引数据框架
    查看>>
    python-queue队列通信
    查看>>
    python-ros No module named PyKDL
    查看>>
    python-social-auth 是否需要会话
    查看>>
    Python再获年度编程语言,微软或成最大赢家
    查看>>
    python-week5
    查看>>
    Python写表情包搜索工具(非常详细)零基础入门到精通,收藏这一篇就够了
    查看>>
    Python-【1】配置
    查看>>
    Python-从子文件夹执行绝对导入
    查看>>
    Python-刷新页面,直到元素可单击
    查看>>
    PYTHON-唤醒+VOSK+DEEPSEEK+离线合成-实现纯离线大模型问答
    查看>>
    Python-在Dash回调中重用函数
    查看>>
    PYTHON-基本数据类型-元祖类型,字典类型,集合类型
    查看>>
    python-外观模式
    查看>>
    Python-如何使用FastAPI和uvicorn.run而不阻塞线程?
    查看>>