fork download
  1. #!/usr/bin/env python3
  2.  
  3. from turtle import Shape, Turtle, mainloop, Vec2D as Vec
  4. from time import sleep
  5.  
  6. G = 9
  7.  
  8. class GravSys(object):
  9. def __init__(self):
  10. self.planets = []
  11. self.t = 0
  12. self.dt = 0.01
  13. def init(self):
  14. for p in self.planets:
  15. p.init()
  16. def start(self):
  17. for i in range(10000):
  18. self.t += self.dt
  19. for p in self.planets:
  20. p.step()
  21.  
  22. class Star(Turtle):
  23. def __init__(self, m, x, v, gravSys, shape):
  24. Turtle.__init__(self, shape=shape)
  25. self.penup()
  26. self.m = m
  27. self.setpos(x)
  28. self.v = v
  29. gravSys.planets.append(self)
  30. self.gravSys = gravSys
  31. self.resizemode("user")
  32. self.pendown()
  33. def init(self):
  34. dt = self.gravSys.dt
  35. self.a = self.acc()
  36. self.v = self.v + 0.5*dt*self.a
  37. def acc(self):
  38. a = Vec(0,0)
  39. for planet in self.gravSys.planets:
  40. if planet != self:
  41. v = planet.pos()-self.pos()
  42. a += (G*planet.m/abs(v)**3)*v
  43. return a
  44. def step(self):
  45. dt = self.gravSys.dt
  46. self.setpos(self.pos() + dt*self.v)
  47. if self.gravSys.planets.index(self) != 0:
  48. self.setheading(self.towards(self.gravSys.planets[0]))
  49. self.a = self.acc()
  50. self.v = self.v + dt*self.a
  51.  
  52.  
  53. ## create compound yellow/blue turtleshape for planets
  54. def main():
  55. s = Turtle()
  56. s.reset()
  57. s.getscreen().tracer(0,0)
  58. s.ht()
  59. s.pu()
  60. s.fd(6)
  61. s.lt(90)
  62. s.begin_poly()
  63. s.circle(6, 180)
  64. s.end_poly()
  65. m1 = s.get_poly()
  66. s.begin_poly()
  67. s.circle(6,180)
  68. s.end_poly()
  69. m2 = s.get_poly()
  70.  
  71. planetshape = Shape("compound")
  72. planetshape.addcomponent(m1,"orange")
  73. planetshape.addcomponent(m2,"blue")
  74. s.getscreen().register_shape("planet", planetshape)
  75. s.getscreen().tracer(1,0)
  76.  
  77. ## setup gravitational system
  78. gs = GravSys()
  79. sun = Star(1000000, Vec(0,0), Vec(0,-2.5), gs, "circle")
  80. sun.color("yellow")
  81. sun.shapesize(1.8)
  82. sun.pu()
  83. earth = Star(12500, Vec(210,0), Vec(0,195), gs, "planet")
  84. earth.pencolor("green")
  85. earth.shapesize(0.8)
  86. moon = Star(1, Vec(220,0), Vec(0,295), gs, "planet")
  87. moon.pencolor("blue")
  88. moon.shapesize(0.5)
  89. gs.init()
  90. gs.start()
  91. return "Done!"
  92.  
  93.  
  94. if __name__ == '__main__':
  95. main()
  96. mainloop()
  97.  
Runtime error #stdin #stdout #stderr 0.04s 22352KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "./prog.py", line 96, in <module>
  File "./prog.py", line 55, in main
  File "/usr/lib/python3.4/turtle.py", line 3812, in __init__
    Turtle._screen = Screen()
  File "/usr/lib/python3.4/turtle.py", line 3662, in Screen
    Turtle._screen = _Screen()
  File "/usr/lib/python3.4/turtle.py", line 3678, in __init__
    _Screen._root = self._root = _Root()
  File "/usr/lib/python3.4/turtle.py", line 434, in __init__
    TK.Tk.__init__(self)
  File "/usr/lib/python3.4/tkinter/__init__.py", line 1859, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: Can't find a usable init.tcl in the following directories: 
    /usr/share/tcltk/tcl8.6 /usr/lib/tcl8.6 /lib/tcl8.6 /usr/library /library /tcl8.6.4/library /tcl8.6.4/library



This probably means that Tcl wasn't installed properly.