Advertisement
here2share

# tk_procedural_slither.py

Aug 11th, 2023
1,126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.17 KB | None | 0 0
  1. # tk_procedural_slither.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 = 160 # 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(700, 901)]
  23. angles += [-angle*0.0002 for angle in range(700, 901)]
  24.  
  25. def movement():
  26.     x = int(ww0 + math.cos(direction[0]) * (math.cos(direction[1]) * (ww0 - 15)))
  27.     y = int(hh0 + math.sin(direction[2]) * (math.sin(direction[3]) * (hh0 - 15)))
  28.     return (x, y)
  29.  
  30. body = range(1, L)
  31. for i in range(L):
  32.     points.append(movement())
  33.  
  34. while True:
  35.     canv.delete('all')
  36.     points.append(movement())
  37.     points = points[-L:]
  38.     angles.extend([angles.pop(0), angles.pop(9), angles.pop(21), angles.pop(79), angles.pop(7), angles.pop(101), angles.pop(57)])
  39.     direction = [angles[-i] + direction[i] for i in (0, 1, 2, 3)] # update the direction
  40.     canv.create_line(points, fill="blue", width=3, smooth=True, splinesteps=5)
  41.     canv.update()
  42.  
  43. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement