Advertisement
here2share

# tk_3D_procedural_slither.py

Aug 12th, 2023
1,470
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.68 KB | None | 0 0
  1. # tk_3D_procedural_slither.py
  2.  
  3. import tkinter as tk
  4. import math
  5. import random
  6.  
  7. ww = 600
  8. hh = 600
  9. cx, cy = ww//2, hh//2
  10. direction = [0, 0, 0, 0]  # initial direction
  11. degrees = math.radians(359.9999999999)
  12. print(degrees)
  13.  
  14. root = tk.Tk()
  15. root.title("tk_3D_procedural_slither")
  16. root.geometry("%dx%d+10+10"%(ww,hh))
  17. canvas = tk.Canvas(root, bg='white', width=ww, height=hh)
  18. canvas.pack()
  19.  
  20. points = []
  21. angles = [angle*0.0002 for angle in range(100, 401)]
  22. angles += [-angle*0.0002 for angle in range(100, 401)]
  23.  
  24. def movement():
  25.     x = int(cx + math.cos(direction[0]) * (math.cos(direction[1]) * (cx - 350)))
  26.     y = int(cy + math.sin(direction[2]) * (math.sin(direction[3]) * (cy - 250)))
  27.     return (x, y)
  28.  
  29.  
  30. rgb = [i for i in range(0, 256, 5)]
  31. colors = ['#{:02x}{:02x}{:02x}'.format(r,g,b) for r in rgb for g in rgb for b in rgb][40:-40]
  32. random.shuffle(colors)
  33.  
  34. p = []
  35. i = 0
  36. points.append(movement())
  37. scale = 1.01
  38. # Zoom into the center of the screen
  39. while True:
  40.     try:
  41.         canvas.delete(p.pop(-160))
  42.     except:
  43.         0
  44.     points.append(movement())
  45.     points = points[-2:]
  46.     angles.extend([angles.pop(0), angles.pop(101), angles.pop(57), angles.pop(21)])
  47.     angles.insert(11, angles.pop(99))
  48.     direction = [angles[-i] + direction[i] for i in (0, 1, 2, 3)] # update the direction
  49.     canvas.scale('all', cx, cy, scale, scale)
  50.     for item in canvas.find_all():
  51.         width = canvas.itemcget(item, 'width')
  52.         new_width = float(width) * 1.01
  53.         canvas.itemconfig(item, width=new_width)
  54.     color = colors.pop(0)
  55.     p += [canvas.create_line(points, smooth=True, fill=color, width=3, capstyle='round')]
  56.     canvas.lower(p[-1])
  57.     colors.insert(-(10-int(i%10)), color)
  58.     i += scale
  59.     i %= 999999999999
  60.     canvas.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement