70周年使用turtle画国旗表白祖国

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_44205272/article/details/101797681

turtle画国旗表白祖国

下面贴出代码

import turtle
class HomeLand():
    def I(self):
        turtle.setup(800,400)    
        turtle.screensize(800, 400)   # 画布大小
        turtle.speed(1)     # 设置速度
        turtle.pencolor("black")
        turtle.pensize(10)    # 设置画笔大小
        turtle.penup()      # 落笔
        turtle.goto(-350,100)
        turtle.pendown()
        turtle.goto(-250,100)
        turtle.penup()
        turtle.goto(-300,100)
        turtle.pendown()
        turtle.goto(-300,-100)
        turtle.penup()
        turtle.goto(-350,-100)
        turtle.pendown()
        turtle.goto(-250,-100)


    def Heart(self):
        def love():
            for i in range(200):
                    turtle.right(1)
                    turtle.forward(1)

        turtle.penup()
        turtle.goto(-85, -80)
        turtle.pencolor('red')   # 画笔颜色
        turtle.fillcolor("red")   # 填充颜色
        turtle.speed(15)
        turtle.penup()
        turtle.begin_fill()
        turtle.pensize(4)
        turtle.pendown()
        turtle.left(140)
        turtle.forward(111.65)
        love()
        turtle.left(120)
        love()
        turtle.forward(111.65)
        turtle.end_fill()


    def NationalFlag(self):
        turtle.home()   
        turtle.up()
        turtle.goto(80, 100)
        turtle.down()
        turtle.begin_fill()
        turtle.fillcolor("red")
        turtle.pencolor("red")
        for i in range(2):
            turtle.forward(280)
            turtle.right(90)
            turtle.forward(200)
            turtle.right(90)
        turtle.end_fill()

        turtle.up()
        turtle.goto(110, 45)
        turtle.down()
        turtle.begin_fill()
        turtle.fillcolor("yellow")
        turtle.pencolor("yellow")
        for x in range(5):
            turtle.forward(50)
            turtle.right(144)
        turtle.end_fill()

        turtle.up()
        turtle.goto(180, 80)
        turtle.down()
        turtle.begin_fill()
        turtle.fillcolor("yellow")
        turtle.pencolor("yellow")
        for x in range(5):
            turtle.forward(20)
            turtle.right(144)
        turtle.end_fill()

        turtle.up()
        turtle.goto(210, 60)
        turtle.down()
        turtle.begin_fill()
        turtle.fillcolor("yellow")
        turtle.pencolor("yellow")
        for x in range(5):
            turtle.forward(20)
            turtle.right(144)
        turtle.end_fill()

        turtle.up()
        turtle.goto(210, 20)
        turtle.down()
        turtle.begin_fill()
        turtle.fillcolor("yellow")
        turtle.pencolor("yellow")
        for x in range(5):
            turtle.forward(20)
            turtle.right(144)
        turtle.end_fill()

        turtle.up()
        turtle.goto(180, 0)
        turtle.down()
        turtle.begin_fill()
        turtle.fillcolor("yellow")
        turtle.pencolor("yellow")
        for x in range(5):
            turtle.forward(20)
            turtle.right(144)
        turtle.end_fill()
        turtle.hideturtle()  # 隐藏小海龟
        # 维持面板
        turtle.done()
HL = HomeLand()
HL.I()
HL.Heart()
HL.NationalFlag()

下面是一些turtle的常用技巧

	.Turtle:注意字母的大写,用于生成一个 turtle 对象
    .fd/forward:沿当前方向,画笔绘制距离
    .bk/backward:沿当前反方向,画笔绘制距离
    .lt/left:沿当前方向向左旋转的角度
    .rt/right:沿当前方向向右旋转的角度
    .pu/penup/up:抬笔 pen up
    .pd/pendown/down:落笔 pen down
    .mainloop:保持画布窗口不消失,用在最后
    .mode: "logo",初始位置指向北(上);"standard",初始位置指向东方(右)
    .seth/setheading: 在当前位置上旋转箭头,旋转角度为所设置的角度。
                      若模式为“logo”,则顺时针旋转;若模式为“standard”,则逆时针旋转;
    .screensize/setup: 设置画布大小,及距离电脑屏幕最左上角的位置;
             为小数时表示画布占整个屏幕的比例,为整数时为画布所占的像素数
    .goto/setposition/setpos: 移动到相对于画布中心点的坐标位置(x,y)
                                (画布是一个以初始位置为原点的坐标系)
    .setx/sety:保持一个坐标不变,移动另一个坐标,移动的距离是相对于原点来计算的
    .home: 让画笔回到初始位置(原点),同时绘制
    .reset: 抹去之前所有的痕迹,重新绘画。即,恢复箭头的初始状态
    .clear: 抹去之前所有的痕迹,但是保持箭头现有状态
    .circle:一个输入,画圆;两个输入,画弧长;三个输入,画正多边形
    .pensize: 设置画笔大小
    .speed:设置画笔移动速度, 注意 0 为最快速度
    .undo:撤销上一次操作
    .undobufferentries:返回可撤销的所有步骤,可跟 while 条件语句联用
    .stamp: 复制一次当前箭头状态,并对复制的箭头进行操作
    .clearstamp: 删除最后一次复制的箭头
    .clearstamps: 为空,删除所有复制箭头;
                  为 0,不删除;为正,删除前面的个数;为负,删除后面的个数;
    .heading: 返回当前简单旋转的角度,
              若为“logo”,则顺时针旋转;若为“standard”,则逆时针旋转;
    .xcor/ycor: 返回当前箭头所处位置的,横、纵坐标
    .distance: 返回当前箭头所处位置与此函数设置的位置间的直线距离
    .isdown: 判断画笔是否落下
    .filling: 判断当前状态是否处于填充状态
    .fillcolor: 设置要填充的颜色
    .color(p1, p2):p1,画笔整体颜色; p2,由画笔画出的图形的填充颜色
    .colormode(cmode=None): cmode=1.0,RGB为小数;cmode=255,RGB为0-255整数
    .pen(pencolor=p1, fillcolor=p2 ):  pencolor=p1, 设置画笔轮廓颜色;
                                        fillcolor=p2,设置画笔填充色
    .begin_fill/end_fill: 在填充颜色的过程中要调用这两个函数
    .write: 绘制文本
    .shape:用于设置箭头形状;“arrow”粗大形1,“triangle”粗大形2
            “turtle”海龟形,“circle”原形,“square”方形,“classic”默认
    .shapesize:设置箭头的大小
    .resizemode: 设置箭头使用模式
    .tilt: 保持当前箭头移动方向,然后选择箭头(只是旋转箭头,而没有旋转移动的方向)
    .shearfactor: >0, 向右下方向窄长变化;反之亦然
    .shapetransform:四个参数:左右拉伸,上半部分左右变化,下半部分左右变化,上下拉伸
                    (加负号,会有 180 度翻转加持;四个参数均为数字)
    .hideturtle/ht:隐藏箭头
    .showturtle/st:显示箭头
    .isvisible: 判断箭头是否隐藏
    .onclick: 对鼠标左键按下时进行事件触发(触发事件应包装在一个函数体内)
    .onrelease: 对鼠标左键释放时进行事件触发(触发事件应包装在一个函数体内)
    .ondrag: 对鼠标左键按下同时拖动的情况下进行事件触发(触发事件应包装在一个函数体内)
    .begin_poly:开始记录绘制的多边形顶点的位置,一开始出发的位置也算作多边形的一个顶点
    .end_poly: 结束多边形顶点位置的记录
    .get_poly: 获取已经记录下来的多边形顶点位置,并以元组的形式返回
    .register_shape:将绘制好的图形添加到 TurtleScreen 对象的 shapelist 中,作为箭头使用
                添加完成后可以直接用 shape 进行调用,通常与begin_poly、end_poly、get_poly一起使用
    .clone: 在当前位置复制一个当前形式的箭头,对已经生成的turtle对象进行操作
    .getturtle/getpen:获取原始箭头作为当前操作对象进行操作(相对于复制的箭头来说)
    .getscreen:获取画布对象,对画布进行操作
    .bgcolor: 设置画布背景颜色,使用之前要使用 .getscreen
    .bgpic: 将图片放到画布中,使用之前要使用 .getscreen
    .screensize(canvwidth=800, canvheight=600, bg='orange'):设置画布大小和背景颜色
    .tracer:将要执行的事件放置在 .tracer(False) 和 .tracer(True) 之间,
                这样就可以更新要执行的事件
    .onkey/onkeyrelease(function, key):按下按键并释放,来触发事件
                                        function 是无参函数,里面是要执行的事件;key 是自已设置的用于触发事件的按键
    .onkeypress(function, key):按下按键不释放,来触发事件
                                function 是无参函数,里面是要执行的事件;key 是自已设置的用于触发事件的按键
    .onclick(function):鼠标左键点击来触发事件,同时在 function 执行事件的 最后两行 要有 解绑后再绑定 俩步操作
                        function(x , y)是有参函数, 进入的 x,y 是鼠标点击的位置
    .textinput(title, prompt): 弹出一个输入文本的弹出框,点击Cancel取消按钮则返回None,点击Ok按钮返回输入的字符串
                                title – 弹框标题(一个string字符串)
                                prompt – 弹框提示(一个string字符串)
    .numinput(title, prompt, default=None, minval=None, maxval=None):
                弹出一个输入数字的弹出框,点击Cancel取消按钮则返回None,点击Ok按钮返回输入的number
                title – 弹框标题(一个string字符串)
                prompt – 弹框提示(一个string字符串)
                default – 默认值number类型 (可选)
                minval – 最小值number类型 (可选)
                maxval – 最大值number类型 (可选)
    .window_height/.window_width: 返回画布的高度/宽度

猜你喜欢

转载自blog.csdn.net/qq_44205272/article/details/101797681