Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_Slither2.py
- from Tkinter import *
- import random
- import time
- HEIGHT=500
- WIDTH=800
- POPULATION=80
- def randomColor():
- return "#%03x" % random.randint(0, 0xFFF)
- def ball(x, y, r=10):
- return canvas.create_oval(x-r, y-r, x+r, y+r, fill=randomColor())
- root=Tk()
- canvas=Canvas(root, width=WIDTH, height=HEIGHT, bg="white")
- canvas.pack()
- root.wm_title("Slither2")
- class Cv():
- x=y=xm=ym=loc=0
- cv=Cv()
- r = 40
- locations = []
- for x in range(r,WIDTH-r,r):
- for y in range(r,HEIGHT/2-r,r):
- locations.append([x,y])
- 0
- balls=[]
- for count in range(POPULATION):
- balls.append(ball(400,400))
- balls.sort(reverse=True)
- def slither():
- while 1:
- if not cv.loc:
- cv.loc=locations[:]
- random.shuffle(cv.loc)
- cv.loc=cv.loc[:20]
- cv.loc.sort(reverse=True)
- x,y=cv.loc.pop()
- if x != cv.xm:
- cv.xm = x
- break
- cv.ym = HEIGHT/2+y*polar
- 0
- polar = -1
- slither()
- track = 0
- while 1:
- if track > 200:
- track=0
- slither()
- polar *= -1
- track+=1
- for t in range(POPULATION):
- objx,objy=canvas.coords(balls[t])[:2]
- if (t == 0):
- xv=0.09 * (cv.xm-objx)
- yv=0.09 * (cv.ym-objy)
- else:
- xv=0.09 * (cv.x-objx)
- yv=0.09 * (cv.y-objy)
- cv.x,cv.y=objx,objy
- canvas.move(balls[t], xv, yv)
- root.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement