[Python] 用 turtle 来绘制瑞典国旗

背景

turtle --- Turtle graphics 中提供了关于 turtle\text{turtle} turtle 模块的介绍。我想用 turtle\text{turtle} turtle 来生成些简单的图案,以巩固学习的效果。有些国家的国旗图案比较简单,可以从这里入手

正文

基本的数据

根据 Sweden 网页所提供的信息,瑞典国旗中各部分的比例如下图所示

我在别的网站上搜到了瑞典国旗中,蓝色和黄色的具体数值(下图中给出了两组数值,我们就用 NCS\text{NCS} NCS 那一组吧)。

代码

我的整体思路是这样的 ⬇️

  1. 先画出一个蓝色的矩形
  2. 画出黄色十字的那一横
  3. 画出黄色十字的那一竖

在此基础上,可以写出完整的 Python\text{Python} Python 代码 ⬇️

python 复制代码
import turtle

ratio = 40
length = 5 + 2 + 9
height = 4 + 2 + 4

def draw_rectangle(start_x, start_y, length, height, color):
    turtle.teleport(start_x, start_y)
    turtle.setheading(0)

    turtle.color(color, color)
    with turtle.fill():
        turtle.forward(length)
        turtle.right(90)
        turtle.forward(height)
        turtle.right(90)
        turtle.forward(length)
        turtle.right(90)
        turtle.forward(height)

draw_rectangle(-length * ratio // 2, height * ratio // 2, length * ratio, height * ratio, "#005583")
draw_rectangle(-length * ratio // 2, 2 * ratio // 2, length * ratio, 2 * ratio, "#FFC200")
draw_rectangle(-length * ratio // 2 + 5 * ratio, height * ratio // 2, 2 * ratio, height * ratio, "#FFC200")

turtle.hideturtle()

turtle.mainloop()

请将以上 Python\text{Python} Python 程序保存为 draw_sweden_national_flag.py\text{draw\_sweden\_national\_flag.py} draw_sweden_national_flag.py。使用如下命令可以运行 draw_sweden_national_flag.py\text{draw\_sweden\_national\_flag.py} draw_sweden_national_flag.py

bash 复制代码
python3 draw_sweden_national_flag.py

运行时的效果示意图如下

最终的效果如下

参考资料

相关推荐
Java尧哥学AI6 分钟前
35岁Java程序员学AI Day2:从装环境到写第一行Python,踩了3个坑
python
zhiSiBuYu051718 分钟前
Flask Session 与 Cookie 新手实战指南
后端·python·flask
码云骑士20 分钟前
104-实战论文搜索引擎-ArXiv爬取-Milvus存储-RAG问答-Gradio前端
前端·python·搜索引擎·milvus
比高创意品牌策划设计28 分钟前
零售卖场门头设计怎么做才显眼
python
鬼手点金1 小时前
Scrapy + Playwright 完整示例(JS 动态渲染网页)
开发语言·javascript·爬虫·python·scrapy·html·json
存在morning1 小时前
【Python 开发实践 一】Python vs Go vs Java 三门语言对比
java·python·golang
vx-程序开发1 小时前
springboot旅游推介平台---附源码24175
java·spring boot·python·spring cloud·eclipse·django·idea
心运软件1 小时前
基于深度学习的宝石图像分类系统
人工智能·python·深度学习·机器学习·分类·数据挖掘
一木 之林1 小时前
AI实战 : Numpy图像处理与深度学习框架
python
c_lb72881 小时前
零基础选策略工具,先分清三件事
人工智能·python