Advertisement
here2share

# Tk_Slither2.py

Apr 23rd, 2019
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.25 KB | None | 0 0
  1. # Tk_Slither2.py
  2.  
  3. from Tkinter import *
  4. import random
  5. import time
  6.  
  7. HEIGHT=500
  8. WIDTH=800
  9. POPULATION=80
  10.  
  11. def randomColor():
  12.     return "#%03x" % random.randint(0, 0xFFF)
  13.    
  14. def ball(x, y, r=10):
  15.     return canvas.create_oval(x-r, y-r, x+r, y+r, fill=randomColor())
  16.        
  17. root=Tk()
  18.  
  19. canvas=Canvas(root, width=WIDTH, height=HEIGHT, bg="white")
  20. canvas.pack()
  21. root.wm_title("Slither2")
  22.  
  23. class Cv():
  24.     x=y=xm=ym=loc=0
  25. cv=Cv()
  26.  
  27. r = 40
  28. locations = []
  29. for x in range(r,WIDTH-r,r):
  30.     for y in range(r,HEIGHT/2-r,r):
  31.         locations.append([x,y])
  32. 0
  33. balls=[]
  34. for count in range(POPULATION):
  35.     balls.append(ball(400,400))
  36. balls.sort(reverse=True)
  37.  
  38. def slither():
  39.     while 1:
  40.         if not cv.loc:
  41.             cv.loc=locations[:]
  42.             random.shuffle(cv.loc)
  43.             cv.loc=cv.loc[:20]
  44.             cv.loc.sort(reverse=True)
  45.         x,y=cv.loc.pop()
  46.         if x != cv.xm:
  47.             cv.xm = x
  48.             break
  49.     cv.ym = HEIGHT/2+y*polar
  50. 0
  51. polar = -1
  52. slither()
  53. track = 0
  54. while 1:
  55.     if track > 200:
  56.         track=0
  57.         slither()
  58.         polar *= -1
  59.     track+=1
  60.     for t in range(POPULATION):
  61.         objx,objy=canvas.coords(balls[t])[:2]
  62.  
  63.         if (t == 0):
  64.             xv=0.09 * (cv.xm-objx)
  65.             yv=0.09 * (cv.ym-objy)
  66.         else:
  67.             xv=0.09 * (cv.x-objx)
  68.             yv=0.09 * (cv.y-objy)
  69.         cv.x,cv.y=objx,objy
  70.         canvas.move(balls[t], xv, yv)
  71.     root.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement