Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_procedural_slither.py
- from tkinter import *
- import math
- ww = 800
- hh = 600
- ww0 = ww//2
- hh0 = hh//2
- L = 160 # 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(700, 901)]
- angles += [-angle*0.0002 for angle in range(700, 901)]
- 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)
- body = range(1, L)
- for i in range(L):
- points.append(movement())
- while True:
- canv.delete('all')
- points.append(movement())
- points = points[-L:]
- angles.extend([angles.pop(0), angles.pop(9), angles.pop(21), angles.pop(79), angles.pop(7), angles.pop(101), angles.pop(57)])
- direction = [angles[-i] + direction[i] for i in (0, 1, 2, 3)] # update the direction
- canv.create_line(points, fill="blue", width=3, smooth=True, splinesteps=5)
- canv.update()
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement