Advertisement
here2share

# Tk_sparkler.py

Jul 24th, 2017
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.27 KB | None | 0 0
  1. # Tk_sparkler.py
  2.  
  3. from Tkinter import *
  4. from random import randrange as rnd, random
  5. import time
  6.  
  7. root = Tk()
  8. root.geometry('800x600')
  9. canv = Canvas(bg='#ADC5E3')
  10. canv.pack(fill=BOTH,expand=1)
  11.  
  12. class Pixel():
  13.     def __init__(self,x,y,dx,dy,r,dr):
  14.         self.x = x
  15.         self.y = y
  16.         self.dx = dx
  17.         self.dy = dy
  18.         self.r = r
  19.         self.dr = dr
  20.         color = 'yellow'
  21.         self.id = canv.create_oval(x-r,y-r,x+r,y+r,fill=color,outline='')
  22.    
  23.     def move(self):
  24.         self.x += self.dx/2
  25.         self.y += self.dy/2
  26.         self.dx *= 0.99
  27.         self.dy *= 0.99
  28.         self.r -= self.dr
  29.         if self.r > 0:
  30.             canv.coords(self.id,self.x-self.r,self.y-self.r,self.x+self.r,self.y+self.r)
  31.             root.after(20,self.move)
  32.         else:
  33.             canv.delete(self.id)
  34.             if rnd(4) == 0:
  35.                 p = Pixel(self.x,self.y,rnd(-10,10),rnd(-10,10),random()*2,0.1+random())
  36.                 p.move()
  37.        
  38.  
  39. def newPixel():
  40.     p = Pixel(mx,my,rnd(-10,10),rnd(-10,10),rnd(5,9),0.1+random())
  41.     p.move()
  42.     root.after(10,newPixel)
  43.  
  44. def move(event):
  45.     global mx,my
  46.     mx,my = event.x,event.y
  47.    
  48. count = 0  
  49. mx = 400
  50. my = 300  
  51. canv.bind('<Motion>',move)
  52.  
  53. newPixel()
  54.  
  55. mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement