Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_fluid.py ZZZ
- import random
- from Tkinter import *
- from itertools import product
- s = "** Press Spacebar To Restart **"
- ppp = '''
- ## OOOOOOOOOOOOOOOO OOOOOO OOOOOO ##
- ## OOOOOOOOOOOOOOOO OOOOOO OOOOOO ##
- ## OOOOOOOOOOOOOOOO OOOOOO OOOOOO OOOOOO ##
- ## OOOOOO OOOOOO OOOOOO OOOOOO ##
- ## OOOOOO OOOOOO OOOOOO OOOOOO ##
- ## OOOOOO OOOOOO OOOOOO ##
- ## OOOOOO OOOOOO OOOOOO ##
- ## OOOOOOOOOOOOOOOO OOOOOO OOOOOO OOOOOO OOOOOO OOOOOO ##
- ## OOOOOOOOOOOOOOOO OOOOOO OOOOOO OOOOOO OOOOOO OOOOO OOOOOO ##
- ## OOOOOOOOOOOOOOOO OOOOOO OOOOOO OOOOOO OOOOOO OOOOOOOOOOOOOOOO ##
- ## OOOOOO OOOOOO OOOOOO OOOOOO OOOOOO OOOOOOOOOOOOOOOOO ##
- ## OOOOOO OOOOOO OOOOOO OOOOOO OOOOOO OOOOOOO OOOOOO ##
- ## OOOOOO #### OOOOOO OOOOOO OOOOOO OOOOOO OOOOOO OOOOOO ##
- ## OOOOOO #### OOOOOO OOOOOO OOOOOO OOOOOO OOOOOO OOOOOO ##
- ## OOOOOO #### OOOOOO OOOOOO OOOOOO OOOOOO OOOOOO OOOOOO ##
- ## OOOOOO OOOOOO OOOOOO OOOOOO OOOOOO OOOOOO OOOOOO ##
- ## OOOOOO OOOOOO OOOOOOO OOOOOO OOOOOO OOOOOOO OOOOOO ##
- ## OOOOOO OOOOOO OOOOOOOOOOOOOOOO OOOOOO OOOOOOOOOOOOOOOOO ##
- ## OOOOOO OOOOOO OOOOOOOO OOOOOO OOOOOO OOOOOOOOO OOOOOO ##
- ## OOOOOO OOOOOO OOOO OOOOOO OOOOOO OOOOO OOOOOO ##
- ## ##
- ## #### ########## ###### ##
- ## #### ###### ##
- ## #### ## ## ###### ##
- ## ## ########### ## ##
- ## ## #################### ##
- ################# ## ## ##
- ## ## ########## #######################
- ## ## ##
- ## ## ##
- ## ## ##
- ## ## ##
- ## ## ##
- ## ## ##
- ## ## ##
- ## ## ##
- ## ## ##
- ## ## ##
- ## ##
- ## ##
- ## ##
- ## ##
- ### ###
- #### ####
- #########################################################################
- ###################################################################
- '''.splitlines()
- nnn = (-1,-1),(0,-1),(1,-1),(-1,0),(0,0),(1,0),(-1,1),(0,1),(1,1)
- gravity = [-0.12,-0.1,-0.1,-0.01,0,-0.01,0.25,0.2,0.2]
- oLR = {3:1,5:2}
- def num_neighbors(pos):
- zzz = {}
- x,y = xy[pos]
- for z in range(9):
- a,b = nnn[z]
- try:
- zzz[z] = grid[(x+a,y+b)]
- except:
- zzz[z] = 2
- for z in [7,6,8]:
- if not zzz[z]:
- a,b = nnn[z]
- grid[(x+a,y+b)] = 1
- grid[(x,y)] = 0
- top[(x,y)] = 2
- xy[pos] = (x+a,y+b)
- vel[pos] += gravity[z]
- return
- LR = (0,(3,5),(5,3))[grid[(x,y)]]
- for z in LR:
- if not zzz[z]:
- a,b = nnn[z]
- grid[(x+a,y+b)] = oLR[z]
- grid[(x,y)] = 0
- top[(x,y)] = 20
- xy[pos] = (x+a,y+b)
- vel[pos] += gravity[z]
- return
- '''
- if vel[pos] > 1: # ZZZ upward splash
- for z in [1,0,2]:
- if not zzz[z]:
- a,b = nnn[z]
- grid[(x+a,y+b)] = 1
- grid[(x,y)] = 0
- xy[pos] = (x+a,y+b)
- vel[pos] += gravity[z]
- return
- '''
- 0
- def update_state():
- xy.sort(key=lambda z: z[1],reverse=1)
- for z in range(len(xy)):
- num_neighbors(z)
- 0
- def print_grid():
- ttt = list(top)
- for x,y in ttt:
- canvas.create_text(x*4+10,y*4+60,text='.', font=fff, fill='blue', tag='temp')
- top[(x,y)] -= 1
- t = (x,y+1)
- if not top[(x,y)]:
- del top[(x,y)]
- elif t in grid:
- if grid[t] in (0,'#'):
- del top[(x,y)]
- for x,y in xy:
- canvas.create_text(x*4+10,y*4+60,text='.', font=fff, fill='blue', tag='temp')
- canvas.update()
- canvas.delete('temp')
- 0
- fff = ('Times New Roman', 16, "bold")
- root = Tk()
- ww = len(ppp[1])*4+16
- hh = len(ppp)*4+80
- canvas = Canvas(root, bg='lightgray', height=hh, width=ww)
- canvas.pack()
- go = [1]
- def restart(e):
- go[0] = 0
- root.bind("<space>", restart)
- canvas.create_text(10, 36, text=s, font=('Ariel', 16, "italic"), fill='black', anchor=NW)
- s = 'A Basic Fluid Simulation'
- canvas.create_text(18, 6, text=s, font=('Ariel', 19, 'bold'), fill='darkred', anchor=NW)
- while 1:
- xy = []
- vel = []
- top = {}
- solid = []
- grid = {}
- for y in range(len(ppp)):
- for x in range(len(ppp[y])):
- if ppp[y][x] in (' '):
- grid[(x,y)] = 0
- elif ppp[y][x] in ('#'):
- grid[(x,y)] = '#'
- solid += [(x*4+10,y*4+60)]
- else:
- grid[(x,y)] = 1
- xy += [(x,y)]
- vel += [0]
- for x,y in solid:
- canvas.create_text(x, y, text='.', font=fff, fill='red')
- while go[0]:
- print_grid()
- update_state()
- go[0] = 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement