当前位置:   article > 正文

turtle库制作简单动画和总结_python简单turtle动画

python简单turtle动画

turtle制作简单动画和总结

最后制作的效果图
请添加图片描述
制作的代码,写的比较乱,后面还有皮卡丘动画,有需要可以私我。

def tree(n,l):
  pd()#下笔
  #阴影效果
  t = cos(radians(heading()+45))/8+0.25
  pencolor(t,t,t)
  pensize(n/2)
  forward(l)#画树枝
  if n>0:

    b = random()*15+10 #右分支偏转角度
    c = random()*15+10 #左分支偏转角度
    d = l*(random()*0.25+0.7) #下一个分支的长度
    #右转一定角度,画右分支
    right(b)
    tree(n-1,d)
    #左转一定角度,画左分支
    left(b+c)
    tree(n-1,d)
    #转回来
    right(c)
  else:
    #画叶子
    right(90)
    n=cos(radians(heading()-45))/4+0.5
    begin_fill()
    pencolor(n, n * 0.8, n * 0.8)
    color(n, n * 0.8, n * 0.8)
    circle(4)
    end_fill()
    left(90)
    #添加0.3倍的飘落叶子
    if(random()>0.7):
      pu()
      #飘落
      t = heading()
      an = 240+random()*40
      setheading(an)
      dis = int(800*random()*0.5 + 400*random()*0.3 + 200*random()*0.2)
      forward(dis)
      setheading(t)
      #画叶子
      pd()
      right(90)
      n = cos(radians(heading()-45))/4+0.5
      pencolor(n*0.5+0.5,0.4+n*0.4,0.4+n*0.4)
      circle(2)
      left(90)
      pu()
      #返回
      t=heading()
      setheading(an)
      backward(dis)
      setheading(t)
  pu()
  backward(l)#退回
def petal(m):  # 树下花瓣
    for i in range(m):
      n = cos(radians(heading() - 45)) / 4 + 0.5
      a = 600- 1200 * random()
      b = 30 - 60 * random()
      up()
      forward(b)
      left(90)
      forward(a)
      down()
      # 淡珊瑚色
      color(n, n * 0.8, n * 0.8)
      circle(2)
      up()
      backward(a)
      right(90)
      backward(b)
def getPosition(x, y):
    t.setx(x)
    t.sety(y)
    print(x, y)

if __name__ == '__main__':
    bgcolor(0.5, 0.5, 0.5)  # 背景色
    screensize(850, 800)
    t.setup(850, 800)
    ht()  # 隐藏turtle
    speed(0)  # 速度 1-10渐进,0 最快
    tracer(1, 0)
    pu()  # 抬笔
    backward(100)
    left(90)  # 左转90pu()  # 抬笔
    getPosition(0, -300)
    tree(12, 100)  # 递归7petal(1000)
    width, height = 800, 600
    #转换背景
    t.bgcolor("black")
    t.delay(0)  # 这里要设为0,否则很卡
    t = Turtle(visible=False, shape='circle')
    print('画背景')
    t.pencolor("white")
    t.fillcolor("white")
    t.penup()
    t.setheading(-90)
    t.goto(width / 2, randint(-height / 2, height / 2))
    stars = []
    for i in range(200):
        star = t.clone()
        s = random() / 3
        star.shapesize(s, s)
        star.speed(int(s * 10))
        star.sety(width / 2 + randint(1, width))
        star.setx(randint(-height / 2, height / 2))
        star.showturtle()
        stars.append(star)
    for i in range(1, 100):
        for star in stars:
            star.sety(star.ycor() - 3 * star.speed())
            if star.xcor() < -width / 2:
                star.hideturtle()
                star.sety(width / 2 + randint(1, width))
                star.setx(randint(-height / 2, height / 2))
                star.showturtle()
    clearscreen()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121

总结

1.可修复性
编程所得的画面,所有的图案都是可修改可拷贝的,且不会有任何影响。而手绘的画作,一旦落笔,可修复性就很差了,不免会出现“一个墨点毁了一幅画”的情况。
2.顺序性
码绘可以选择先绘制任何图形,再通过移动代码的位置改变它们的遮挡关系。而在手绘的过程中,不得不先总体确定有哪些图案先绘制,或需要保留完整性,以及剩余图案的分布和位置。
3.素材选择性
以颜色为例,RGB的三个数值,囊括了所有可视颜色。而在手绘的颜色工具相比之下则少得多。
4.重复性
对于有规律可循的图形,编程绘画便很有优势,恰当地使用循环、嵌套等语句,简单的一些函数便可以减少很多工作量。相比而言手绘则更加按部就班,且绘制重复图案时不可避免会有微小的差异。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/874157?site
推荐阅读
相关标签
  

闽ICP备14008679号