Advertisement
here2share

# Tk_Slither.py

Oct 17th, 2016
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.92 KB | None | 0 0
  1. # Tk_Slither.py
  2.  
  3. from Tkinter import *
  4. import random
  5. import time
  6.  
  7. WIDTH=HEIGHT=800
  8. POPULATION=200
  9.  
  10. def randomColor():
  11.     return "#%03x" % random.randint(0, 0xFFF)
  12.    
  13. def ball(x, y, r=10):
  14.     return canvas.create_oval(x-r, y-r, x+r, y+r, fill=randomColor())
  15.        
  16. root=Tk()
  17.  
  18. canvas=Canvas(root, width=WIDTH, height=HEIGHT, bg="white")
  19. canvas.pack()
  20. root.wm_title("Slither")
  21.  
  22. balls=[]
  23. for count in range(POPULATION):
  24.     x=random.randint(0, WIDTH)
  25.     y=random.randint(0, HEIGHT)
  26.     balls.append(ball(x, y))
  27.  
  28. class Cv():
  29.     x=y=xm=ym=0
  30. cv=Cv()
  31.  
  32. def slither(event):
  33.     cv.xm, cv.ym=event.x, event.y
  34.    
  35. canvas.bind("<Motion>", slither)
  36. while 1:
  37.     for t in range(POPULATION):
  38.         objx,objy=canvas.coords(balls[t])[:2]
  39.  
  40.         if (t == 0):
  41.             xv=0.7 * (cv.xm-objx)
  42.             yv=0.7 * (cv.ym-objy)
  43.         else:
  44.             xv=0.7 * (cv.x-objx)
  45.             yv=0.7 * (cv.y-objy)
  46.         cv.x,cv.y=objx,objy
  47.         canvas.move(balls[t], xv, yv)
  48.     time.sleep(0.03)
  49.     root.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement