当前位置:   article > 正文

python之 turtle好例子集锦_python+turtle+实例

python+turtle+实例

python好之turtle例子集锦

这些程序大多取自或借鉴网上源码,整理在此,便于初学者借鉴,提高学习兴趣。包括:风车动画、小轿车、新年贺卡、动态贺卡、科赫曲线、谢尔宾斯基三角

一、风车动画

效果图如下:

源代码如下:

  1. import turtle as t
  2. t.pensize(2)
  3. t.hideturtle()
  4. windSpeed = 2
  5. radius = 50
  6. def windmill(c):
  7. t.pencolor(c)
  8. t.tracer(False) #隐藏绘制轨迹,之后用update()直接出图
  9. for i in range(4):
  10. t.forward(2*radius)
  11. t.right(90)
  12. t.circle(-radius,180)
  13. while True:
  14. windmill('red')
  15. t.update() #刷新图画
  16. windmill('white') #在此用背景色白色重绘风车,以此清除之前的黑色轨迹
  17. t.right(windSpeed)
  18. t.done()

二、小轿车

源码如下:

  1. import turtle as t
  2. #车身
  3. t.penup()
  4. t.goto(-130,0)
  5. t.pencolor("red")
  6. t.pendown()
  7. t.goto(-75,0)
  8. t.penup()
  9. t.goto(-25,0)
  10. t.pendown()
  11. t.pencolor("red")
  12. t.goto(50,0)
  13. t.penup()
  14. t.goto(100,0)
  15. t.pendown()
  16. t.pencolor("red")
  17. t.goto(125,0)
  18. t.color("red")
  19. t.begin_fill()
  20. t.goto(125,40)
  21. t.goto(120,40)
  22. t.goto(65,70)
  23. t.goto(-40,70)
  24. t.goto(-75,40)
  25. t.goto(-105,40)
  26. t.goto(-130,0)
  27. t.end_fill()
  28. #轮
  29. t.penup()
  30. t.goto(-50,-25)
  31. t.color("black")
  32. t.begin_fill()
  33. t.pendown()
  34. t.circle(25)
  35. t.end_fill()
  36. t.penup()
  37. t.goto(75,-25)
  38. t.color("black")
  39. t.begin_fill()
  40. t.pendown()
  41. t.circle(25)
  42. t.end_fill()
  43. #门
  44. t.penup()
  45. t.pensize(4)
  46. t.goto(-40,65)
  47. t.pendown()
  48. t.pencolor("black")
  49. t.color("blue")
  50. t.begin_fill()
  51. t.goto(-65,40)
  52. t.goto(65,40)
  53. t.goto(65,65)
  54. t.goto(-40,65)
  55. t.end_fill()
  56. t.penup()
  57. t.goto(15,65)
  58. t.pendown()
  59. t.pencolor("black")
  60. t.goto(15,5)
  61. t.penup()
  62. t.goto(-65,40)
  63. t.pendown()
  64. t.goto(-65,5)
  65. t.goto(15,5)
  66. t.goto(65,5)
  67. t.goto(65,40)
  68. t.penup()
  69. t.goto(0,30)
  70. t.pendown()
  71. t.goto(30,30)
  72. t.pensize(2)
  73. #标志
  74. t.penup()
  75. t.goto(-105,40)
  76. t.pencolor("black")
  77. t.pendown()
  78. t.goto(-105,5)
  79. t.goto(-125,5)
  80. t.goto(-110,5)
  81. t.goto(-110,30)
  82. t.penup()
  83. t.goto(-115,5)
  84. t.pendown()
  85. t.goto(-115,23)
  86. t.penup()
  87. t.goto(126,20)
  88. t.color("black")
  89. t.begin_fill()
  90. t.pendown()
  91. t.goto(100,20)
  92. t.goto(100,0)
  93. t.goto(126,0)
  94. t.goto(126,20)
  95. t.end_fill()
  96. #灯
  97. t.penup()
  98. t.goto(-100,16)
  99. t.pendown()
  100. t.color("orange")
  101. t.begin_fill()
  102. t.circle(12,-180)
  103. t.goto(-100,16)
  104. t.end_fill()
  105. t.penup()
  106. t.goto(110,43)
  107. t.pendown()
  108. t.color("orange")
  109. t.begin_fill()
  110. t.circle(11,-180)
  111. t.goto(110,43)
  112. t.end_fill()
  113. t.hideturtle()
  114. t.done()

三、新年贺卡

效果图如下:

 源代码如下:

  1. import turtle as t
  2. t.color("red")
  3. t.write("万\n事\n大\n吉", font=("华文行楷", 45, "normal"))
  4. t.penup()
  5. t.goto(80,0)
  6. t.pendown()
  7. t.write("新\n年\n快\n乐", font=("华文行楷", 45, "normal"))
  8. t.penup()
  9. t.goto(-120,110)
  10. t.pendown()
  11. t.write("李\n一\n民\n贺", font=("方正舒体", 20, "normal"))
  12. t.penup()
  13. t.goto(-130,0)
  14. t.color("red")
  15. t.pendown()
  16. t.begin_fill()
  17. t.forward(55)
  18. t.left(90)
  19. t.forward(55)
  20. t.left(90)
  21. t.forward(55)
  22. t.left(90)
  23. t.forward(55)
  24. t.left(90)
  25. t.end_fill()
  26. t.color("white")
  27. t.penup()
  28. t.goto(-100,0)
  29. t.pendown()
  30. t.write("万\n事", font=("华文隶书", 20, "normal"))
  31. t.penup()
  32. t.goto(-130,0)
  33. t.pendown()
  34. t.write("如\n意", font=("华文隶书", 20, "normal"))
  35. t.done()

四、动态贺卡

效果图

源码如下

  1. import turtle as T
  2. import random
  3. import time
  4. t = T.Turtle()
  5. w = T.Screen()
  6. t.hideturtle()
  7. t.getscreen().tracer(5, 0)
  8. w.screensize(bg='maroon')
  9. t.left(90)
  10. t.up()
  11. t.forward(280)
  12. t.down()
  13. t.pensize(3)
  14. n=100
  15. t.color("orange","yellow")
  16. t.begin_fill()
  17. t.left(126)
  18. for i in range(5):
  19. t.forward(n/5)
  20. t.right(144)
  21. t.forward(n/5)
  22. t.left(71)
  23. t.end_fill()
  24. t.left(60)
  25. t.pensize(8)
  26. t.forward(60)
  27. t.right(20)
  28. t.right(116)
  29. t.pensize(6)
  30. t.color('dark green')
  31. n=130
  32. for i in range(6):
  33. time.sleep(0.5)
  34. a=1+i/2
  35. t.begin_fill()
  36. t.left(90)
  37. t.forward(n*a*0.707)
  38. t.left(135)
  39. t.forward(n*a)
  40. t.left(135)
  41. t.forward(n*a*0.707)
  42. t.end_fill()
  43. t.up()
  44. t.left(90)
  45. t.forward(n*a*0.707/3)
  46. t.left(135)
  47. t.forward(n*a/6)
  48. t.left(135)
  49. t.down()
  50. t.up()
  51. t.right(135)
  52. t.forward(30)
  53. t.right(90)
  54. t.forward(157)
  55. t.down()
  56. t.color('saddlebrown')
  57. t.begin_fill()
  58. t.forward(80)
  59. t.right(90)
  60. t.forward(45)
  61. t.right(90)
  62. t.forward(80)
  63. t.right(90)
  64. t.forward(45)
  65. t.end_fill()
  66. t.up()
  67. t.backward(45)
  68. t.right(90)
  69. t.backward(470)
  70. t.down()
  71. def light(l,t):
  72. t.pensize(3)
  73. colors = ["magenta","darkorange","red","blue"]
  74. for i in range(l):
  75. time.sleep(0.2)
  76. b = 70+16*i
  77. a = b/2*random.randint(-100,100)/100
  78. t.up()
  79. t.forward(b)
  80. t.left(90)
  81. t.forward(a)
  82. t.down()
  83. t.color("lightyellow",colors[i%4])
  84. t.begin_fill()
  85. t.circle(10)
  86. t.end_fill()
  87. t.up()
  88. t.backward(a)
  89. t.right(90)
  90. t.backward(b)
  91. t.down()
  92. t.pensize(1)
  93. def snow(m,t):
  94. for i in range(m):
  95. a = 400 - 800 * random.random()
  96. b = 600 - 800 * random.random()
  97. t.up()
  98. t.forward(b)
  99. t.left(90)
  100. t.forward(a)
  101. t.down()
  102. t.color('white')
  103. t.begin_fill()
  104. t.circle(1)
  105. t.end_fill()
  106. t.up()
  107. t.backward(a)
  108. t.right(90)
  109. t.backward(b)
  110. light(24,t)
  111. snow(600, t)
  112. t.goto(-200,200)
  113. my_word = ("Merry Christmas")
  114. t.write(my_word,font=("Edwardian Script ITC",40,"bold"))
  115. time.sleep(0.3)
  116. t.goto(-100,50)
  117. my_word = ("and")
  118. t.write(my_word,font=("Edwardian Script ITC",50,"bold"))
  119. time.sleep(0.3)
  120. t.goto(-150,-100)
  121. my_word = ("Happy New Year")
  122. t.write(my_word,font=("Edwardian Script ITC",40,"bold"))

五、科赫曲线

科赫曲线是一个典型的分形曲线,可以通过不断迭代生成。图形都具有自相似性和无限细节的特点。

效果图

源码如下:

  1. import turtle
  2. # 科赫曲线
  3. def koch_snowflake(length, depth, t):
  4. if depth == 0:
  5. t.forward(length)
  6. else:
  7. length /= 3.0
  8. koch_snowflake(length, depth-1, t)
  9. t.left(60)
  10. koch_snowflake(length, depth-1, t)
  11. t.right(120)
  12. koch_snowflake(length, depth-1, t)
  13. t.left(60)
  14. koch_snowflake(length, depth-1, t)
  15. def draw_koch_snowflake():
  16. screen = turtle.Screen()
  17. screen.setup(600, 600)
  18. screen.tracer(0)
  19. snowflake = turtle.Turtle()
  20. snowflake.hideturtle()
  21. snowflake.speed(0)
  22. snowflake.pu()
  23. snowflake.goto(-150, 100)
  24. snowflake.pd()
  25. snowflake.color("blue")
  26. for _ in range(3):
  27. koch_snowflake(300, 4, snowflake)
  28. snowflake.right(120)
  29. screen.update()
  30. turtle.done()
  31. draw_koch_snowflake()

六、谢尔宾斯基三角

谢尔宾斯基三角也是一个著名的分形图形,由等边三角形不断分割而成。图形都具有自相似性和无限细节的特点。

效果图:

源码如下:

  1. import turtle
  2. # 谢尔宾斯基三角
  3. def sierpinski_triangle(vertices, level):
  4. x1, y1 = vertices[0]
  5. x2, y2 = vertices[1]
  6. x3, y3 = vertices[2]
  7. if level == 0:
  8. turtle.penup()
  9. turtle.goto(x1, y1)
  10. turtle.pendown()
  11. turtle.begin_fill()
  12. turtle.goto(x2, y2)
  13. turtle.goto(x3, y3)
  14. turtle.goto(x1, y1)
  15. turtle.end_fill()
  16. else:
  17. x12 = (x1 + x2) / 2
  18. y12 = (y1 + y2) / 2
  19. x23 = (x2 + x3) / 2
  20. y23 = (y2 + y3) / 2
  21. x31 = (x3 + x1) / 2
  22. y31 = (y3 + y1) / 2
  23. sierpinski_triangle([(x1, y1), (x12, y12), (x31, y31)], level - 1)
  24. sierpinski_triangle([(x12, y12), (x2, y2), (x23, y23)], level - 1)
  25. sierpinski_triangle([(x31, y31), (x23, y23), (x3, y3)], level - 1)
  26. turtle.speed(0)
  27. turtle.penup()
  28. turtle.goto(-300, -250)
  29. turtle.pendown()
  30. sierpinski_triangle([(-300, 250), (300, 250), (0, -250)], 5)
  31. turtle.hideturtle()
  32. turtle.done()

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

闽ICP备14008679号