美文网首页
2021-03-20—Python中的乐趣

2021-03-20—Python中的乐趣

作者: 小蓉子五一 | 来源:发表于2021-03-20 00:18 被阅读0次

    文/小蓉子

    今天分享一点自己学习的Python中的一点点小乐趣:

    for i in range(1, 10):

        for j in range(1, i+1):

            print('{}x{}={}\t'.format(j, i, i*j), end='')

        print()

    源码 结果

    通过turtle海龟绘图实现蟒蛇的绘制

    import turtle #引入turtle绘制图像库

    turtle.setup(650,350,200,200) #设定了一个宽650像素和高350像素的窗体,其位置左上角坐标是200,200

    turtle.penup() #抬起画笔

    turtle.fd(-250) #画笔向前运动-250像素

    turtle.pendown() #落下画笔,此时画笔在画布上显示

    turtle.pensize(25) #设置画笔的宽度为25像素

    turtle.pencolor('purple') #设置画笔的颜色

    turtle.seth(-40) #控制画笔转向,朝绝对的负40°方向上,只改变方向,不改变行进

    for i in range(4): #循环语句,设置循环次数

        turtle.circle(40,80) #以40像素为半径,圆心为左侧顺时针绘制80°的弧度

        turtle.circle(-40,80) #以40像素为半径,圆心为右侧顺时针绘制80°的弧度

    turtle.circle(40,80/2) #以40像素为半径,圆心为左侧顺时针绘制40°的弧度

    turtle.fd(40) #向前绘制40像素

    turtle.circle(16,180) #以16像素为半径,圆心为左侧顺时针绘制180°的弧度

    turtle.fd(40*2/3) #向前行进40*2/3像素

    turtle.done() #用turtle.done()函数,程序运行之后不会自动退出

    代码 结果

    from turtle import Screen, Turtle

    t=Turtle()

    s=Screen()

    leng, heig = 33,99

    ge = 5

    t.hideturtle()

    t.speed(8)

    t.pensize(2)

    def bai():

        for _ in range(2):

            t.fd(leng)

            t.left(90)

            t.fd(heig)

            t.left(90)

    def hei():

        t.begin_fill()

        for _ in range(2):

            t.fd(int(leng//3*2))

            t.left(90)

            t.fd(int (heig // 2))

            t.left(90)

        t.fillcolor('black')

        t.end_fill()

    def draw(n,color,pos=(0,0)):

        x,y = pos

        if color == 'b':

            x += int(leng // 3 * 2)

            y += int(heig // 2)

        for _ in range(n):

            t.up()

            t.setpos(x,y)

            t.down()

            if color == 'w':

                bai()

            elif color =='b':

                hei()

            x += leng

    draw(3,'w')

    draw(2,'b')

    draw(4,'w',pos=(leng * 3 +ge,0))

    draw(3,'b',pos=(leng * 3 +ge,0))

    s.exitonclick()

    示意图

    相关文章

      网友评论

          本文标题:2021-03-20—Python中的乐趣

          本文链接:https://www.haomeiwen.com/subject/dydicltx.html