Advertisement
boris-vlasenko

Прилипчивые шарики

Apr 11th, 2016
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.39 KB | None | 0 0
  1. from tkinter import *
  2. from random import randrange as rnd, random, choice
  3. import time, math
  4.  
  5. root = Tk()
  6. root.geometry('800x600')
  7. canv = Canvas(root, bg = 'white')
  8. canv.pack(fill=BOTH,expand=1)
  9.  
  10. colors = ['green','red','blue','yellow']
  11.  
  12.        
  13.        
  14. class Ball():
  15.     def __init__(self,x,y):
  16.         self.x = x
  17.         self.y = y
  18.         self.r = r = rnd(5,30)
  19.         #self.color = choice(colors)
  20.         self.color = "#%02x%02x%02x" % (rnd(256), rnd(256), rnd(256))
  21.         self.pen_color = 'blue'
  22.         self.id = canv.create_oval(x-r,y-r,x+r,y+r,fill=self.color,outline=self.pen_color,width=3)
  23.    
  24.     def paint(self):
  25.         canv.coords(self.id,self.x-self.r,self.y-self.r,self.x+self.r,self.y+self.r)
  26.         canv.itemconfig(self.id,fill=self.color,outline=self.pen_color)
  27.        
  28.     def kill(self):
  29.         canv.delete(self.id)
  30.         balls.remove(self)
  31.    
  32.     def distanceTo(self,x,y):
  33.         return ((self.x-x)**2 + (self.y-y)**2)**0.5
  34.    
  35. def new(event=0):
  36.     global balls
  37.     balls = []
  38.     canv.delete(ALL)
  39.    
  40.     for i in range(15):
  41.         x = rnd(50,500)
  42.         y = rnd(20,500)
  43.         balls.append(Ball(x,y))
  44.         x += 50
  45.    
  46. def click(event):
  47.     global m
  48.     print(m)
  49.     if not m:
  50.         for ball in balls:
  51.             if ball.distanceTo(event.x,event.y) < ball.r:
  52.                 m = ball
  53.     else:
  54.         m = None
  55.  
  56. def move(event):
  57.     if m:
  58.         m.x = event.x
  59.         m.y = event.y
  60.         m.paint()
  61.  
  62. m = None
  63.  
  64. new()
  65.  
  66. canv.bind('<1>',click)
  67. canv.bind('<Motion>',move)
  68. canv.bind('<3>',new)
  69.  
  70. mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement