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

    你可能感兴趣的文章
    MySQL的insert-on-duplicate语句详解
    查看>>
    mysql的logrotate脚本
    查看>>
    MySQL的my.cnf文件(解决5.7.18下没有my-default.cnf)
    查看>>
    MySQL的on duplicate key update 的使用
    查看>>
    MySQL的Replace用法详解
    查看>>
    mysql的root用户无法建库的问题
    查看>>
    mysql的sql_mode参数
    查看>>
    MySQL的sql_mode模式说明及设置
    查看>>
    mysql的sql执行计划详解
    查看>>
    mysql的sql语句基本练习
    查看>>
    Mysql的timestamp(时间戳)详解以及2038问题的解决方案
    查看>>
    mysql的util类怎么写_自己写的mysql类
    查看>>
    MySQL的xml中对大于,小于,等于的处理转换
    查看>>
    mysql的下载安装
    查看>>
    Mysql的两种存储引擎详细分析及区别(全)
    查看>>
    mysql的临时表简介
    查看>>
    MySQL的主从复制云栖社区_mysql 主从复制配置
    查看>>
    MySQL的事务隔离级别实战
    查看>>
    mysql的优化策略有哪些
    查看>>
    MySQL的使用
    查看>>