Advertisement
here2share

# Tk_2D_ripples.py

Mar 22nd, 2022
1,090
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.50 KB | None | 0 0
  1. # Tk_2D_ripples.py
  2.  
  3. from tkinter import *
  4. import time
  5. from PIL import Image, ImageTk
  6.  
  7. tx =  time.time
  8. ww = 180
  9. hh = 180
  10.  
  11. root = Tk()
  12. root.title("Tk_2D_ripples")
  13. root.geometry("%dx%d+-6+-2"%(ww,hh))
  14. canvas = Canvas(width=ww, height=hh)
  15. canvas.pack()
  16.  
  17. def rgb2hex(r,g,b):
  18.     return '#%02X%02X%02X'%(r,g,b)
  19.    
  20. def motion(event):
  21.     mouse_pos.append((event.x, event.y))
  22. root.bind('<B1-Motion>', motion)
  23.  
  24. cols = ww
  25. rows = hh
  26.  
  27. #dampening = how fast the ripple effect stops.
  28. dampening = 0.99999
  29.  
  30. #Arrays that hold the colors of the screen.
  31. rgb = []
  32. xy = {}
  33. for j in range(0,rows):
  34.     for i in range(0,cols):
  35.         xy[i,j] = len(rgb)
  36.         rgb += [(0)]
  37. current = rgb[:]
  38. previous = rgb[:]
  39.  
  40. mouse_pos = []
  41. image = Image.new("RGB", (ww,hh))
  42.  
  43. def draw():
  44.     image.putdata(rgb)
  45.     photo = ImageTk.PhotoImage(image)
  46.     canvas.create_image(0,0,image=photo,anchor=NW)
  47.     canvas.update()
  48.  
  49. def sides(i,j):
  50.     return ([i-1,j],[i+1,j],[i,j-1],[i,j+1])
  51.  
  52. #Mainloop
  53. while 1:
  54.     for i in range(1,cols-1):
  55.         for j in range(1,rows-1):
  56.             if mouse_pos:
  57.                 x,y = mouse_pos.pop(0)
  58.                 for x2,y2 in  sides(x,y):
  59.                     try:
  60.                         t = xy[x2,y2]
  61.                         previous[t] = 500
  62.                     except:
  63.                         0
  64.                        
  65.             t = xy[i,j]
  66.            
  67.             sides2 = [previous[xy[x,y]] for x,y in sides(i,j)]
  68.             current[t] = sum(sides2)*0.5-current[t]
  69.             current[t] *= dampening
  70.            
  71.             val = min(255, max(0, int(current[t])))
  72.             rgb[t] = (val,val,val)
  73.  
  74.     # print(tx)
  75.  
  76.     draw()
  77.  
  78.     # Switching the arrays
  79.     previous, current = current, previous
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement