Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_Slither.py
- from Tkinter import *
- import random
- import time
- WIDTH=HEIGHT=800
- POPULATION=200
- 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("Slither")
- balls=[]
- for count in range(POPULATION):
- x=random.randint(0, WIDTH)
- y=random.randint(0, HEIGHT)
- balls.append(ball(x, y))
- class Cv():
- x=y=xm=ym=0
- cv=Cv()
- def slither(event):
- cv.xm, cv.ym=event.x, event.y
- canvas.bind("<Motion>", slither)
- while 1:
- for t in range(POPULATION):
- objx,objy=canvas.coords(balls[t])[:2]
- if (t == 0):
- xv=0.7 * (cv.xm-objx)
- yv=0.7 * (cv.ym-objy)
- else:
- xv=0.7 * (cv.x-objx)
- yv=0.7 * (cv.y-objy)
- cv.x,cv.y=objx,objy
- canvas.move(balls[t], xv, yv)
- time.sleep(0.03)
- root.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement