Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_procedural_slither2.py
- from tkinter import *
- import math
- ww = 800
- hh = 600
- ww0 = ww//2
- hh0 = hh//2
- L = 400 # snake length by pixel
- direction = [0, 0, 0, 0] # initial direction
- degrees = math.radians(359.9999999999)
- print(degrees)
- root = Tk()
- root.title("tk procedural slither")
- root.geometry("%dx%d+10+10"%(ww,hh))
- canv = Canvas(root, width=ww, height=hh)
- canv.pack()
- points = []
- angles = [angle*0.0002 for angle in range(100, 401)]
- angles += [-angle*0.0002 for angle in range(100, 401)]
- def movement():
- x = int(ww0 + math.cos(direction[0]) * (math.cos(direction[1]) * (ww0 - 15)))
- y = int(hh0 + math.sin(direction[2]) * (math.sin(direction[3]) * (hh0 - 15)))
- return (x, y)
- p = {}
- point = movement()
- points.append(point)
- for i in range(L):
- point = movement()
- points.append(point)
- p[i] = canv.create_line(points, fill="blue", width=3, smooth=True, splinesteps=5)
- i = 0
- while True:
- canv.delete(p[i])
- points.append(movement())
- points = points[-L:]
- angles.extend([angles.pop(0), angles.pop(7), angles.pop(27), angles.pop(79), angles.pop(101), angles.pop(57)])
- angles.insert(11, angles.pop(99))
- direction = [angles[-i] + direction[i] for i in (0, 1, 2, 3)] # update the direction
- p[i] = canv.create_line(points[-2:], fill="blue", width=3, smooth=True, splinesteps=5)
- i = (i + 1) % L
- canv.update()
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement