Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- from random import randrange as rnd, random, choice
- import time, math
- root = Tk()
- root.geometry('800x600')
- canv = Canvas(root, bg = 'white')
- canv.pack(fill=BOTH,expand=1)
- colors = ['green','red','blue','yellow']
- class Ball():
- def __init__(self,x,y):
- self.x = x
- self.y = y
- self.r = r = rnd(5,30)
- #self.color = choice(colors)
- self.color = "#%02x%02x%02x" % (rnd(256), rnd(256), rnd(256))
- self.pen_color = 'blue'
- self.id = canv.create_oval(x-r,y-r,x+r,y+r,fill=self.color,outline=self.pen_color,width=3)
- def paint(self):
- canv.coords(self.id,self.x-self.r,self.y-self.r,self.x+self.r,self.y+self.r)
- canv.itemconfig(self.id,fill=self.color,outline=self.pen_color)
- def kill(self):
- canv.delete(self.id)
- balls.remove(self)
- def distanceTo(self,x,y):
- return ((self.x-x)**2 + (self.y-y)**2)**0.5
- def new(event=0):
- global balls
- balls = []
- canv.delete(ALL)
- for i in range(15):
- x = rnd(50,500)
- y = rnd(20,500)
- balls.append(Ball(x,y))
- x += 50
- def click(event):
- global m
- print(m)
- if not m:
- for ball in balls:
- if ball.distanceTo(event.x,event.y) < ball.r:
- m = ball
- else:
- m = None
- def move(event):
- if m:
- m.x = event.x
- m.y = event.y
- m.paint()
- m = None
- new()
- canv.bind('<1>',click)
- canv.bind('<Motion>',move)
- canv.bind('<3>',new)
- mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement