fork(2) download
  1. import turtle
  2. turtle.tracer(1, 0)
  3.  
  4. def createLSystem(numIters,axiom):
  5. startString = axiom
  6. endString = ""
  7. for i in range(numIters):
  8. endString = processString(startString)
  9. startString = endString
  10.  
  11. return endString
  12.  
  13. def processString(oldStr):
  14. newstr = ""
  15. for ch in oldStr:
  16. newstr = newstr + applyRules(ch)
  17.  
  18. return newstr
  19.  
  20. def applyRules(ch): #ПРАВИЛА ИТЕРАЦИИ
  21. newstr = ""
  22. if ch == 'F':
  23. newstr = '-F+F+[+F-F-]-[-F+F+F]' # Rule 1
  24. elif ch == 'X':
  25. newstr = 'X+YF+' # Rule 2
  26. elif ch =='Y':
  27. newstr='-FX-Y'
  28. else:
  29. newstr = ch # no rules apply so keep the character
  30.  
  31. return newstr
  32.  
  33. stack=[] #СПИСОК КООРДИНАТ ЧЕРЕПУХИ
  34.  
  35. def drawLsystem(aTurtle, instructions, angle, distance): #ЧТЕНИЕ ПРАВИЛ ЧЕРЕПАХОЙ
  36. for cmd in instructions:
  37. if cmd == 'F':
  38. aTurtle.forward(distance)
  39. elif cmd == 'B':
  40. aTurtle.backward(distance)
  41. elif cmd == '+':
  42. aTurtle.right(angle)
  43. elif cmd == '-':
  44. aTurtle.left(angle)
  45. elif cmd == '[': #НЕ РОБИТ МРАЗЬ
  46. stack.append(turtle.xcor())
  47. stack.append(turtle.ycor())
  48. stack.append(turtle.heading())
  49. elif cmd == ']':
  50. x=float(stack.pop(-3))
  51. y=float(stack.pop(-2))
  52. a = float(stack.pop(-1))
  53. turtle.setx(x)
  54. turtle.sety(y)
  55. turtle.setheading(a)
  56.  
  57. def main():
  58. inst = createLSystem(7, "F") # create the string
  59. print(inst)
  60. t = turtle.Turtle() # create the turtle
  61. wn = turtle.Screen()
  62.  
  63. t.up()
  64. t.back(200)
  65. t.down()
  66. t.speed(0)
  67. drawLsystem(t, inst, 22, 3) # draw the picture
  68. # angle 60, segment length 5
  69. wn.exitonclick()
  70.  
  71. main()
  72.  
Runtime error #stdin #stdout #stderr 0.17s 23876KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "./prog.py", line 1, in <module>
  File "/usr/lib/python3.7/turtle.py", line 107, in <module>
    import tkinter as TK
ModuleNotFoundError: No module named 'tkinter'