Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_2D_ripples.py
- from tkinter import *
- import time
- from PIL import Image, ImageTk
- tx = time.time
- ww = 180
- hh = 180
- root = Tk()
- root.title("Tk_2D_ripples")
- root.geometry("%dx%d+-6+-2"%(ww,hh))
- canvas = Canvas(width=ww, height=hh)
- canvas.pack()
- def rgb2hex(r,g,b):
- return '#%02X%02X%02X'%(r,g,b)
- def motion(event):
- mouse_pos.append((event.x, event.y))
- root.bind('<B1-Motion>', motion)
- cols = ww
- rows = hh
- #dampening = how fast the ripple effect stops.
- dampening = 0.99999
- #Arrays that hold the colors of the screen.
- rgb = []
- xy = {}
- for j in range(0,rows):
- for i in range(0,cols):
- xy[i,j] = len(rgb)
- rgb += [(0)]
- current = rgb[:]
- previous = rgb[:]
- mouse_pos = []
- image = Image.new("RGB", (ww,hh))
- def draw():
- image.putdata(rgb)
- photo = ImageTk.PhotoImage(image)
- canvas.create_image(0,0,image=photo,anchor=NW)
- canvas.update()
- def sides(i,j):
- return ([i-1,j],[i+1,j],[i,j-1],[i,j+1])
- #Mainloop
- while 1:
- for i in range(1,cols-1):
- for j in range(1,rows-1):
- if mouse_pos:
- x,y = mouse_pos.pop(0)
- for x2,y2 in sides(x,y):
- try:
- t = xy[x2,y2]
- previous[t] = 500
- except:
- 0
- t = xy[i,j]
- sides2 = [previous[xy[x,y]] for x,y in sides(i,j)]
- current[t] = sum(sides2)*0.5-current[t]
- current[t] *= dampening
- val = min(255, max(0, int(current[t])))
- rgb[t] = (val,val,val)
- # print(tx)
- draw()
- # Switching the arrays
- previous, current = current, previous
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement