脚本专栏 
首页 > 脚本专栏 > 浏览文章

Python使用Turtle模块绘制国旗的方法示例

(编辑:jimmy 日期: 2024/5/9 浏览:3 次 )

turtle模块

turtle模块:python内置的绘图工具

turtle(海龟)模块,我们是用它来进行画图的,基本上就是画简单的直线,点,和曲线。

你可以把它想成一个小海龟,在沙滩上行走,然后留下的各种痕迹,使用Turtle模块可以绘制很多精美的图形。

基本操作(Turtle方法)

  • turtle.forward(step):前进step个像素
  • turtle.back(step):后退step个像素
  • turtle.right():右转一个角度
  • turtle.left():左转一个角度
  • turtle.pencolor(“string”):画笔颜色
  • turtle.fillcolor(“string”):填充颜色
  • turtle.speed(int):运动速度

其他的turtle方法可以参见python官网

https://docs.python.org/zh-cn/3.7/library/turtle.html

具体代码实现

# 绘画
# 中国国旗
# 转载请标明出处!!


import turtle
import time


def draw__stars(tur, step, x, y, arg):
 """
 绘制五角星
 :param tur: turtle object
 :param step: 五角星一条边的长度
 :param x: 开始绘制五角星的起点x坐标
 :param y: 开始绘制五角星的起点y坐标
 :param arg:
 :return:
 """
 tur.pencolor('yellow')
 tur.fillcolor('yellow')
 tur.up()
 tur.goto(x, y)
 tur.begin_fill()
 tur.down()
 tur.right(arg)
 tur.forward(step)
 tur.right(144)
 tur.forward(step)
 tur.right(144)
 tur.forward(step)
 tur.right(144)
 tur.forward(step)
 tur.right(144)
 tur.forward(step)
 tur.right(144)
 tur.end_fill()


def draw__flag(tur, wide, height):
 """
 绘制国旗的长方形形状
 :param tur: turtle object
 :param wide: the width of the flag
 :param height: the height of the flag
 :return: None
 """
 tur.pencolor('red')
 tur.fillcolor('red')
 tur.goto(- wide / 2, height / 2)
 tur.begin_fill()
 tur.forward(wide)
 tur.right(90)
 tur.forward(height)
 tur.right(90)
 tur.forward(wide)
 tur.right(90)
 tur.forward(height)
 tur.end_fill()


if __name__ == '__main__':
 """
 main 函数
 """
 # tur = turtle.Turtle()
 turtle.screensize(canvwidth=3000, canvheight=2000, bg=None)
 # 绘制star的turtle对象
 tur_star = turtle.Turtle()
 # 绘制flag的turtle对象
 tur_flag = turtle.Turtle()
 tur_flag.speed(3)
 tur_star.speed(3)
 # 隐藏turtle对象
 tur_star.hideturtle()
 tur_flag.hideturtle()
 # 间隔3秒,可以没有,这个是我调试时加上去的
 time.sleep(3)
 # 绘制长方形
 draw__flag(tur_flag, 630, 420)
 # 绘制五角星,在合适的位置进行绘制五角星
 # 调用五次函数绘制五颗五角星
 draw__stars(tur_star, step=60, x=-280, y=155, arg=0)
 draw__stars(tur_star, step=25, x=-182, y=177, arg=- 25)
 draw__stars(tur_star, step=25, x=-175, y=125, arg=41)
 draw__stars(tur_star, step=25, x=-208, y=79, arg=23)
 draw__stars(tur_star, step=25, x=-265, y=75, arg=48)
 # 使画面锁定
 turtle.done()


运行结果

Python使用Turtle模块绘制国旗的方法示例

总结

上一篇:Python的collections模块真的很好用
下一篇:Python  Asyncio模块实现的生产消费者模型的方法
一句话新闻
高通与谷歌联手!首款骁龙PC优化Chrome浏览器发布
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网