Advertisement
here2share

# tk_procedural_slither2.py

Aug 11th, 2023
1,203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.35 KB | None | 0 0
  1. # tk_procedural_slither2.py
  2.  
  3. from tkinter import *
  4. import math
  5.  
  6. ww = 800
  7. hh = 600
  8. ww0 = ww//2
  9. hh0 = hh//2
  10. L = 400 # snake length by pixel
  11. direction = [0, 0, 0, 0]  # initial direction
  12. degrees = math.radians(359.9999999999)
  13. print(degrees)
  14.  
  15. root = Tk()
  16. root.title("tk procedural slither")
  17. root.geometry("%dx%d+10+10"%(ww,hh))
  18. canv = Canvas(root, width=ww, height=hh)
  19. canv.pack()
  20.  
  21. points = []
  22. angles = [angle*0.0002 for angle in range(100, 401)]
  23. angles += [-angle*0.0002 for angle in range(100, 401)]
  24.  
  25.  
  26. def movement():
  27.     x = int(ww0 + math.cos(direction[0]) * (math.cos(direction[1]) * (ww0 - 15)))
  28.     y = int(hh0 + math.sin(direction[2]) * (math.sin(direction[3]) * (hh0 - 15)))
  29.     return (x, y)
  30.  
  31. p = {}
  32. point = movement()
  33. points.append(point)
  34. for i in range(L):
  35.     point = movement()
  36.     points.append(point)
  37.     p[i] = canv.create_line(points, fill="blue", width=3, smooth=True, splinesteps=5)
  38.  
  39. i = 0
  40. while True:
  41.     canv.delete(p[i])
  42.     points.append(movement())
  43.     points = points[-L:]
  44.     angles.extend([angles.pop(0), angles.pop(7), angles.pop(27), angles.pop(79), angles.pop(101), angles.pop(57)])
  45.     angles.insert(11, angles.pop(99))
  46.     direction = [angles[-i] + direction[i] for i in (0, 1, 2, 3)] # update the direction
  47.     p[i] = canv.create_line(points[-2:], fill="blue", width=3, smooth=True, splinesteps=5)
  48.     i = (i + 1) % L
  49.     canv.update()
  50.  
  51. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement